@Test public void testReadWrite() throws Exception { final TransferStatus status = new TransferStatus(); final byte[] content = "test".getBytes(StandardCharsets.UTF_8); status.setLength(content.length); final Path test = new Path(new FTPWorkdirService(session).find(), UUID.randomUUID().toString(), EnumSet.of(Path.Type.file)); final OutputStream out = new FTPWriteFeature(session).write(test, status, new DisabledConnectionCallback()); assertNotNull(out); new StreamCopier(new TransferStatus(), new TransferStatus()).transfer(new ByteArrayInputStream(content), out); out.close(); assertTrue(session.getFeature(Find.class).find(test)); assertEquals(content.length, new FTPListService(session, null, TimeZone.getDefault()).list(test.getParent(), new DisabledListProgressListener()).get(test).attributes().getSize()); assertEquals(content.length, new FTPWriteFeature(session).append(test, status.getLength(), PathCache.empty()).size, 0L); { final InputStream in = new FTPReadFeature(session).read(test, new TransferStatus().length(content.length), new DisabledConnectionCallback()); final ByteArrayOutputStream buffer = new ByteArrayOutputStream(content.length); new StreamCopier(status, status).transfer(in, buffer); in.close(); assertArrayEquals(content, buffer.toByteArray()); } { final byte[] buffer = new byte[content.length - 1]; final InputStream in = new FTPReadFeature(session).read(test, new TransferStatus().length(content.length).append(true).skip(1L), new DisabledConnectionCallback()); IOUtils.readFully(in, buffer); in.close(); final byte[] reference = new byte[content.length - 1]; System.arraycopy(content, 1, reference, 0, content.length - 1); assertArrayEquals(reference, buffer); } 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();  }