39d54907a09b6cd4073d5712cadcea3b6655ee5d
[splitter-ng] / Splitter-ng-plugin-bitsplitter / src / bitsplitterplugin / CodecBitsplitter.java
1 package bitsplitterplugin;
2
3
4
5 import java.io.FileInputStream;
6 import java.io.FileNotFoundException;
7 import java.io.FileOutputStream;
8 import java.io.IOException;
9 import java.nio.file.Files;
10 import java.nio.file.Path;
11 import java.util.Collections;
12 import java.util.HashMap;
13 import java.util.Map;
14 import java.util.Map.Entry;
15
16 import splitterng.plugin.CodecDescription;
17 import splitterng.plugin.interfaces.SplitterCodec;
18 import BitSplitter.BitSplitter;
19
20
21 public class CodecBitsplitter implements SplitterCodec {
22
23         public final static String codecID = "bitsplitter";
24         public final static String codecName = "Bitsplitter";
25         public final static String codecDescriptionText = "Simple bitsplitter. 1234567812345678 -> 12341234 56785678";
26
27         public final static Map <String, Integer []> codecParameterMinMax;
28
29         private int k,m,w,packetsize;
30         
31         static {
32                 Map <String, Integer []> tmpMap = new HashMap<String, Integer[]>();
33                 tmpMap.put("k", new Integer[] {new Integer(2),new Integer(Integer.MAX_VALUE)});
34                 tmpMap.put("m", new Integer[] {new Integer(0),new Integer(0)});
35                 
36                 codecParameterMinMax  = Collections.unmodifiableMap(tmpMap);                            
37         }
38         
39         
40         private static final CodecDescription codecDescription = new CodecDescription(
41         codecID, codecName, codecDescriptionText, codecParameterMinMax);
42         
43     private byte[][] coding_ptrs;
44         private byte[][] data_ptrs;
45         private byte[][] dataAndCoding_ptrs;
46         BitSplitter bs;
47         private int [] erasures;
48         private byte[] data_ptrs_flat;
49         
50         
51         public static final CodecDescription getCodecDescription(){
52                 //System.out.println("JerasureCodecParity: getCodecDescription: "+JerasureCodecParity.codecDescription);
53                 return CodecBitsplitter.codecDescription;
54         }
55         
56         /**
57          * @param codecParameter
58          */
59         public CodecBitsplitter(Map<String, Integer> codecParameter) {
60                 super();
61                 //Test CodecParameter
62                 for (Entry<String, Integer[]> requiredEntry : CodecBitsplitter.codecParameterMinMax
63                                 .entrySet()) {
64                         if (codecParameter.containsKey(requiredEntry.getKey())) {
65                                 // Test ob Parameter vorhanden und in den gültigen grenzen
66                                 // liegen
67                                 //System.out.println("Test" +codecParameter.get(requiredEntry.getKey()) +" "+  requiredEntry.getValue()[0]);
68                                 if (codecParameter.get(requiredEntry.getKey()) < requiredEntry.getValue()[0])
69                                         throw new IllegalArgumentException(
70                                                         "unzulässiger Wert für Parameter: "
71                                                                         + requiredEntry.getKey() + " < "
72                                                                         + requiredEntry.getValue()[0]);
73                                 if (codecParameter.get(requiredEntry.getKey()) > requiredEntry.getValue()[1])
74                                         throw new IllegalArgumentException(
75                                                         "unzulässiger Wert für Parameter: "
76                                                                         + requiredEntry.getKey() + " > "
77                                                                         + requiredEntry.getValue()[1]);
78                         } else {
79                                 throw new IllegalArgumentException("Missing Codec Parameter: "
80                                                 + requiredEntry.getKey());
81                         }
82                 }
83                 
84                 k=(int)codecParameter.get("k");
85                 m=(int)codecParameter.get("m");
86                 //System.out.println("Init Bitsplitter");
87                 bs = new BitSplitter();
88         }
89
90         @Override
91         public byte[][] decode(byte[][] dataAndCoding_ptrs, boolean [] erased){
92                 //TODO Array Testen ob zulässig? eventuell nicht, da es hier bremsen könnte
93                 
94                 int fragmentsize = dataAndCoding_ptrs[0].length;
95                 
96                 if (data_ptrs_flat == null || (data_ptrs_flat.length != k * fragmentsize)){
97                         data_ptrs_flat = new byte[k * fragmentsize];
98                 }
99                 if (data_ptrs == null || (data_ptrs.length != k) || (data_ptrs[0].length != fragmentsize)){
100                         data_ptrs = new byte[k][fragmentsize];
101                 }                       
102                 
103                 bs.bitjoinbuffer(dataAndCoding_ptrs, data_ptrs_flat, k);
104
105                 for (int i=0; i<k; i++){
106                         System.arraycopy(data_ptrs_flat, i*fragmentsize, data_ptrs[i], 0, fragmentsize);
107                 }
108                 return data_ptrs;
109         }
110
111         @Override
112         public byte[][] encode(byte[][] data_ptrs){
113                 //TODO Array Testen ob zulässig? eventuell nicht, da es hier bremsen könnte
114                 // buffersize%(sizeof(int)*w*k) == 0
115                 int fragmentsize = data_ptrs[0].length;
116                 
117                 if (data_ptrs_flat == null || (data_ptrs_flat.length != k * fragmentsize)){
118                         data_ptrs_flat = new byte[k * fragmentsize];
119                 }
120                 for (int i=0; i < k; i++) {
121                         System.arraycopy(data_ptrs[i], 0, data_ptrs_flat, i*fragmentsize , fragmentsize);
122                 }               
123                 if (dataAndCoding_ptrs == null || (dataAndCoding_ptrs.length != k+m) || (dataAndCoding_ptrs[0].length != Math.ceil(fragmentsize/k))){
124                         //System.out.println("dataAndCoding_ptrs neu");
125                         dataAndCoding_ptrs = new byte[k+m][(int) Math.ceil(fragmentsize/k)];
126                 }else {
127                         //System.out.println("dataAndCoding_ptrs reuse");
128                 }
129                 bs.bitsplitbuffer(data_ptrs_flat, dataAndCoding_ptrs, k, m);
130                                 
131                 return dataAndCoding_ptrs;
132         }
133         private void checkArraySizes(int fragmentsize) {
134                 
135                 if (data_ptrs == null || (data_ptrs.length != k) || (data_ptrs[0].length != fragmentsize)){
136                         data_ptrs = new byte[k][fragmentsize];
137                 }                       
138                 
139                 //Test reuse or create new
140 //              if (data_ptrs_flat == null || (data_ptrs_flat.length != k * fragmentsize)){
141 //                      data_ptrs_flat = new byte[k * fragmentsize];
142 //              }
143 //              
144 //              
145 //              if (coding_ptrs_flat == null || (coding_ptrs_flat.length != m * fragmentsize)){
146 //                      coding_ptrs_flat = new byte[m * fragmentsize];
147 //              }
148                 
149 //              if (coding_ptrs == null || (coding_ptrs.length != m ) || (coding_ptrs[0].length != fragmentsize)){
150 //                      coding_ptrs = new byte[m][fragmentsize];
151 //              }
152                 
153                 if (dataAndCoding_ptrs == null || (dataAndCoding_ptrs.length != k+m) || (dataAndCoding_ptrs[0].length != fragmentsize/k +1)){
154                         //System.out.println("dataAndCoding_ptrs neu");
155                         dataAndCoding_ptrs = new byte[k+m][fragmentsize/k +1];
156                 }else {
157                         //System.out.println("dataAndCoding_ptrs reuse");
158                 }
159         }
160         private void fill(int fragmentsize) {
161         
162         }
163         
164 }