@Test public void testReadRange() throws Exception { final Path test = new Path(new DefaultHomeFinderService(session).find(), UUID.randomUUID().toString(), EnumSet.of(Path.Type.file)); session.getFeature(Touch.class).touch(test, new TransferStatus()); final Local local = new Local(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString()); final byte[] content = new RandomStringGenerator.Builder().build().generate(1000).getBytes(); final OutputStream out = local.getOutputStream(false); assertNotNull(out); IOUtils.write(content, out); out.close(); new DAVUploadFeature(new DAVWriteFeature(session)).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 DAVReadFeature(session).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 DAVDeleteFeature(session).delete(Collections.<Path>singletonList(test), new DisabledLoginCallback(), new Delete.DisabledCallback()); session.close(); }
@Override public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { final List<Header> headers = new ArrayList<Header>(this.headers()); if(status.isAppend()) { final HttpRange range = HttpRange.withStatus(status); final String header; if(-1 == range.getEnd()) { header = String.format("bytes=%d-", range.getStart()); } else { header = String.format("bytes=%d-%d", range.getStart(), range.getEnd()); } if(log.isDebugEnabled()) { log.debug(String.format("Add range header %s for file %s", header, file)); } headers.add(new BasicHeader(HttpHeaders.RANGE, header)); headers.add(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, "identity")); } try { final StringBuilder resource = new StringBuilder(new DAVPathEncoder().encode(file)); if(!status.getParameters().isEmpty()) { resource.append("?"); } for(Map.Entry<String, String> parameter : status.getParameters().entrySet()) { if(!resource.toString().endsWith("?")) { resource.append("&"); } resource.append(URIEncoder.encode(parameter.getKey())) .append("=") .append(URIEncoder.encode(parameter.getValue())); } final ContentLengthStatusInputStream stream = session.getClient().get(resource.toString(), headers); if(status.isAppend()) { if(stream.getCode() == HttpStatus.SC_OK) { log.warn(String.format("Range header not supported. Skipping %d bytes in file %s.", status.getOffset(), file)); stream.skip(status.getOffset()); } } return stream; } catch(SardineException e) { throw new DAVExceptionMappingService().map("Download {0} failed", e, file); } catch(IOException e) { throw new HttpExceptionMappingService().map("Download {0} failed", e, file); } }
DAVReadFeature implements Read { @Override public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { final List<Header> headers = new ArrayList<Header>(this.headers()); if(status.isAppend()) { final HttpRange range = HttpRange.withStatus(status); final String header; if(-1 == range.getEnd()) { header = String.format("bytes=%d-", range.getStart()); } else { header = String.format("bytes=%d-%d", range.getStart(), range.getEnd()); } if(log.isDebugEnabled()) { log.debug(String.format("Add range header %s for file %s", header, file)); } headers.add(new BasicHeader(HttpHeaders.RANGE, header)); headers.add(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, "identity")); } try { final StringBuilder resource = new StringBuilder(new DAVPathEncoder().encode(file)); if(!status.getParameters().isEmpty()) { resource.append("?"); } for(Map.Entry<String, String> parameter : status.getParameters().entrySet()) { if(!resource.toString().endsWith("?")) { resource.append("&"); } resource.append(URIEncoder.encode(parameter.getKey())) .append("=") .append(URIEncoder.encode(parameter.getValue())); } final ContentLengthStatusInputStream stream = session.getClient().get(resource.toString(), headers); if(status.isAppend()) { if(stream.getCode() == HttpStatus.SC_OK) { log.warn(String.format("Range header not supported. Skipping %d bytes in file %s.", status.getOffset(), file)); stream.skip(status.getOffset()); } } return stream; } catch(SardineException e) { throw new DAVExceptionMappingService().map("Download {0} failed", e, file); } catch(IOException e) { throw new HttpExceptionMappingService().map("Download {0} failed", e, file); } } }
DAVReadFeature implements Read { @Override public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { final List<Header> headers = new ArrayList<Header>(this.headers()); if(status.isAppend()) { final HttpRange range = HttpRange.withStatus(status); final String header; if(-1 == range.getEnd()) { header = String.format("bytes=%d-", range.getStart()); } else { header = String.format("bytes=%d-%d", range.getStart(), range.getEnd()); } if(log.isDebugEnabled()) { log.debug(String.format("Add range header %s for file %s", header, file)); } headers.add(new BasicHeader(HttpHeaders.RANGE, header)); headers.add(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, "identity")); } try { final StringBuilder resource = new StringBuilder(new DAVPathEncoder().encode(file)); if(!status.getParameters().isEmpty()) { resource.append("?"); } for(Map.Entry<String, String> parameter : status.getParameters().entrySet()) { if(!resource.toString().endsWith("?")) { resource.append("&"); } resource.append(URIEncoder.encode(parameter.getKey())) .append("=") .append(URIEncoder.encode(parameter.getValue())); } final ContentLengthStatusInputStream stream = session.getClient().get(resource.toString(), headers); if(status.isAppend()) { if(stream.getCode() == HttpStatus.SC_OK) { log.warn(String.format("Range header not supported. Skipping %d bytes in file %s.", status.getOffset(), file)); stream.skip(status.getOffset()); } } return stream; } catch(SardineException e) { throw new DAVExceptionMappingService().map("Download {0} failed", e, file); } catch(IOException e) { throw new HttpExceptionMappingService().map("Download {0} failed", e, file); } } DAVReadFeature(final DAVSession session); }
DAVReadFeature implements Read { @Override public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { final List<Header> headers = new ArrayList<Header>(this.headers()); if(status.isAppend()) { final HttpRange range = HttpRange.withStatus(status); final String header; if(-1 == range.getEnd()) { header = String.format("bytes=%d-", range.getStart()); } else { header = String.format("bytes=%d-%d", range.getStart(), range.getEnd()); } if(log.isDebugEnabled()) { log.debug(String.format("Add range header %s for file %s", header, file)); } headers.add(new BasicHeader(HttpHeaders.RANGE, header)); headers.add(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, "identity")); } try { final StringBuilder resource = new StringBuilder(new DAVPathEncoder().encode(file)); if(!status.getParameters().isEmpty()) { resource.append("?"); } for(Map.Entry<String, String> parameter : status.getParameters().entrySet()) { if(!resource.toString().endsWith("?")) { resource.append("&"); } resource.append(URIEncoder.encode(parameter.getKey())) .append("=") .append(URIEncoder.encode(parameter.getValue())); } final ContentLengthStatusInputStream stream = session.getClient().get(resource.toString(), headers); if(status.isAppend()) { if(stream.getCode() == HttpStatus.SC_OK) { log.warn(String.format("Range header not supported. Skipping %d bytes in file %s.", status.getOffset(), file)); stream.skip(status.getOffset()); } } return stream; } catch(SardineException e) { throw new DAVExceptionMappingService().map("Download {0} failed", e, file); } catch(IOException e) { throw new HttpExceptionMappingService().map("Download {0} failed", e, file); } } DAVReadFeature(final DAVSession session); @Override InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback); @Override boolean offset(final Path file); Set<Header> headers(); }
DAVReadFeature implements Read { @Override public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { final List<Header> headers = new ArrayList<Header>(this.headers()); if(status.isAppend()) { final HttpRange range = HttpRange.withStatus(status); final String header; if(-1 == range.getEnd()) { header = String.format("bytes=%d-", range.getStart()); } else { header = String.format("bytes=%d-%d", range.getStart(), range.getEnd()); } if(log.isDebugEnabled()) { log.debug(String.format("Add range header %s for file %s", header, file)); } headers.add(new BasicHeader(HttpHeaders.RANGE, header)); headers.add(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, "identity")); } try { final StringBuilder resource = new StringBuilder(new DAVPathEncoder().encode(file)); if(!status.getParameters().isEmpty()) { resource.append("?"); } for(Map.Entry<String, String> parameter : status.getParameters().entrySet()) { if(!resource.toString().endsWith("?")) { resource.append("&"); } resource.append(URIEncoder.encode(parameter.getKey())) .append("=") .append(URIEncoder.encode(parameter.getValue())); } final ContentLengthStatusInputStream stream = session.getClient().get(resource.toString(), headers); if(status.isAppend()) { if(stream.getCode() == HttpStatus.SC_OK) { log.warn(String.format("Range header not supported. Skipping %d bytes in file %s.", status.getOffset(), file)); stream.skip(status.getOffset()); } } return stream; } catch(SardineException e) { throw new DAVExceptionMappingService().map("Download {0} failed", e, file); } catch(IOException e) { throw new HttpExceptionMappingService().map("Download {0} failed", e, file); } } DAVReadFeature(final DAVSession session); @Override InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback); @Override boolean offset(final Path file); Set<Header> headers();  }