List of usage examples for com.google.common.io LimitInputStream LimitInputStream
public LimitInputStream(InputStream paramInputStream, long paramLong)
From source file:com.comphenix.protocol.compat.netty.shaded.ShadedByteBufAdapter.java
@Override public int getBytes(int index, GatheringByteChannel out, int length) throws IOException { byte[] data = ByteStreams.toByteArray(new LimitInputStream(input, length)); out.write(ByteBuffer.wrap(data)); return data.length; }
From source file:com.comphenix.protocol.compat.netty.shaded.ShadedByteBufAdapter.java
@Override public int setBytes(int index, InputStream in, int length) throws IOException { LimitInputStream limit = new LimitInputStream(in, length); ByteStreams.copy(limit, output);// w w w . jav a2 s . c o m return length - limit.available(); }
From source file:com.google.api.client.googleapis.MediaHttpUploader.java
/** * Sets the HTTP media content chunk and the required headers that should be used in the upload * request./*from www .jav a 2s .com*/ * * @param bytesWritten The number of bytes that have been successfully uploaded on the server */ private void setContentAndHeadersOnCurrentRequest(long bytesWritten) throws Exception { int blockSize = (int) Math.min(chunkSize, getMediaContentLength() - bytesWritten); InputStreamContent contentChunk = new InputStreamContent(mediaContent.getType(), new LimitInputStream(contentInputStream, blockSize)); contentChunk.setCloseInputStream(false); contentChunk.setRetrySupported(true); contentChunk.setLength(blockSize); // Mark the current position in case we need to retry the request. contentInputStream.mark(blockSize); currentRequest.setContent(contentChunk); currentRequest.getHeaders().setContentRange( "bytes " + bytesWritten + "-" + (bytesWritten + blockSize - 1) + "/" + getMediaContentLength()); }
From source file:com.google.api.client.googleapis.media.MediaHttpUploader.java
/** * Sets the HTTP media content chunk and the required headers that should be used in the upload * request.//from w w w . j ava 2 s . c o m * * @param bytesWritten The number of bytes that have been successfully uploaded on the server */ private void setContentAndHeadersOnCurrentRequest(long bytesWritten) throws Exception { int blockSize = (int) Math.min(chunkSize, getMediaContentLength() - bytesWritten); // TODO(rmistry): Add tests for LimitInputStream. InputStreamContent contentChunk = new InputStreamContent(mediaContent.getType(), new LimitInputStream(contentInputStream, blockSize)); contentChunk.setCloseInputStream(false); contentChunk.setRetrySupported(true); contentChunk.setLength(blockSize); // Mark the current position in case we need to retry the request. contentInputStream.mark(blockSize); currentRequest.setContent(contentChunk); currentRequest.getHeaders().setContentRange( "bytes " + bytesWritten + "-" + (bytesWritten + blockSize - 1) + "/" + getMediaContentLength()); }
From source file:com.google.dart.compiler.backend.js.ClosureJsBackend.java
/** * @return a mutable list/* ww w. j a v a 2s . c om*/ * @throws IOException */ private static List<JSSourceFile> getDefaultExterns() throws IOException { Class<ClosureJsBackend> clazz = ClosureJsBackend.class; InputStream input = clazz.getResourceAsStream("/com/google/javascript/jscomp/externs.zip"); if (input == null) { /* * HACK - the open source version of the closure compiler maps the * resource into a different location. */ input = clazz.getResourceAsStream("/externs.zip"); } ZipInputStream zip = new ZipInputStream(input); Map<String, JSSourceFile> externsMap = Maps.newHashMap(); for (ZipEntry entry = null; (entry = zip.getNextEntry()) != null;) { InputStream entryStream = new BufferedInputStream(new LimitInputStream(zip, entry.getSize())); externsMap.put(entry.getName(), JSSourceFile.fromInputStream( // Give the files an odd prefix, so that they do not conflict // with the user's files. "externs.zip//" + entry.getName(), entryStream)); } Preconditions.checkState(externsMap.keySet().equals(Sets.newHashSet(DEFAULT_EXTERNS_NAMES)), "Externs zip must match our hard-coded list of externs."); // Order matters, so the resources must be added to the result list // in the expected order. List<JSSourceFile> externs = Lists.newArrayList(); for (String key : DEFAULT_EXTERNS_NAMES) { externs.add(externsMap.get(key)); } // Add methods used when running the unit tests. externs.add(JSSourceFile.fromCode("missingExterns", MISSING_EXTERNS)); // Add methods used when running the unit tests. externs.add(JSSourceFile.fromCode("unitTestStubs", UNIT_TEST_EXTERN_STUBS)); // Add methods used by Closure Compiler itself. externs.add(JSSourceFile.fromCode("closureCompilerPrimitives", CLOSURE_PRIMITIVES)); return externs; }
From source file:brooklyn.util.internal.ssh.SshjTool.java
private Payload toPayload(InputStream input, long length) { InputStreamPayload payload = new InputStreamPayload(new LimitInputStream(input, length)); payload.getContentMetadata().setContentLength(length); return payload; }