@Test public void testReadRangeUnknownLength() throws Exception { final Path drive = new MantaDirectoryFeature(session).mkdir(randomDirectory(), "", new TransferStatus()); final Path test = new Path(drive, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file)); new MantaTouchFeature(session).touch(test, new TransferStatus()); final Local local = new Local(System.getProperty("java.io.tmpdir"), new AlphanumericRandomStringService().random()); final byte[] content = RandomUtils.nextBytes(1000); final OutputStream out = local.getOutputStream(false); assertNotNull(out); IOUtils.write(content, out); out.close(); new DefaultUploadFeature<Void>(new MantaWriteFeature(session)).upload( test, local, new BandwidthThrottle(BandwidthThrottle.UNLIMITED), new DisabledStreamListener(), new TransferStatus().length(content.length), new DisabledConnectionCallback()); final TransferStatus status = new TransferStatus(); status.setLength(-1L); status.setAppend(true); status.setOffset(100L); final InputStream in = new MantaReadFeature(session).read(test, status, 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(); final MantaDeleteFeature delete = new MantaDeleteFeature(session); delete.delete(Collections.singletonList(test), new DisabledLoginCallback(), new Delete.DisabledCallback()); }
@Override public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback connectionCallback) throws BackgroundException { final MantaHttpHeaders headers = new MantaHttpHeaders(); try { try { if(status.isAppend()) { final HttpRange range = HttpRange.withStatus(status); headers.setByteRange(range.getStart(), range.getEnd() < 0 ? null : range.getEnd()); } return session.getClient().getAsInputStream(file.getAbsolute(), headers); } catch(UnsupportedOperationException e) { final MantaObject probablyEmptyFile = session.getClient().head(file.getAbsolute()); if(probablyEmptyFile.getContentLength() != 0) { throw new AccessDeniedException(); } return new NullInputStream(0L); } } catch(MantaException e) { throw new MantaExceptionMappingService().map("Download {0} failed", e, file); } catch(MantaClientHttpResponseException e) { throw new MantaHttpExceptionMappingService().map("Download {0} failed", e, file); } catch(IOException e) { throw new DefaultIOExceptionMappingService().map("Download {0} failed", e, file); } }
MantaReadFeature implements Read { @Override public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback connectionCallback) throws BackgroundException { final MantaHttpHeaders headers = new MantaHttpHeaders(); try { try { if(status.isAppend()) { final HttpRange range = HttpRange.withStatus(status); headers.setByteRange(range.getStart(), range.getEnd() < 0 ? null : range.getEnd()); } return session.getClient().getAsInputStream(file.getAbsolute(), headers); } catch(UnsupportedOperationException e) { final MantaObject probablyEmptyFile = session.getClient().head(file.getAbsolute()); if(probablyEmptyFile.getContentLength() != 0) { throw new AccessDeniedException(); } return new NullInputStream(0L); } } catch(MantaException e) { throw new MantaExceptionMappingService().map("Download {0} failed", e, file); } catch(MantaClientHttpResponseException e) { throw new MantaHttpExceptionMappingService().map("Download {0} failed", e, file); } catch(IOException e) { throw new DefaultIOExceptionMappingService().map("Download {0} failed", e, file); } } }
MantaReadFeature implements Read { @Override public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback connectionCallback) throws BackgroundException { final MantaHttpHeaders headers = new MantaHttpHeaders(); try { try { if(status.isAppend()) { final HttpRange range = HttpRange.withStatus(status); headers.setByteRange(range.getStart(), range.getEnd() < 0 ? null : range.getEnd()); } return session.getClient().getAsInputStream(file.getAbsolute(), headers); } catch(UnsupportedOperationException e) { final MantaObject probablyEmptyFile = session.getClient().head(file.getAbsolute()); if(probablyEmptyFile.getContentLength() != 0) { throw new AccessDeniedException(); } return new NullInputStream(0L); } } catch(MantaException e) { throw new MantaExceptionMappingService().map("Download {0} failed", e, file); } catch(MantaClientHttpResponseException e) { throw new MantaHttpExceptionMappingService().map("Download {0} failed", e, file); } catch(IOException e) { throw new DefaultIOExceptionMappingService().map("Download {0} failed", e, file); } } MantaReadFeature(final MantaSession session); }
MantaReadFeature implements Read { @Override public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback connectionCallback) throws BackgroundException { final MantaHttpHeaders headers = new MantaHttpHeaders(); try { try { if(status.isAppend()) { final HttpRange range = HttpRange.withStatus(status); headers.setByteRange(range.getStart(), range.getEnd() < 0 ? null : range.getEnd()); } return session.getClient().getAsInputStream(file.getAbsolute(), headers); } catch(UnsupportedOperationException e) { final MantaObject probablyEmptyFile = session.getClient().head(file.getAbsolute()); if(probablyEmptyFile.getContentLength() != 0) { throw new AccessDeniedException(); } return new NullInputStream(0L); } } catch(MantaException e) { throw new MantaExceptionMappingService().map("Download {0} failed", e, file); } catch(MantaClientHttpResponseException e) { throw new MantaHttpExceptionMappingService().map("Download {0} failed", e, file); } catch(IOException e) { throw new DefaultIOExceptionMappingService().map("Download {0} failed", e, file); } } MantaReadFeature(final MantaSession session); @Override InputStream read(final Path file, final TransferStatus status, final ConnectionCallback connectionCallback); @Override boolean offset(final Path file); }
MantaReadFeature implements Read { @Override public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback connectionCallback) throws BackgroundException { final MantaHttpHeaders headers = new MantaHttpHeaders(); try { try { if(status.isAppend()) { final HttpRange range = HttpRange.withStatus(status); headers.setByteRange(range.getStart(), range.getEnd() < 0 ? null : range.getEnd()); } return session.getClient().getAsInputStream(file.getAbsolute(), headers); } catch(UnsupportedOperationException e) { final MantaObject probablyEmptyFile = session.getClient().head(file.getAbsolute()); if(probablyEmptyFile.getContentLength() != 0) { throw new AccessDeniedException(); } return new NullInputStream(0L); } } catch(MantaException e) { throw new MantaExceptionMappingService().map("Download {0} failed", e, file); } catch(MantaClientHttpResponseException e) { throw new MantaHttpExceptionMappingService().map("Download {0} failed", e, file); } catch(IOException e) { throw new DefaultIOExceptionMappingService().map("Download {0} failed", e, file); } } MantaReadFeature(final MantaSession session); @Override InputStream read(final Path file, final TransferStatus status, final ConnectionCallback connectionCallback); @Override boolean offset(final Path file);  }