getObject.setRequestHeaders(httpHeaders);
/** * Retrieve part of the file. * * Example thing you may want to do with the result: * String str = new String( Arrays.copyOfRange(out.toByteArray() ); * * @throws IOException */ public ByteArrayOutputStream getPartialObjectData(String bucket, String fname, long start, long endIncl, @Nullable ByteArrayOutputStream optionalOldOutputToReuse) throws IOException { ByteArrayOutputStream out; if (null == optionalOldOutputToReuse) { out = new ByteArrayOutputStream((int) (endIncl - start + 1)); } else { out = optionalOldOutputToReuse; out.reset(); } Storage.Objects.Get getObject = storage.objects().get(bucket, fname); getObject.setRequestHeaders(new HttpHeaders().setRange( String.format("bytes=%d-%d", start, endIncl))); getObject.getMediaHttpDownloader().setDirectDownloadEnabled(!IS_APP_ENGINE); getObject.executeMediaAndDownloadTo(out); if (out.size() != (endIncl - start + 1)) { String err = "getPartialObjectData failed! Expected " + (endIncl - start + 1) + " bytes, got " + out.size(); LOGGER.log(Level.WARNING, err); throw new IOException(err); } return out; }
/** * Retrieve part of the file. * * Example thing you may want to do with the result: * String str = new String( Arrays.copyOfRange(out.toByteArray() ); * * @throws IOException */ public ByteArrayOutputStream getPartialObjectData(String bucket, String fname, long start, long endIncl, @Nullable ByteArrayOutputStream optionalOldOutputToReuse) throws IOException { ByteArrayOutputStream out; if (null == optionalOldOutputToReuse) { out = new ByteArrayOutputStream((int) (endIncl - start + 1)); } else { out = optionalOldOutputToReuse; out.reset(); } Storage.Objects.Get getObject = storage.objects().get(bucket, fname); getObject.setRequestHeaders(new HttpHeaders().setRange( String.format("bytes=%d-%d", start, endIncl))); getObject.getMediaHttpDownloader().setDirectDownloadEnabled(!IS_APP_ENGINE); getObject.executeMediaAndDownloadTo(out); if (out.size() != (endIncl - start + 1)) { String err = "getPartialObjectData failed! Expected " + (endIncl - start + 1) + " bytes, got " + out.size(); LOGGER.log(Level.WARNING, err); throw new IOException(err); } return out; }