Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(191)

Side by Side Diff: components/cronet/android/java/src/org/chromium/net/urlconnection/CronetBufferedOutputStream.java

Issue 966743003: [Cronet] Implement getOutputStream in CronetHttpURLConnection (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@chunked_support
Patch Set: Use large data in buffered case too Created 5 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 package org.chromium.net.urlconnection;
6
7 import org.chromium.net.UploadDataProvider;
8 import org.chromium.net.UploadDataSink;
9
10 import java.io.ByteArrayOutputStream;
11 import java.io.IOException;
12 import java.io.OutputStream;
13 import java.nio.ByteBuffer;
14
15 /**
16 * An implementation of {@link java.io.OutputStream} that buffers entire request
17 * body in memory. This is used when neither
18 * {@link CronetHttpURLConnection#setFixedLengthStreamingMode}
19 * or {@link CronetHttpURLConnection#setChunkedStreamingMode} is set.
20 */
21 final class CronetBufferedOutputStream extends OutputStream
22 implements UploadDataProvider {
23 private final int mInitialContentLength;
24 private final CronetHttpURLConnection mConnection;
25 // Internal buffer that is used to buffer the request body.
26 private final ByteArrayOutputStream mBuffer;
27 // Number of bytes consumed by the native UploadDataStream.
28 private int mBytesConsumed;
29
30 /**
31 * Packaged protected constructor.
mef 2015/03/10 16:48:22 nit: Package protected constructor.
xunjieli 2015/03/12 21:55:09 Done.
32 * @param connection The CronetHttpURLConnection object.
33 * @param contentLength The content length of the request body. If content
34 * length cannot be determined in advance, use -1.
35 */
36 CronetBufferedOutputStream(final CronetHttpURLConnection connection,
37 final long contentLength) {
38 if (connection == null) {
39 throw new NullPointerException();
40 }
41
42 if (contentLength > Integer.MAX_VALUE) {
mef 2015/03/10 16:48:22 what if it is negative, but not -1?
xunjieli 2015/03/12 21:55:09 Done. Right, we should handle that case. To make a
43 throw new IllegalStateException("Use setFixedLengthStreamingMode()"
44 + " or setChunkedStreamingMode() for requests larger than 2GB.") ;
45 }
46 mConnection = connection;
47 mInitialContentLength = (int) contentLength;
48 if (mInitialContentLength == -1) {
49 // Bufferring without knowing content-length.
50 mBuffer = new ByteArrayOutputStream();
51 } else {
52 mBuffer = new ByteArrayOutputStream(mInitialContentLength);
53 }
54 mBytesConsumed = 0;
55 }
56
57 @Override
58 public void write(int oneByte) throws IOException {
59 mBuffer.write((byte) oneByte);
60 if (mBuffer.size() == mInitialContentLength) {
mef 2015/03/10 16:48:22 what happens if app will write more than expected?
xunjieli 2015/03/12 21:55:09 Done. Good catch, it should fail with a java.net.P
61 // Entire post data has been received. Now start the request.
62 mConnection.connect();
63 }
64 }
65
66 @Override
67 public void write(byte[] buffer, int offset, int count) throws IOException {
68 mBuffer.write(buffer, offset, count);
69 if (mInitialContentLength != -1 && mBuffer.size() > mInitialContentLengt h) {
70 throw new IllegalStateException("Writing out of bound.");
71 }
72 if (mBuffer.size() == mInitialContentLength) {
mef 2015/03/10 16:48:22 When does it connect() if mInitialContentLength ==
xunjieli 2015/03/12 21:55:09 The embedder calls connect() or any function that
73 // Entire post data has been received. Now start the request.
74 mConnection.connect();
75 }
76 }
77
78 @Override
mef 2015/03/10 16:48:22 nit: Add comment that these methods are implementa
xunjieli 2015/03/12 21:55:09 Done.
79 public long getLength() {
80 // This method is supposed to be called just before starting the request .
81 // If content length is not initially passed in, the number of bytes
82 // written will be used as the content length.
83 if (mInitialContentLength < 0) {
84 return mBuffer.size();
85 }
86 return mInitialContentLength;
87 }
88
89 @Override
90 public void read(UploadDataSink uploadDataSink, ByteBuffer byteBuffer) {
91 int toConsume = Math.min(byteBuffer.remaining(),
92 mBuffer.size() - mBytesConsumed);
93 if (toConsume > 0) {
94 byteBuffer.put(mBuffer.toByteArray(), mBytesConsumed, toConsume);
95 }
96 mBytesConsumed += toConsume;
97 uploadDataSink.onReadSucceeded(false);
98 }
99
100 @Override
101 public void rewind(UploadDataSink uploadDataSink) {
102 mBytesConsumed = 0;
103 uploadDataSink.onRewindSucceeded();
104 }
105 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698