OLD | NEW |
(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 */ |
| 19 public class ChunkedWritableByteChannel implements WritableByteChannel { |
| 20 |
| 21 private final ArrayList<ByteBuffer> mBuffers = new ArrayList<ByteBuffer>(); |
| 22 |
| 23 private ByteBuffer mInitialBuffer; |
| 24 |
| 25 private ByteBuffer mBuffer; |
| 26 |
| 27 private int mSize; |
| 28 |
| 29 private boolean mClosed; |
| 30 |
| 31 public void setCapacity(int capacity) { |
| 32 if (!mBuffers.isEmpty() || mInitialBuffer != null) { |
| 33 throw new IllegalStateException(); |
| 34 } |
| 35 |
| 36 mInitialBuffer = ByteBuffer.allocateDirect(capacity); |
| 37 } |
| 38 |
| 39 @Override |
| 40 public int write(ByteBuffer buffer) throws IOException { |
| 41 if (mClosed) { |
| 42 throw new ClosedChannelException(); |
| 43 } |
| 44 |
| 45 int size = buffer.remaining(); |
| 46 mSize += size; |
| 47 |
| 48 if (mInitialBuffer != null) { |
| 49 if (size <= mInitialBuffer.remaining()) { |
| 50 mInitialBuffer.put(buffer); |
| 51 return size; |
| 52 } |
| 53 |
| 54 // The supplied initial size was incorrect. Keep the accumulated |
| 55 // data |
| 56 // and switch to the usual "sequence of buffers" mode. |
| 57 mInitialBuffer.flip(); |
| 58 mBuffers.add(mInitialBuffer); |
| 59 mInitialBuffer = null; |
| 60 } |
| 61 |
| 62 // We can't hold a reference to this buffer, because it may wrap native |
| 63 // memory |
| 64 // and is not guaranteed to be immutable. |
| 65 ByteBuffer tmpBuf = ByteBuffer.allocateDirect(size); |
| 66 tmpBuf.put(buffer).rewind(); |
| 67 mBuffers.add(tmpBuf); |
| 68 return size; |
| 69 } |
| 70 |
| 71 /** |
| 72 * Returns the entire content accumulated by the channel as a ByteBuffer. |
| 73 */ |
| 74 public ByteBuffer getByteBuffer() { |
| 75 if (mInitialBuffer != null) { |
| 76 mInitialBuffer.flip(); |
| 77 mBuffer = mInitialBuffer; |
| 78 mInitialBuffer = null; |
| 79 } else if (mBuffer != null && mSize == mBuffer.capacity()) { |
| 80 // Cache hit |
| 81 } else if (mBuffer == null && mBuffers.size() == 1) { |
| 82 mBuffer = mBuffers.get(0); |
| 83 } else { |
| 84 mBuffer = ByteBuffer.allocateDirect(mSize); |
| 85 int count = mBuffers.size(); |
| 86 for (int i = 0; i < count; i++) { |
| 87 mBuffer.put(mBuffers.get(i)); |
| 88 } |
| 89 mBuffer.rewind(); |
| 90 } |
| 91 return mBuffer; |
| 92 } |
| 93 |
| 94 /** |
| 95 * Returns the entire content accumulated by the channel as a byte array. |
| 96 */ |
| 97 public byte[] getBytes() { |
| 98 byte[] bytes = new byte[mSize]; |
| 99 if (mInitialBuffer != null) { |
| 100 mInitialBuffer.flip(); |
| 101 mInitialBuffer.get(bytes); |
| 102 } else { |
| 103 int bufferCount = mBuffers.size(); |
| 104 int offset = 0; |
| 105 for (int i = 0; i < bufferCount; i++) { |
| 106 ByteBuffer buffer = mBuffers.get(i); |
| 107 int bufferSize = buffer.remaining(); |
| 108 buffer.get(bytes, offset, bufferSize); |
| 109 buffer.rewind(); |
| 110 offset += bufferSize; |
| 111 } |
| 112 } |
| 113 return bytes; |
| 114 } |
| 115 |
| 116 @Override |
| 117 public void close() { |
| 118 mClosed = true; |
| 119 } |
| 120 |
| 121 @Override |
| 122 public boolean isOpen() { |
| 123 return !mClosed; |
| 124 } |
| 125 } |
OLD | NEW |