This source file includes following definitions.
- setCapacity
- write
- getByteBuffer
- getBytes
- close
- isOpen
package org.chromium.net;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.ClosedChannelException;
import java.nio.channels.WritableByteChannel;
import java.util.ArrayList;
public class ChunkedWritableByteChannel implements WritableByteChannel {
private final ArrayList<ByteBuffer> mBuffers = new ArrayList<ByteBuffer>();
private ByteBuffer mInitialBuffer;
private ByteBuffer mBuffer;
private int mSize;
private boolean mClosed;
public void setCapacity(int capacity) {
if (!mBuffers.isEmpty() || mInitialBuffer != null) {
throw new IllegalStateException();
}
mInitialBuffer = ByteBuffer.allocateDirect(capacity);
}
@Override
public int write(ByteBuffer buffer) throws IOException {
if (mClosed) {
throw new ClosedChannelException();
}
int size = buffer.remaining();
mSize += size;
if (mInitialBuffer != null) {
if (size <= mInitialBuffer.remaining()) {
mInitialBuffer.put(buffer);
return size;
}
mInitialBuffer.flip();
mBuffers.add(mInitialBuffer);
mInitialBuffer = null;
}
ByteBuffer tmpBuf = ByteBuffer.allocateDirect(size);
tmpBuf.put(buffer).rewind();
mBuffers.add(tmpBuf);
return size;
}
public ByteBuffer getByteBuffer() {
if (mInitialBuffer != null) {
mInitialBuffer.flip();
mBuffer = mInitialBuffer;
mInitialBuffer = null;
} else if (mBuffer != null && mSize == mBuffer.capacity()) {
} else if (mBuffer == null && mBuffers.size() == 1) {
mBuffer = mBuffers.get(0);
} else {
mBuffer = ByteBuffer.allocateDirect(mSize);
int count = mBuffers.size();
for (int i = 0; i < count; i++) {
mBuffer.put(mBuffers.get(i));
}
mBuffer.rewind();
}
return mBuffer;
}
public byte[] getBytes() {
byte[] bytes = new byte[mSize];
if (mInitialBuffer != null) {
mInitialBuffer.flip();
mInitialBuffer.get(bytes);
} else {
int bufferCount = mBuffers.size();
int offset = 0;
for (int i = 0; i < bufferCount; i++) {
ByteBuffer buffer = mBuffers.get(i);
int bufferSize = buffer.remaining();
buffer.get(bytes, offset, bufferSize);
buffer.rewind();
offset += bufferSize;
}
}
return bytes;
}
@Override
public void close() {
mClosed = true;
}
@Override
public boolean isOpen() {
return !mClosed;
}
}