buffer.clear(); setStatus(ReplyWorker.FINISHED); channel.close(); getSocketProvider().close();
buffer.clear();
    public void run () {        if ( getCommand() == null )            throw new IllegalArgumentException("Given command is null!");        if ( getSocketProvider() == null )            throw new IllegalArgumentException("Given connection is closed already!");                if ( getCommand() instanceof ListCommand ) {//       TODO: think about a own solution for reading LIST//       instead of using control connection message read            this.setReply(ReplyWorker.readReply(getSocketProvider()));            setStatus( ReplyWorker.FINISHED );        }else if ( getCommand() instanceof RetrieveCommand ) {            RetrieveCommand retrieveCommand = (RetrieveCommand) getCommand();                        if ( retrieveCommand.getFromFile().getType().intern() == Command.TYPE_I ) {                try                {                    FileOutputStream out = new FileOutputStream(retrieveCommand.getToFile());                    FileChannel channel = out.getChannel();                    int amount;                    try {                        while ((amount = getSocketProvider().read(buffer)) != -1)                         {                            if (amount == 0)                             {                                try {                                   Thread.sleep(4);                                } catch (InterruptedException e) {}                            }                             if (buffer.remaining() > 0) {                                continue;                            }                            buffer.flip();                            channel.write(buffer);                            buffer.clear();                        }                        buffer.flip();                        channel.write(buffer);                        buffer.clear();                        setStatus(ReplyWorker.FINISHED);                        channel.close();                        getSocketProvider().close();                    } catch (IOException ioe) {                        setCaughtException(ioe);                        setStatus(ReplyWorker.ERROR_IO_EXCEPTION);                    }                                   }catch (FileNotFoundException fnfe)                {                    setCaughtException(fnfe);                    setStatus(ReplyWorker.ERROR_FILE_NOT_FOUND);                }            }else                throw new IllegalArgumentException("Unknown file transfer type for download!");                                            }else if ( getCommand() instanceof StoreCommand ) {            StoreCommand storeCommand = (StoreCommand) getCommand();                        if ( storeCommand.getToFile().getType().intern() == Command.TYPE_I )            {                try                {                	log.debug("Upload file: " + storeCommand.getFromFile().toString());                    FileInputStream in = new FileInputStream(storeCommand.getFromFile());                    FileChannel channel = in.getChannel();                    int amount;                    int socketWrite;                    int socketAmount = 0;                    try {                        while ((amount = channel.read(buffer)) != -1) {                            buffer.flip();                            socketWrite = 0;                            while ((socketWrite = getSocketProvider().write(buffer)) != -1)                             {                                socketAmount += socketWrite;                                if (amount <= socketAmount) {                                    break;                                }                                if (socketWrite == 0)                                 {                                   try {                                         Thread.sleep(4);                                   } catch (InterruptedException e) {}                                }                            }                            if (socketWrite == -1) {                                break;                            }                            socketAmount = 0;                            buffer.clear();                            setStatus(ReplyWorker.FINISHED);                            channel.close();                            getSocketProvider().close();                        }                    } catch (IOException ioe) {                        setCaughtException(ioe);                        setStatus(ReplyWorker.ERROR_IO_EXCEPTION);                    }                                    }catch (FileNotFoundException fnfe)                {                    setCaughtException(fnfe);                    setStatus(ReplyWorker.ERROR_FILE_NOT_FOUND);                }            }            else                throw new IllegalArgumentException("Unknown file transfer type for upload!");                    }else            throw new IllegalArgumentException("Given command is not supported!");    }