Splitter-NG CodingTask: add deepCopy functionality for metadata map
[splitter-ng] / Splitter-ng / src / splitterng / CodingTask.java
1 package splitterng;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.ByteArrayOutputStream;
5 import java.io.IOException;
6 import java.io.ObjectInputStream;
7 import java.io.ObjectOutputStream;
8 import java.nio.file.Path;
9 import java.util.Map;
10
11 public abstract class CodingTask {
12
13         protected Path filePath;
14         protected byte[] data;
15         
16         protected Map <Integer, Path>    fragmentPathsMap;
17         protected Map <Integer, byte []> dataFragmentMap = null;
18         protected Path outputDir;
19         
20         protected Map <String,Object> metadataMap;
21         protected SplitterCallback callback;
22         protected boolean codingFinished = false;
23         protected boolean codingSuccessfull = false;
24         protected String codingErrMsg = "";
25         protected boolean isFileTask = true;
26         
27         protected int fragmentsize;
28         protected int bufferSize;
29         protected long readins;
30         protected long readin = 0;
31         protected long writeout = 0;
32         protected long originalSize;
33         protected int truncate;
34         protected long readBytesTotal=0;
35         protected long writtenBytesTotal=0;
36         
37         protected byte [][] workingDataFragemnts;
38         
39         public abstract boolean hasDataToRead();
40         
41         public boolean isCodingFinished() {
42                 return codingFinished;
43         }       
44         
45         public abstract boolean isFileTask();
46
47         protected void setCodingFinished(boolean codingFinished) {
48                 this.codingFinished = codingFinished;
49         }
50         
51         public boolean isCodingSuccessfull() {
52                 return codingSuccessfull;
53         }
54
55         protected void setCodingSuccessfull(boolean codingSuccessfull) {
56                 this.codingSuccessfull = codingSuccessfull;
57         }
58
59         public String getCodingErrMsg() {
60                 return codingErrMsg;
61         }
62
63         public void setCodingErrMsg(String codingErrMsg) {
64                 this.codingErrMsg = codingErrMsg;
65         }
66
67         public SplitterCallback getCallback() {
68                 return callback;
69         }
70
71
72         public void setCallback(SplitterCallback callback) {
73                 this.callback = callback;
74         }
75
76
77         public Path getFilePath() {
78                 return filePath;
79         }
80
81
82         public void setFilePath(Path filePath) {
83                 this.filePath = filePath;
84         }
85
86         public byte [] getData() {
87                 return this.data;
88         }
89
90
91         public void setData(byte[] data) {
92                 this.data = data;
93         }
94
95         public Map<Integer, Path> getFragmentPaths() {
96                 return fragmentPathsMap;
97         }
98         
99         public Map<Integer, byte[]> getFragmentData() {
100                 return this.dataFragmentMap;
101         }
102
103         public void setFragmentPaths(Map<Integer, Path> fragmentPaths) {
104                 this.fragmentPathsMap = fragmentPaths;
105         }
106
107         public void setFragmentPath(Integer fragment_nr, Path fragment_path){
108                 fragmentPathsMap.put(fragment_nr, fragment_path);
109         }
110
111
112         public Map<String, Object> getMetadata() {
113                 return metadataMap;
114         }
115         
116         public Map<String, Object> getMetadataAsDeepCopy() throws Exception {
117                 return CodingTask.deepCopy(metadataMap);
118         }
119         
120         public Object getMetadata(String key) {
121                 return metadataMap.get(key);
122         }
123
124         public void setMetadataMap(Map<String, Object> metadataMap) {
125                 this.metadataMap = metadataMap;
126         }
127         
128         public void setMetadataMapAsDeepCopy(Map<String, Object> metadataMap) throws Exception {
129                 this.metadataMap = CodingTask.deepCopy(metadataMap);
130         }
131         
132         public void setMetadata(String name, Object object) {
133                 this.metadataMap.put(name, object);
134         }
135
136         @SuppressWarnings("unchecked")
137         public static <T> T deepCopy( T o ) throws Exception
138         {
139           ByteArrayOutputStream baos = new ByteArrayOutputStream();
140           new ObjectOutputStream( baos ).writeObject( o );
141
142           ByteArrayInputStream bais = new ByteArrayInputStream( baos.toByteArray() );
143           Object p = new ObjectInputStream( bais ).readObject();
144
145           return (T) p;
146         }
147 //      public boolean isEncodingTask() {
148 //              return isEncodingTask;
149 //      }
150 //
151 //
152 //      public void setEncodingTask(boolean isEncodingTask) {
153 //              this.isEncodingTask = isEncodingTask;
154 //      }
155 }
156