@Test public void testReadRange() throws Exception { final LocalSession session = new LocalSession(new Host(new LocalProtocol(), new LocalProtocol().getDefaultHostname())); session.open(Proxy.DIRECT, new DisabledHostKeyCallback(), new DisabledLoginCallback()); session.login(Proxy.DIRECT, new DisabledLoginCallback(), new DisabledCancelCallback()); final Path workdir = new LocalHomeFinderFeature(session).find(); final Path test = new Path(workdir, UUID.randomUUID().toString(), EnumSet.of(Path.Type.file)); new LocalTouchFeature(session).touch(test, new TransferStatus()); final int length = 1048576; final byte[] content = RandomUtils.nextBytes(length); { final TransferStatus status = new TransferStatus().length(content.length).exists(true); final OutputStream out = new LocalWriteFeature(session).write(test, status, new DisabledConnectionCallback()); assertNotNull(out); new StreamCopier(status, status).withLimit(new Long(content.length)).transfer(new ByteArrayInputStream(content), out); out.close(); } { final TransferStatus status = new TransferStatus(); status.setLength(content.length); status.setAppend(true); status.setOffset(100L); final InputStream in = new LocalReadFeature(session).read(test, status, new DisabledConnectionCallback()); assertNotNull(in); final ByteArrayOutputStream buffer = new ByteArrayOutputStream(content.length - 100); new StreamCopier(status, status).withLimit(new Long(content.length - 100)).transfer(in, buffer); in.close(); final byte[] reference = new byte[content.length - 100]; System.arraycopy(content, 100, reference, 0, content.length - 100); assertArrayEquals(reference, buffer.toByteArray()); } new LocalDeleteFeature(session).delete(Collections.singletonList(test), new DisabledLoginCallback(), new Delete.DisabledCallback()); session.close(); }
@Override public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { try { final FileChannel channel = FileChannel.open(session.toPath(file), StandardOpenOption.READ); channel.position(status.getOffset()); return Channels.newInputStream(channel); } catch(IOException e) { throw new LocalExceptionMappingService().map("Download {0} failed", e, file); } }
LocalReadFeature implements Read { @Override public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { try { final FileChannel channel = FileChannel.open(session.toPath(file), StandardOpenOption.READ); channel.position(status.getOffset()); return Channels.newInputStream(channel); } catch(IOException e) { throw new LocalExceptionMappingService().map("Download {0} failed", e, file); } } }
LocalReadFeature implements Read { @Override public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { try { final FileChannel channel = FileChannel.open(session.toPath(file), StandardOpenOption.READ); channel.position(status.getOffset()); return Channels.newInputStream(channel); } catch(IOException e) { throw new LocalExceptionMappingService().map("Download {0} failed", e, file); } } LocalReadFeature(final LocalSession session); }
LocalReadFeature implements Read { @Override public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { try { final FileChannel channel = FileChannel.open(session.toPath(file), StandardOpenOption.READ); channel.position(status.getOffset()); return Channels.newInputStream(channel); } catch(IOException e) { throw new LocalExceptionMappingService().map("Download {0} failed", e, file); } } LocalReadFeature(final LocalSession session); @Override InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback); @Override boolean offset(final Path file); }
LocalReadFeature implements Read { @Override public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { try { final FileChannel channel = FileChannel.open(session.toPath(file), StandardOpenOption.READ); channel.position(status.getOffset()); return Channels.newInputStream(channel); } catch(IOException e) { throw new LocalExceptionMappingService().map("Download {0} failed", e, file); } } LocalReadFeature(final LocalSession session); @Override InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback); @Override boolean offset(final Path file);  }