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

Side by Side Diff: components/cronet/android/api/src/org/chromium/net/ChunkedWritableByteChannel.java

Issue 2339223002: Cronet API Refactoring (Closed)
Patch Set: Javadoc + rebase Created 4 years, 3 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 2014 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;
6
7 import java.io.IOException;
8 import java.nio.ByteBuffer;
9 import java.nio.channels.ClosedChannelException;
10 import java.nio.channels.WritableByteChannel;
11 import java.util.ArrayList;
12
13 /**
14 * A writable byte channel that is optimized for chunked writing. Each call to
15 * {@link #write} results in a ByteBuffer being created and remembered. Then all
16 * of those byte buffers are combined on demand. This approach allows to avoid
17 * the cost of reallocating a byte buffer.
18 * @deprecated This is no longer used in the new async API.
19 * {@hide as it's deprecated}
20 */
21 @Deprecated
22 public class ChunkedWritableByteChannel implements WritableByteChannel {
23
24 private final ArrayList<ByteBuffer> mBuffers = new ArrayList<ByteBuffer>();
25
26 private ByteBuffer mInitialBuffer;
27
28 private ByteBuffer mBuffer;
29
30 private int mSize;
31
32 private boolean mClosed;
33
34 public void setCapacity(int capacity) {
35 if (!mBuffers.isEmpty() || mInitialBuffer != null) {
36 throw new IllegalStateException();
37 }
38
39 mInitialBuffer = ByteBuffer.allocateDirect(capacity);
40 }
41
42 @Override
43 public int write(ByteBuffer buffer) throws IOException {
44 if (mClosed) {
45 throw new ClosedChannelException();
46 }
47
48 int size = buffer.remaining();
49 mSize += size;
50
51 if (mInitialBuffer != null) {
52 if (size <= mInitialBuffer.remaining()) {
53 mInitialBuffer.put(buffer);
54 return size;
55 }
56
57 // The supplied initial size was incorrect. Keep the accumulated
58 // data and switch to the usual "sequence of buffers" mode.
59 mInitialBuffer.flip();
60 mBuffers.add(mInitialBuffer);
61 mInitialBuffer = null;
62 }
63
64 // We can't hold a reference to this buffer, because it may wrap native
65 // memory and is not guaranteed to be immutable.
66 ByteBuffer tmpBuf = ByteBuffer.allocateDirect(size);
67 tmpBuf.put(buffer).rewind();
68 mBuffers.add(tmpBuf);
69 return size;
70 }
71
72 /**
73 * Returns the entire content accumulated by the channel as a ByteBuffer.
74 */
75 public ByteBuffer getByteBuffer() {
76 if (mInitialBuffer != null) {
77 mInitialBuffer.flip();
78 mBuffer = mInitialBuffer;
79 mInitialBuffer = null;
80 } else if (mBuffer != null && mSize == mBuffer.capacity()) {
81 // Cache hit
82 } else if (mBuffer == null && mBuffers.size() == 1) {
83 mBuffer = mBuffers.get(0);
84 } else {
85 mBuffer = ByteBuffer.allocateDirect(mSize);
86 int count = mBuffers.size();
87 for (int i = 0; i < count; i++) {
88 mBuffer.put(mBuffers.get(i));
89 }
90 mBuffer.rewind();
91 }
92 return mBuffer;
93 }
94
95 /**
96 * Returns the entire content accumulated by the channel as a byte array.
97 */
98 public byte[] getBytes() {
99 byte[] bytes = new byte[mSize];
100 if (mInitialBuffer != null) {
101 mInitialBuffer.flip();
102 mInitialBuffer.get(bytes);
103 } else {
104 int bufferCount = mBuffers.size();
105 int offset = 0;
106 for (int i = 0; i < bufferCount; i++) {
107 ByteBuffer buffer = mBuffers.get(i);
108 int bufferSize = buffer.remaining();
109 buffer.get(bytes, offset, bufferSize);
110 buffer.rewind();
111 offset += bufferSize;
112 }
113 }
114 return bytes;
115 }
116
117 @Override
118 public void close() {
119 mClosed = true;
120 }
121
122 @Override
123 public boolean isOpen() {
124 return !mClosed;
125 }
126 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698