com.google.api.client.http.AbstractInputStreamContent

Here are the examples of the java api class com.google.api.client.http.AbstractInputStreamContent taken from open source projects.

1. PublishListingMojo#uploadSingleGraphic()

View license
private void uploadSingleGraphic(File dir, String locale, String imageType) throws MojoExecutionException, MojoFailureException {
    AbstractInputStreamContent contentGraphic = getImageAsStream(dir, imageType);
    if (contentGraphic == null) {
        return;
    }
    AndroidPublisher.Edits.Images images = edits.images();
    try {
        getLog().info("Deleting the old " + imageType);
        // Delete current image in play store
        images.deleteall(packageName, editId, locale, imageType).execute();
        getLog().info("Uploading the " + imageType);
        // After that upload the new image
        images.upload(packageName, editId, locale, imageType, contentGraphic).execute();
    } catch (IOException e) {
        getLog().error(e.getMessage(), e);
        throw new MojoExecutionException(e.getMessage(), e);
    }
}

2. MediaHttpUploader#setContentAndHeadersOnCurrentRequest()

View license
/**
   * Sets the HTTP media content chunk and the required headers that should be used in the upload
   * request.
   */
private void setContentAndHeadersOnCurrentRequest() throws IOException {
    int blockSize;
    if (isMediaLengthKnown()) {
        // We know exactly what the blockSize will be because we know the media content length.
        blockSize = (int) Math.min(chunkSize, getMediaContentLength() - totalBytesServerReceived);
    } else {
        // Use the chunkSize as the blockSize because we do know what what it is yet.
        blockSize = chunkSize;
    }
    AbstractInputStreamContent contentChunk;
    int actualBlockSize = blockSize;
    if (isMediaLengthKnown()) {
        // Mark the current position in case we need to retry the request.
        contentInputStream.mark(blockSize);
        InputStream limitInputStream = ByteStreams.limit(contentInputStream, blockSize);
        contentChunk = new InputStreamContent(mediaContent.getType(), limitInputStream).setRetrySupported(true).setLength(blockSize).setCloseInputStream(false);
        mediaContentLengthStr = String.valueOf(getMediaContentLength());
    } else {
        // If the media content length is not known we implement a custom buffered input stream that
        // enables us to detect the length of the media content when the last chunk is sent. We
        // accomplish this by always trying to read an extra byte further than the end of the current
        // chunk.
        int actualBytesRead;
        int bytesAllowedToRead;
        // amount of bytes which need to be copied from last chunk buffer
        int copyBytes = 0;
        if (currentRequestContentBuffer == null) {
            bytesAllowedToRead = cachedByte == null ? blockSize + 1 : blockSize;
            currentRequestContentBuffer = new byte[blockSize + 1];
            if (cachedByte != null) {
                currentRequestContentBuffer[0] = cachedByte;
            }
        } else {
            // currentRequestContentBuffer is not null that means one of the following:
            // 1. This is a request to recover from a server error (e.g. 503)
            // or
            // 2. The server received less bytes than the amount of bytes the client had sent. For
            // example, the client sends bytes 100-199, but the server returns back status code 308,
            // and its "Range" header is "bytes=0-150".
            // In that case, the new request will be constructed from the previous request's byte buffer
            // plus new bytes from the stream.
            copyBytes = (int) (totalBytesClientSent - totalBytesServerReceived);
            // shift copyBytes bytes to the beginning - those are the bytes which weren't received by
            // the server in the last chunk.
            System.arraycopy(currentRequestContentBuffer, currentChunkLength - copyBytes, currentRequestContentBuffer, 0, copyBytes);
            if (cachedByte != null) {
                // add the last cached byte to the buffer
                currentRequestContentBuffer[copyBytes] = cachedByte;
            }
            bytesAllowedToRead = blockSize - copyBytes;
        }
        actualBytesRead = ByteStreams.read(contentInputStream, currentRequestContentBuffer, blockSize + 1 - bytesAllowedToRead, bytesAllowedToRead);
        if (actualBytesRead < bytesAllowedToRead) {
            actualBlockSize = copyBytes + Math.max(0, actualBytesRead);
            if (cachedByte != null) {
                actualBlockSize++;
                cachedByte = null;
            }
            if (mediaContentLengthStr.equals("*")) {
                // At this point we know we reached the media content length because we either read less
                // than the specified chunk size or there is no more data left to be read.
                mediaContentLengthStr = String.valueOf(totalBytesServerReceived + actualBlockSize);
            }
        } else {
            cachedByte = currentRequestContentBuffer[blockSize];
        }
        contentChunk = new ByteArrayContent(mediaContent.getType(), currentRequestContentBuffer, 0, actualBlockSize);
        totalBytesClientSent = totalBytesServerReceived + actualBlockSize;
    }
    currentChunkLength = actualBlockSize;
    currentRequest.setContent(contentChunk);
    if (actualBlockSize == 0) {
        // No bytes to upload. Either zero content media being uploaded, or a server failure on the
        // last write, even though the write actually succeeded. Either way,
        // mediaContentLengthStr will contain the actual media length.
        currentRequest.getHeaders().setContentRange("bytes */" + mediaContentLengthStr);
    } else {
        currentRequest.getHeaders().setContentRange("bytes " + totalBytesServerReceived + "-" + (totalBytesServerReceived + actualBlockSize - 1) + "/" + mediaContentLengthStr);
    }
}