@Test public void testWriteContentRange() throws Exception { final FTPWriteFeature feature = new FTPWriteFeature(session); final Path test = new Path(new FTPWorkdirService(session).find(), UUID.randomUUID().toString(), EnumSet.of(Path.Type.file)); final byte[] content = RandomUtils.nextBytes(64000); { final TransferStatus status = new TransferStatus(); status.setLength(1024L); final OutputStream out = feature.write(test, status, new DisabledConnectionCallback()); new StreamCopier(status, status).withOffset(0L).withLimit(status.getLength()).transfer(new ByteArrayInputStream(content), out); out.close(); } assertTrue(new DefaultFindFeature(session).find(test)); assertEquals(1024L, new DefaultAttributesFinderFeature(session).find(test).getSize()); { final TransferStatus status = new TransferStatus(); status.setLength(content.length - 1024L); status.setOffset(1024L); status.setAppend(true); final OutputStream out = feature.write(test, status, new DisabledConnectionCallback()); new StreamCopier(status, status).withOffset(status.getOffset()).withLimit(status.getLength()).transfer(new ByteArrayInputStream(content), out); out.close(); } final ByteArrayOutputStream out = new ByteArrayOutputStream(content.length); IOUtils.copy(new FTPReadFeature(session).read(test, new TransferStatus().length(content.length), new DisabledConnectionCallback()), out); assertArrayEquals(content, out.toByteArray()); new FTPDeleteFeature(session).delete(Collections.singletonList(test), new DisabledLoginCallback(), new Delete.DisabledCallback()); }
@Override public StatusOutputStream<Integer> write(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { try { if(!session.getClient().setFileType(FTPClient.BINARY_FILE_TYPE)) { throw new FTPException(session.getClient().getReplyCode(), session.getClient().getReplyString()); } final OutputStream out = new DataConnectionActionExecutor(session).data(new DataConnectionAction<OutputStream>() { @Override public OutputStream execute() throws BackgroundException { try { if(status.isAppend()) { if(!status.isExists()) { log.warn(String.format("Allocate %d bytes for file %s", status.getOffset(), file)); session.getClient().allocate((int) status.getOffset()); } return session.getClient().appendFileStream(file.getAbsolute()); } else { return session.getClient().storeFileStream(file.getAbsolute()); } } catch(IOException e) { throw new FTPExceptionMappingService().map(e); } } }, new DisabledProgressListener()); return new ReadReplyOutputStream(out, status); } catch(IOException e) { throw new FTPExceptionMappingService().map("Upload {0} failed", e, file); } }
FTPWriteFeature extends AppendWriteFeature<Integer> { @Override public StatusOutputStream<Integer> write(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { try { if(!session.getClient().setFileType(FTPClient.BINARY_FILE_TYPE)) { throw new FTPException(session.getClient().getReplyCode(), session.getClient().getReplyString()); } final OutputStream out = new DataConnectionActionExecutor(session).data(new DataConnectionAction<OutputStream>() { @Override public OutputStream execute() throws BackgroundException { try { if(status.isAppend()) { if(!status.isExists()) { log.warn(String.format("Allocate %d bytes for file %s", status.getOffset(), file)); session.getClient().allocate((int) status.getOffset()); } return session.getClient().appendFileStream(file.getAbsolute()); } else { return session.getClient().storeFileStream(file.getAbsolute()); } } catch(IOException e) { throw new FTPExceptionMappingService().map(e); } } }, new DisabledProgressListener()); return new ReadReplyOutputStream(out, status); } catch(IOException e) { throw new FTPExceptionMappingService().map("Upload {0} failed", e, file); } } }
FTPWriteFeature extends AppendWriteFeature<Integer> { @Override public StatusOutputStream<Integer> write(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { try { if(!session.getClient().setFileType(FTPClient.BINARY_FILE_TYPE)) { throw new FTPException(session.getClient().getReplyCode(), session.getClient().getReplyString()); } final OutputStream out = new DataConnectionActionExecutor(session).data(new DataConnectionAction<OutputStream>() { @Override public OutputStream execute() throws BackgroundException { try { if(status.isAppend()) { if(!status.isExists()) { log.warn(String.format("Allocate %d bytes for file %s", status.getOffset(), file)); session.getClient().allocate((int) status.getOffset()); } return session.getClient().appendFileStream(file.getAbsolute()); } else { return session.getClient().storeFileStream(file.getAbsolute()); } } catch(IOException e) { throw new FTPExceptionMappingService().map(e); } } }, new DisabledProgressListener()); return new ReadReplyOutputStream(out, status); } catch(IOException e) { throw new FTPExceptionMappingService().map("Upload {0} failed", e, file); } } FTPWriteFeature(final FTPSession session); }
FTPWriteFeature extends AppendWriteFeature<Integer> { @Override public StatusOutputStream<Integer> write(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { try { if(!session.getClient().setFileType(FTPClient.BINARY_FILE_TYPE)) { throw new FTPException(session.getClient().getReplyCode(), session.getClient().getReplyString()); } final OutputStream out = new DataConnectionActionExecutor(session).data(new DataConnectionAction<OutputStream>() { @Override public OutputStream execute() throws BackgroundException { try { if(status.isAppend()) { if(!status.isExists()) { log.warn(String.format("Allocate %d bytes for file %s", status.getOffset(), file)); session.getClient().allocate((int) status.getOffset()); } return session.getClient().appendFileStream(file.getAbsolute()); } else { return session.getClient().storeFileStream(file.getAbsolute()); } } catch(IOException e) { throw new FTPExceptionMappingService().map(e); } } }, new DisabledProgressListener()); return new ReadReplyOutputStream(out, status); } catch(IOException e) { throw new FTPExceptionMappingService().map("Upload {0} failed", e, file); } } FTPWriteFeature(final FTPSession session); @Override StatusOutputStream<Integer> write(final Path file, final TransferStatus status, final ConnectionCallback callback); @Override boolean temporary(); @Override boolean random(); }
FTPWriteFeature extends AppendWriteFeature<Integer> { @Override public StatusOutputStream<Integer> write(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { try { if(!session.getClient().setFileType(FTPClient.BINARY_FILE_TYPE)) { throw new FTPException(session.getClient().getReplyCode(), session.getClient().getReplyString()); } final OutputStream out = new DataConnectionActionExecutor(session).data(new DataConnectionAction<OutputStream>() { @Override public OutputStream execute() throws BackgroundException { try { if(status.isAppend()) { if(!status.isExists()) { log.warn(String.format("Allocate %d bytes for file %s", status.getOffset(), file)); session.getClient().allocate((int) status.getOffset()); } return session.getClient().appendFileStream(file.getAbsolute()); } else { return session.getClient().storeFileStream(file.getAbsolute()); } } catch(IOException e) { throw new FTPExceptionMappingService().map(e); } } }, new DisabledProgressListener()); return new ReadReplyOutputStream(out, status); } catch(IOException e) { throw new FTPExceptionMappingService().map("Upload {0} failed", e, file); } } FTPWriteFeature(final FTPSession session); @Override StatusOutputStream<Integer> write(final Path file, final TransferStatus status, final ConnectionCallback callback); @Override boolean temporary(); @Override boolean random();  }