Java Code Examples for com.google.api.client.util.StreamingContent#writeTo()

The following examples show how to use com.google.api.client.util.StreamingContent#writeTo() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: GZipEncoding.java    From google-http-java-client with Apache License 2.0 6 votes vote down vote up
public void encode(StreamingContent content, OutputStream out) throws IOException {
  // must not close the underlying output stream
  OutputStream out2 =
      new BufferedOutputStream(out) {
        @Override
        public void close() throws IOException {
          // copy implementation of super.close(), except do not close the underlying output
          // stream
          try {
            flush();
          } catch (IOException ignored) {
          }
        }
      };
  GZIPOutputStream zipper = new GZIPOutputStream(out2);
  content.writeTo(zipper);
  // cannot call just zipper.finish() because that would cause a severe memory leak
  zipper.close();
}
 
Example 2
Source File: NetHttpRequest.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
@Override
public void write(OutputStream outputStream, final StreamingContent content)
    throws IOException {
  content.writeTo(outputStream);
}
 
Example 3
Source File: MultipartContent.java    From google-http-java-client with Apache License 2.0 4 votes vote down vote up
public void writeTo(OutputStream out) throws IOException {
  Writer writer = new OutputStreamWriter(out, getCharset());
  String boundary = getBoundary();
  for (Part part : parts) {
    HttpHeaders headers = new HttpHeaders().setAcceptEncoding(null);
    if (part.headers != null) {
      headers.fromHttpHeaders(part.headers);
    }
    headers
        .setContentEncoding(null)
        .setUserAgent(null)
        .setContentType(null)
        .setContentLength(null)
        .set("Content-Transfer-Encoding", null);
    // analyze the content
    HttpContent content = part.content;
    StreamingContent streamingContent = null;
    if (content != null) {
      headers.set("Content-Transfer-Encoding", Arrays.asList("binary"));
      headers.setContentType(content.getType());
      HttpEncoding encoding = part.encoding;
      long contentLength;
      if (encoding == null) {
        contentLength = content.getLength();
        streamingContent = content;
      } else {
        headers.setContentEncoding(encoding.getName());
        streamingContent = new HttpEncodingStreamingContent(content, encoding);
        contentLength = AbstractHttpContent.computeLength(content);
      }
      if (contentLength != -1) {
        headers.setContentLength(contentLength);
      }
    }
    // write multipart-body from RFC 1521 §7.2.1
    // write encapsulation
    // write delimiter
    writer.write(TWO_DASHES);
    writer.write(boundary);
    writer.write(NEWLINE);
    // write body-part; message from RFC 822 §4.1
    // write message fields
    HttpHeaders.serializeHeadersForMultipartRequests(headers, null, null, writer);
    if (streamingContent != null) {
      writer.write(NEWLINE);
      writer.flush();
      // write message text/body
      streamingContent.writeTo(out);
    }
    // terminate encapsulation
    writer.write(NEWLINE);
  }
  // write close-delimiter
  writer.write(TWO_DASHES);
  writer.write(boundary);
  writer.write(TWO_DASHES);
  writer.write(NEWLINE);
  writer.flush();
}
 
Example 4
Source File: MultipartFormDataContent.java    From Broadsheet.ie-Android with MIT License 4 votes vote down vote up
@Override
public void writeTo(OutputStream out) throws IOException {

    Writer writer = new OutputStreamWriter(out, getCharset());
    String boundary = getBoundary();

    for (Part part : parts) {
        HttpHeaders headers = new HttpHeaders().setAcceptEncoding(null);
        if (part.headers != null) {
            headers.fromHttpHeaders(part.headers);
        }
        headers.setContentEncoding(null).setUserAgent(null).setContentType(null).setContentLength(null);
        // analyze the content
        HttpContent content = part.content;
        StreamingContent streamingContent = null;
        String contentDisposition = String.format("form-data; name=\"%s\"", part.name);
        if (part.filename != null) {
            headers.setContentType(content.getType());
            contentDisposition += String.format("; filename=\"%s\"", part.filename);
        }
        headers.set("Content-Disposition", contentDisposition);
        HttpEncoding encoding = part.encoding;
        if (encoding == null) {
            streamingContent = content;
        } else {
            headers.setContentEncoding(encoding.getName());
            streamingContent = new HttpEncodingStreamingContent(content, encoding);
        }
        // write separator
        writer.write(TWO_DASHES);
        writer.write(boundary);
        writer.write(NEWLINE);
        // write headers
        HttpHeaders.serializeHeadersForMultipartRequests(headers, null, null, writer);
        // write content
        if (streamingContent != null) {
            writer.write(NEWLINE);
            writer.flush();
            streamingContent.writeTo(out);
            writer.write(NEWLINE);
        }
    }
    // write end separator
    writer.write(TWO_DASHES);
    writer.write(boundary);
    writer.write(TWO_DASHES);
    writer.write(NEWLINE);
    writer.flush();
}