@Test public void testReadEmpty() throws Exception { final Path container = new Path("test.cyberduck.ch", EnumSet.of(Path.Type.directory, Path.Type.volume)); final Path directory = new GoogleStorageDirectoryFeature(session).mkdir(new Path(container, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.directory)), null, new TransferStatus()); final Path file = new GoogleStorageTouchFeature(session).touch(new Path(directory, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file)), new TransferStatus()); assertEquals(0, new GoogleStorageAttributesFinderFeature(session).find(file).getSize()); final CountingInputStream in = new CountingInputStream(new GoogleStorageReadFeature(session).read(file, new TransferStatus(), new DisabledConnectionCallback())); in.close(); assertEquals(0L, in.getByteCount(), 0L); new GoogleStorageDeleteFeature(session).delete(Arrays.asList(file, directory), new DisabledLoginCallback(), new Delete.DisabledCallback()); }
@Override public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { try { if(0L == status.getLength()) { return new NullInputStream(0L); } final StringBuilder uri = new StringBuilder(String.format("%sstorage/v1/b/%s/o/%s?alt=media", session.getClient().getRootUrl(), containerService.getContainer(file).getName(), GoogleStorageUriEncoder.encode(containerService.getKey(file)))); if(StringUtils.isNotBlank(file.attributes().getVersionId())) { uri.append(String.format("?generation=%s", file.attributes().getVersionId())); } final HttpUriRequest request = new HttpGet(uri.toString()); request.addHeader(HTTP.CONTENT_TYPE, MEDIA_TYPE); 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)); } request.addHeader(new BasicHeader(HttpHeaders.RANGE, header)); request.addHeader(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, "identity")); } final HttpClient client = session.getHttpClient(); final HttpResponse response = client.execute(request); switch(response.getStatusLine().getStatusCode()) { case HttpStatus.SC_OK: case HttpStatus.SC_PARTIAL_CONTENT: return new HttpMethodReleaseInputStream(response); default: throw new DefaultHttpResponseExceptionMappingService().map( new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase())); } } catch(IOException e) { throw new GoogleStorageExceptionMappingService().map("Download {0} failed", e, file); } }
GoogleStorageReadFeature implements Read { @Override public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { try { if(0L == status.getLength()) { return new NullInputStream(0L); } final StringBuilder uri = new StringBuilder(String.format("%sstorage/v1/b/%s/o/%s?alt=media", session.getClient().getRootUrl(), containerService.getContainer(file).getName(), GoogleStorageUriEncoder.encode(containerService.getKey(file)))); if(StringUtils.isNotBlank(file.attributes().getVersionId())) { uri.append(String.format("?generation=%s", file.attributes().getVersionId())); } final HttpUriRequest request = new HttpGet(uri.toString()); request.addHeader(HTTP.CONTENT_TYPE, MEDIA_TYPE); 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)); } request.addHeader(new BasicHeader(HttpHeaders.RANGE, header)); request.addHeader(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, "identity")); } final HttpClient client = session.getHttpClient(); final HttpResponse response = client.execute(request); switch(response.getStatusLine().getStatusCode()) { case HttpStatus.SC_OK: case HttpStatus.SC_PARTIAL_CONTENT: return new HttpMethodReleaseInputStream(response); default: throw new DefaultHttpResponseExceptionMappingService().map( new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase())); } } catch(IOException e) { throw new GoogleStorageExceptionMappingService().map("Download {0} failed", e, file); } } }
GoogleStorageReadFeature implements Read { @Override public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { try { if(0L == status.getLength()) { return new NullInputStream(0L); } final StringBuilder uri = new StringBuilder(String.format("%sstorage/v1/b/%s/o/%s?alt=media", session.getClient().getRootUrl(), containerService.getContainer(file).getName(), GoogleStorageUriEncoder.encode(containerService.getKey(file)))); if(StringUtils.isNotBlank(file.attributes().getVersionId())) { uri.append(String.format("?generation=%s", file.attributes().getVersionId())); } final HttpUriRequest request = new HttpGet(uri.toString()); request.addHeader(HTTP.CONTENT_TYPE, MEDIA_TYPE); 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)); } request.addHeader(new BasicHeader(HttpHeaders.RANGE, header)); request.addHeader(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, "identity")); } final HttpClient client = session.getHttpClient(); final HttpResponse response = client.execute(request); switch(response.getStatusLine().getStatusCode()) { case HttpStatus.SC_OK: case HttpStatus.SC_PARTIAL_CONTENT: return new HttpMethodReleaseInputStream(response); default: throw new DefaultHttpResponseExceptionMappingService().map( new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase())); } } catch(IOException e) { throw new GoogleStorageExceptionMappingService().map("Download {0} failed", e, file); } } GoogleStorageReadFeature(final GoogleStorageSession session); }
GoogleStorageReadFeature implements Read { @Override public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { try { if(0L == status.getLength()) { return new NullInputStream(0L); } final StringBuilder uri = new StringBuilder(String.format("%sstorage/v1/b/%s/o/%s?alt=media", session.getClient().getRootUrl(), containerService.getContainer(file).getName(), GoogleStorageUriEncoder.encode(containerService.getKey(file)))); if(StringUtils.isNotBlank(file.attributes().getVersionId())) { uri.append(String.format("?generation=%s", file.attributes().getVersionId())); } final HttpUriRequest request = new HttpGet(uri.toString()); request.addHeader(HTTP.CONTENT_TYPE, MEDIA_TYPE); 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)); } request.addHeader(new BasicHeader(HttpHeaders.RANGE, header)); request.addHeader(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, "identity")); } final HttpClient client = session.getHttpClient(); final HttpResponse response = client.execute(request); switch(response.getStatusLine().getStatusCode()) { case HttpStatus.SC_OK: case HttpStatus.SC_PARTIAL_CONTENT: return new HttpMethodReleaseInputStream(response); default: throw new DefaultHttpResponseExceptionMappingService().map( new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase())); } } catch(IOException e) { throw new GoogleStorageExceptionMappingService().map("Download {0} failed", e, file); } } GoogleStorageReadFeature(final GoogleStorageSession session); @Override boolean offset(Path file); @Override InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback); }
GoogleStorageReadFeature implements Read { @Override public InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback) throws BackgroundException { try { if(0L == status.getLength()) { return new NullInputStream(0L); } final StringBuilder uri = new StringBuilder(String.format("%sstorage/v1/b/%s/o/%s?alt=media", session.getClient().getRootUrl(), containerService.getContainer(file).getName(), GoogleStorageUriEncoder.encode(containerService.getKey(file)))); if(StringUtils.isNotBlank(file.attributes().getVersionId())) { uri.append(String.format("?generation=%s", file.attributes().getVersionId())); } final HttpUriRequest request = new HttpGet(uri.toString()); request.addHeader(HTTP.CONTENT_TYPE, MEDIA_TYPE); 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)); } request.addHeader(new BasicHeader(HttpHeaders.RANGE, header)); request.addHeader(new BasicHeader(HttpHeaders.ACCEPT_ENCODING, "identity")); } final HttpClient client = session.getHttpClient(); final HttpResponse response = client.execute(request); switch(response.getStatusLine().getStatusCode()) { case HttpStatus.SC_OK: case HttpStatus.SC_PARTIAL_CONTENT: return new HttpMethodReleaseInputStream(response); default: throw new DefaultHttpResponseExceptionMappingService().map( new HttpResponseException(response.getStatusLine().getStatusCode(), response.getStatusLine().getReasonPhrase())); } } catch(IOException e) { throw new GoogleStorageExceptionMappingService().map("Download {0} failed", e, file); } } GoogleStorageReadFeature(final GoogleStorageSession session); @Override boolean offset(Path file); @Override InputStream read(final Path file, final TransferStatus status, final ConnectionCallback callback);  }