@Test public void testReadRange() throws Exception { final Path bucket = new Path("test-cyberduck", EnumSet.of(Path.Type.directory, Path.Type.volume)); final Path test = new Path(bucket, UUID.randomUUID().toString(), EnumSet.of(Path.Type.file)); final B2FileidProvider fileid = new B2FileidProvider(session).withCache(cache); new B2TouchFeature(session, fileid).touch(test, new TransferStatus()); final Local local = new Local(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString()); final byte[] content = RandomUtils.nextBytes(1000); final OutputStream out = local.getOutputStream(false); assertNotNull(out); IOUtils.write(content, out); out.close(); new B2SingleUploadService(new B2WriteFeature(session, fileid)).upload( test, local, new BandwidthThrottle(BandwidthThrottle.UNLIMITED), new DisabledStreamListener(), new TransferStatus().length(content.length), new DisabledConnectionCallback()); final TransferStatus status = new TransferStatus(); status.setLength(content.length); status.setAppend(true); status.setOffset(100L); final InputStream in = new B2ReadFeature(session, fileid).read(test, status.length(content.length - 100), new DisabledConnectionCallback()); assertNotNull(in); final ByteArrayOutputStream buffer = new ByteArrayOutputStream(content.length - 100); new StreamCopier(status, status).transfer(in, buffer); final byte[] reference = new byte[content.length - 100]; System.arraycopy(content, 100, reference, 0, content.length - 100); assertArrayEquals(reference, buffer.toByteArray()); in.close(); new B2DeleteFeature(session, fileid).delete(Collections.singletonList(test), new DisabledLoginCallback(), new Delete.DisabledCallback()); }
@Override public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { try { if(file.getType().contains(Path.Type.upload)) { return new NullInputStream(0L); } if(status.isAppend()) { final HttpRange range = HttpRange.withStatus(status); return session.getClient().downloadFileRangeByIdToStream( fileid.getFileid(file, new DisabledListProgressListener()), range.getStart(), range.getEnd() ); } final B2DownloadFileResponse response = session.getClient().downloadFileById(fileid.getFileid(file, new DisabledListProgressListener())); return new HttpMethodReleaseInputStream(response.getResponse()); } catch(B2ApiException e) { if(StringUtils.equals("file_state_none", e.getMessage())) { return new NullInputStream(0L); } throw new B2ExceptionMappingService().map("Download {0} failed", e, file); } catch(IOException e) { throw new DefaultIOExceptionMappingService().map("Download {0} failed", e, file); } }
B2ReadFeature implements Read { @Override public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { try { if(file.getType().contains(Path.Type.upload)) { return new NullInputStream(0L); } if(status.isAppend()) { final HttpRange range = HttpRange.withStatus(status); return session.getClient().downloadFileRangeByIdToStream( fileid.getFileid(file, new DisabledListProgressListener()), range.getStart(), range.getEnd() ); } final B2DownloadFileResponse response = session.getClient().downloadFileById(fileid.getFileid(file, new DisabledListProgressListener())); return new HttpMethodReleaseInputStream(response.getResponse()); } catch(B2ApiException e) { if(StringUtils.equals("file_state_none", e.getMessage())) { return new NullInputStream(0L); } throw new B2ExceptionMappingService().map("Download {0} failed", e, file); } catch(IOException e) { throw new DefaultIOExceptionMappingService().map("Download {0} failed", e, file); } } }
B2ReadFeature implements Read { @Override public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { try { if(file.getType().contains(Path.Type.upload)) { return new NullInputStream(0L); } if(status.isAppend()) { final HttpRange range = HttpRange.withStatus(status); return session.getClient().downloadFileRangeByIdToStream( fileid.getFileid(file, new DisabledListProgressListener()), range.getStart(), range.getEnd() ); } final B2DownloadFileResponse response = session.getClient().downloadFileById(fileid.getFileid(file, new DisabledListProgressListener())); return new HttpMethodReleaseInputStream(response.getResponse()); } catch(B2ApiException e) { if(StringUtils.equals("file_state_none", e.getMessage())) { return new NullInputStream(0L); } throw new B2ExceptionMappingService().map("Download {0} failed", e, file); } catch(IOException e) { throw new DefaultIOExceptionMappingService().map("Download {0} failed", e, file); } } B2ReadFeature(final B2Session session, final B2FileidProvider fileid); }
B2ReadFeature implements Read { @Override public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { try { if(file.getType().contains(Path.Type.upload)) { return new NullInputStream(0L); } if(status.isAppend()) { final HttpRange range = HttpRange.withStatus(status); return session.getClient().downloadFileRangeByIdToStream( fileid.getFileid(file, new DisabledListProgressListener()), range.getStart(), range.getEnd() ); } final B2DownloadFileResponse response = session.getClient().downloadFileById(fileid.getFileid(file, new DisabledListProgressListener())); return new HttpMethodReleaseInputStream(response.getResponse()); } catch(B2ApiException e) { if(StringUtils.equals("file_state_none", e.getMessage())) { return new NullInputStream(0L); } throw new B2ExceptionMappingService().map("Download {0} failed", e, file); } catch(IOException e) { throw new DefaultIOExceptionMappingService().map("Download {0} failed", e, file); } } B2ReadFeature(final B2Session session, final B2FileidProvider fileid); @Override InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback); @Override boolean offset(final Path file); }
B2ReadFeature implements Read { @Override public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { try { if(file.getType().contains(Path.Type.upload)) { return new NullInputStream(0L); } if(status.isAppend()) { final HttpRange range = HttpRange.withStatus(status); return session.getClient().downloadFileRangeByIdToStream( fileid.getFileid(file, new DisabledListProgressListener()), range.getStart(), range.getEnd() ); } final B2DownloadFileResponse response = session.getClient().downloadFileById(fileid.getFileid(file, new DisabledListProgressListener())); return new HttpMethodReleaseInputStream(response.getResponse()); } catch(B2ApiException e) { if(StringUtils.equals("file_state_none", e.getMessage())) { return new NullInputStream(0L); } throw new B2ExceptionMappingService().map("Download {0} failed", e, file); } catch(IOException e) { throw new DefaultIOExceptionMappingService().map("Download {0} failed", e, file); } } B2ReadFeature(final B2Session session, final B2FileidProvider fileid); @Override InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback); @Override boolean offset(final Path file);  }