2424import java .io .FileNotFoundException ;
2525import java .io .FileWriter ;
2626import java .io .IOException ;
27+ import java .nio .charset .StandardCharsets ;
2728import java .security .InvalidParameterException ;
28- import java .util .Arrays ;
2929import java .util .Map ;
30+ import java .util .StringJoiner ;
3031
3132import org .strongback .tools .utils .FileUtils ;
3233import org .strongback .tools .utils .Parser ;
@@ -180,33 +181,41 @@ public static final void main(String[] args) {
180181 }
181182 }
182183
184+ private static String readString (DataInputStream in ) throws EOFException , IOException {
185+ int len = in .readInt ();
186+ byte [] value = new byte [len ];
187+ in .read (value );
188+ return new String (value ,StandardCharsets .UTF_8 );
189+ }
190+
183191 /* Actually converts the log */
184192 private static final void decode (DataInputStream in , BufferedWriter writer )
185193 throws BadFileFormatException , EOFException , IOException {
186194 // Verify Header
187195 printer .print (Strings .CHECK_LOG , Printer .Verbosity .VERBOSE );
188- byte [] header = new byte [ 3 ] ;
189- in . read ( header );
190- if (!Arrays . equals ( header , "log" . getBytes () )) throw new BadFileFormatException ();
196+ String header = readString ( in ) ;
197+ printer . print ( "Found header = " + header , Printer . Verbosity . VERBOSE );
198+ if (!"data-record" . equals ( header )) throw new BadFileFormatException ();
191199 printer .print (Strings .SUCCESS , Printer .Verbosity .VERBOSE );
192200
193- // Get the number of elements
194- int numElements = in .read ();
201+ // Get the number of channels
202+ int numElements = in .readInt ();
195203 printer .print (Strings .ELEMENT_COUNT + numElements , Printer .Verbosity .VERBOSE );
196204
197- // Get the size of each element
205+ // Get the size of each channel sample
198206 int [] elementSizes = new int [numElements ];
199207 for (int i = 0 ; i < elementSizes .length ; i ++) {
200- elementSizes [i ] = in .read ();
208+ elementSizes [i ] = in .readInt ();
209+ printer .print ("read channel " + i + " size = " + elementSizes [i ], Printer .Verbosity .VERBOSE );
201210 }
202211
203- // Write the name of each element
212+ // Read and write the name of each channel
213+ StringJoiner joiner = new StringJoiner ("," );
204214 for (int i = 0 ; i < numElements ; i ++) {
205- int nameSize = in .read ();
206- byte [] b = new byte [nameSize ];
207- in .read (b );
208- writer .write (new String (b ) + ", " );
215+ joiner .add (readString (in ));
209216 }
217+ writer .write (joiner .toString ());
218+ printer .print ("channel names = " + joiner , Printer .Verbosity .VERBOSE );
210219 writer .newLine ();
211220
212221 printer .print (Strings .READING_LOG , Printer .Verbosity .VERBOSE );
@@ -218,11 +227,16 @@ private static final void decode(DataInputStream in, BufferedWriter writer)
218227 printer .print (Strings .READ_LINE + lineCount , Printer .Verbosity .VERBOSE );
219228 in .reset ();
220229 for (int i = 0 ; i < numElements ; i ++) {
230+ if (i !=0 ) writer .write ("," );
221231 if (elementSizes [i ]==4 ){
222- writer .write (in .readInt () + ", " );
232+ int value = in .readInt ();
233+ writer .write (Integer .toString (value ));
234+ } else if (elementSizes [i ]==2 ) {
235+ short value = in .readShort ();
236+ writer .write (Short .toString (value ));
237+ } else {
238+ throw new IOException ("Unexpected size of data: " + elementSizes [i ]);
223239 }
224- else if (elementSizes [i ]==2 )
225- writer .write (in .readShort () + ", " );
226240 }
227241 writer .newLine ();
228242 lineCount ++;
0 commit comments