From fd7a05859fc66725d54945f3c33e75d501b3e524 Mon Sep 17 00:00:00 2001 From: Björn Hagemeister Date: Thu, 10 Jul 2014 15:12:21 +0200 Subject: Finished implementing exception handling. --- .../java/org/openslx/filetransfer/Uploader.java | 57 +++++++++++++++++++--- 1 file changed, 51 insertions(+), 6 deletions(-) (limited to 'src/main/java/org/openslx/filetransfer/Uploader.java') diff --git a/src/main/java/org/openslx/filetransfer/Uploader.java b/src/main/java/org/openslx/filetransfer/Uploader.java index 26f8879..7312600 100644 --- a/src/main/java/org/openslx/filetransfer/Uploader.java +++ b/src/main/java/org/openslx/filetransfer/Uploader.java @@ -95,9 +95,21 @@ public class Uploader byte[] data = sendToken.getBytes( StandardCharsets.UTF_8 ); dataToServer.writeByte( data.length ); dataToServer.write( data ); + } catch ( SocketTimeoutException ste) { + ste.printStackTrace(); + log.info( "Socket timeout occured ... close connection." ); + this.close(); + return false; } catch ( IOException e ) { e.printStackTrace(); - + readMetaData(); + if (ERROR != null) { + if (ERROR == "timeout") { + log.info( "Socket timeout occured ... close connection." ); + this.close(); + } + } + log.info( "Sending TOKEN in Uploader failed..." ); return false; } return true; @@ -130,8 +142,20 @@ public class Uploader dataToServer.writeByte( data.length ); dataToServer.write( data ); dataToServer.writeByte( 0 ); + } catch ( SocketTimeoutException ste ) { + ste.printStackTrace(); + log.info( "Socket timeout occured ... close connection." ); + this.close(); } catch ( IOException e ) { e.printStackTrace(); + readMetaData(); + if (ERROR != null) { + if (ERROR == "timeout") { + log.info( "Socket timeout occured ... close connection." ); + this.close(); + } + } + log.info( "Sending RANGE in Uploader failed..." ); return false; } return true; @@ -247,14 +271,13 @@ public class Uploader if ( splitted[1] != null ) ERROR = splitted[1]; System.err.println( "ERROR: " + ERROR ); - this.close(); return false; } } } catch (SocketTimeoutException ste) { ste.printStackTrace(); sendErrorCode("timeout"); - log.info( "Socket Timeout occured in Downloader." ); + log.info( "Socket timeout occured ... close connection" ); this.close(); } catch ( Exception e ) { e.printStackTrace(); @@ -271,8 +294,9 @@ public class Uploader */ public Boolean sendFile( String filename ) { + RandomAccessFile file = null; try { - RandomAccessFile file = new RandomAccessFile( new File( filename ), "r" ); + file = new RandomAccessFile( new File( filename ), "r" ); if ( getStartOfRange() == -1 ) { file.close(); @@ -289,17 +313,38 @@ public class Uploader if ( ret == -1 ) { System.out.println( "Error occured in Uploader.sendFile()," + " while reading from File to send." ); - file.close(); return false; } hasRead += ret; dataToServer.write( data, 0, ret ); } file.close(); - + } catch ( SocketTimeoutException ste) { + ste.printStackTrace(); + sendErrorCode( "timeout" ); + log.info( "Socket timeout occured ... close connection." ); + this.close(); + return false; + } catch ( IOException ioe ) { + ioe.printStackTrace(); + readMetaData(); + if (ERROR != null) { + if (ERROR == "timeout") { + log.info( "Socket timeout occured ... close connection." ); + this.close(); + } + } + log.info( "Sending RANGE " + getStartOfRange() + ":" + getEndOfRange() + " of File " + + filename + " failed..." ); + return false; } catch ( Exception e ) { e.printStackTrace(); return false; + } finally { + try { + file.close(); + } catch ( IOException e ) { + } } return true; } -- cgit v1.2.3-55-g7522