@@ -127,6 +127,7 @@ public void run() {
127127 createOrOverrideFile (fileName , content );
128128
129129 } else if (operation == DEFAULT_OPERATION ) {
130+ logger .write ("LiveSync: input stream is empty!" );
130131 break ;
131132 } else {
132133 throw new IllegalArgumentException (String .format ("\n LiveSync: Operation not recognised. %s" , LIVESYNC_ERROR_SUGGESTION ));
@@ -135,7 +136,7 @@ public void run() {
135136 } while (this .input .available () > 0 );
136137
137138 } catch (Exception e ) {
138- logger .write ("Error while LiveSyncing: " + e .toString ());
139+ logger .write (String . format ( "Error while LiveSyncing: %s" , e .toString () ));
139140 e .printStackTrace ();
140141 exceptionWhileLivesyncing = true ;
141142 } finally {
@@ -160,60 +161,86 @@ private int getOperation() {
160161 try {
161162
162163 byte [] operationBuff = readNextBytes (OPERATION_BYTE_SIZE );
163- String operationAsString = new String (operationBuff );
164- boolean noOperationRead = operationAsString .trim ().isEmpty ();
165- if (noOperationRead ) {
164+ if (operationBuff == null ) {
166165 return DEFAULT_OPERATION ;
167166 }
168- operation = Integer .parseInt (operationAsString );
167+ operation = Integer .parseInt (new String ( operationBuff ) );
169168
169+ } catch (NumberFormatException e ) {
170+ throw new IllegalStateException (String .format ("\n LiveSync: failed to parse %s. %s\n original exception: %s" , OPERATION , LIVESYNC_ERROR_SUGGESTION , e .toString ()));
170171 } catch (Exception e ) {
171- throw new IllegalStateException (String .format ("\n LiveSync: failed to parse " + OPERATION + ". %s" , LIVESYNC_ERROR_SUGGESTION ));
172+ throw new IllegalStateException (String .format ("\n LiveSync: failed to parse %s. %s \n original exception: %s" , OPERATION , LIVESYNC_ERROR_SUGGESTION , e . toString () ));
172173 }
173174 return operation ;
174175 }
175176
176177 private String getFileName () {
177178 byte [] fileNameBuffer ;
178179 int fileNameLenth = -1 ;
180+ byte [] fileNameLengthBuffer ;
181+
179182 try {
180183
181- byte [] fileNameLengthBuffer = readNextBytes (FILE_NAME_LENGTH_BYTE_SIZE );
182- fileNameLenth = Integer .valueOf (new String (fileNameLengthBuffer ));
184+ fileNameLengthBuffer = readNextBytes (FILE_NAME_LENGTH_BYTE_SIZE );
183185
184186 } catch (Exception e ) {
185- throw new IllegalStateException (String .format ("\n LiveSync: failed to parse %s. %s" , FILE_NAME_LENGTH , LIVESYNC_ERROR_SUGGESTION ));
187+ throw new IllegalStateException (String .format ("\n LiveSync: failed to parse %s. %s\n original exception: %s " , FILE_NAME_LENGTH , LIVESYNC_ERROR_SUGGESTION , e . toString () ));
186188 }
187189
188- try {
190+ if (fileNameLengthBuffer == null ) {
191+ throw new IllegalStateException (String .format ("\n LiveSync: Missing %s bytes. %s" , FILE_NAME_LENGTH , LIVESYNC_ERROR_SUGGESTION ));
192+ }
189193
194+ try {
195+ fileNameLenth = Integer .valueOf (new String (fileNameLengthBuffer ));
190196 fileNameBuffer = readNextBytes (fileNameLenth );
191197
198+ } catch (NumberFormatException e ) {
199+ throw new IllegalStateException (String .format ("\n LiveSync: failed to parse %s. %s\n original exception: %s" , FILE_NAME , LIVESYNC_ERROR_SUGGESTION , e .toString ()));
192200 } catch (Exception e ) {
193- throw new IllegalStateException (String .format ("\n LiveSync: failed to parse %s. %s" , FILE_NAME , LIVESYNC_ERROR_SUGGESTION ));
201+ throw new IllegalStateException (String .format ("\n LiveSync: failed to parse %s. %s\n original exception: %s " , FILE_NAME , LIVESYNC_ERROR_SUGGESTION , e . toString () ));
194202 }
195- return new String (fileNameBuffer );
203+
204+ if (fileNameBuffer == null ) {
205+ throw new IllegalStateException (String .format ("\n LiveSync: Missing %s bytes. %s" , FILE_NAME , LIVESYNC_ERROR_SUGGESTION ));
206+ }
207+
208+ String fileName = new String (fileNameBuffer );
209+ if (fileName .trim ().length () < fileNameLenth ) {
210+ logger .write (String .format ("WARNING: %s parsed length is less than %s. We read less information than you specified!" , FILE_NAME , FILE_NAME_LENGTH ));
211+ }
212+
213+ return fileName .trim ();
196214 }
197215
198216 private byte [] getFileContent () throws IOException {
199217 byte [] contentBuff ;
200218 int contentL = -1 ;
219+ byte [] contentLength ;
201220 try {
202-
203- byte [] contentLength = readNextBytes (CONTENT_LENGTH_BYTE_SIZE );
204- contentL = Integer .parseInt (new String (contentLength ));
205-
221+ contentLength = readNextBytes (CONTENT_LENGTH_BYTE_SIZE );
206222 } catch (Exception e ) {
207- throw new IllegalStateException (String .format ("\n LiveSync: failed to parse %s. %s" , FILE_CONTENT_LENGTH , LIVESYNC_ERROR_SUGGESTION ));
223+ throw new IllegalStateException (String .format ("\n LiveSync: failed to parse %s. %s\n original exception: %s " , FILE_CONTENT_LENGTH , LIVESYNC_ERROR_SUGGESTION , e . toString () ));
208224 }
209225
210- try {
226+ if (contentLength == null ) {
227+ throw new IllegalStateException (String .format ("\n LiveSync: Missing %s bytes. %s" , FILE_CONTENT_LENGTH , LIVESYNC_ERROR_SUGGESTION ));
228+ }
211229
230+ try {
231+ contentL = Integer .parseInt (new String (contentLength ));
212232 contentBuff = readNextBytes (contentL );
213233
234+ } catch (NumberFormatException e ) {
235+ throw new IllegalStateException (String .format ("\n LiveSync: failed to parse %s. %s\n original exception: %s" , FILE_CONTENT_LENGTH , LIVESYNC_ERROR_SUGGESTION , e .toString ()));
214236 } catch (Exception e ) {
215- throw new IllegalStateException (String .format ("\n LiveSync: failed to parse %s. %s" , FILE_CONTENT , LIVESYNC_ERROR_SUGGESTION ));
237+ throw new IllegalStateException (String .format ("\n LiveSync: failed to parse %s. %s\n original exception: %s" , FILE_CONTENT , LIVESYNC_ERROR_SUGGESTION , e .toString ()));
238+ }
239+
240+ if (contentBuff == null ) {
241+ throw new IllegalStateException (String .format ("\n LiveSync: Missing %s bytes. %s" , FILE_CONTENT , LIVESYNC_ERROR_SUGGESTION ));
216242 }
243+
217244 return contentBuff ;
218245 }
219246
@@ -227,7 +254,7 @@ private void createOrOverrideFile(String fileName, byte[] content) throws IOExce
227254 fos .close ();
228255
229256 } catch (Exception e ) {
230- throw new IOException (String .format ("\n LiveSync: failed to write file: %s" , fileName ));
257+ throw new IOException (String .format ("\n LiveSync: failed to write file: %s\n original exception: %s " , fileName , e . toString () ));
231258 }
232259 }
233260
@@ -260,6 +287,9 @@ private byte[] readNextBytes(int size) throws IOException {
260287
261288 bytesRead = this .input .read (buffer , bufferWriteOffset , size );
262289 if (bytesRead == -1 ) {
290+ if (bufferWriteOffset == 0 ) {
291+ return null ;
292+ }
263293 break ;
264294 }
265295 size -= bytesRead ;
0 commit comments