001: /*
002: * $HeadURL:https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpcore/trunk/module-nio/src/main/java/org/apache/http/nio/buffer/ExpandableBuffer.java $
003: * $Revision:473999 $
004: * $Date:2006-11-12 17:31:38 +0000 (Sun, 12 Nov 2006) $
005: *
006: * ====================================================================
007: * Licensed to the Apache Software Foundation (ASF) under one
008: * or more contributor license agreements. See the NOTICE file
009: * distributed with this work for additional information
010: * regarding copyright ownership. The ASF licenses this file
011: * to you under the Apache License, Version 2.0 (the
012: * "License"); you may not use this file except in compliance
013: * with the License. You may obtain a copy of the License at
014: *
015: * http://www.apache.org/licenses/LICENSE-2.0
016: *
017: * Unless required by applicable law or agreed to in writing,
018: * software distributed under the License is distributed on an
019: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
020: * KIND, either express or implied. See the License for the
021: * specific language governing permissions and limitations
022: * under the License.
023: * ====================================================================
024: *
025: * This software consists of voluntary contributions made by many
026: * individuals on behalf of the Apache Software Foundation. For more
027: * information on the Apache Software Foundation, please see
028: * <http://www.apache.org/>.
029: *
030: */
031:
032: package org.apache.http.nio.util;
033:
034: import java.nio.ByteBuffer;
035:
036: public class ExpandableBuffer {
037:
038: public final static int INPUT_MODE = 0;
039: public final static int OUTPUT_MODE = 1;
040:
041: private int mode;
042: protected ByteBuffer buffer = null;
043: private final ByteBufferAllocator allocator;
044:
045: public ExpandableBuffer(int buffersize,
046: final ByteBufferAllocator allocator) {
047: super ();
048: if (allocator == null) {
049: throw new IllegalArgumentException(
050: "ByteBuffer allocator may not be null");
051: }
052: this .allocator = allocator;
053: this .buffer = allocator.allocate(buffersize);
054: this .mode = INPUT_MODE;
055: }
056:
057: protected int getMode() {
058: return this .mode;
059: }
060:
061: protected void setOutputMode() {
062: if (this .mode != OUTPUT_MODE) {
063: this .buffer.flip();
064: this .mode = OUTPUT_MODE;
065: }
066: }
067:
068: protected void setInputMode() {
069: if (this .mode != INPUT_MODE) {
070: if (this .buffer.hasRemaining()) {
071: this .buffer.compact();
072: } else {
073: this .buffer.clear();
074: }
075: this .mode = INPUT_MODE;
076: }
077: }
078:
079: private void expandCapacity(int capacity) {
080: ByteBuffer oldbuffer = this .buffer;
081: this .buffer = allocator.allocate(capacity);
082: oldbuffer.flip();
083: this .buffer.put(oldbuffer);
084: }
085:
086: protected void expand() {
087: int newcapacity = (this .buffer.capacity() + 1) << 1;
088: if (newcapacity < 0) {
089: newcapacity = Integer.MAX_VALUE;
090: }
091: expandCapacity(newcapacity);
092: }
093:
094: protected void ensureCapacity(int requiredCapacity) {
095: if (requiredCapacity > this .buffer.capacity()) {
096: expandCapacity(requiredCapacity);
097: }
098: }
099:
100: public int capacity() {
101: return this .buffer.capacity();
102: }
103:
104: public boolean hasData() {
105: setOutputMode();
106: return this .buffer.hasRemaining();
107: }
108:
109: public int length() {
110: setOutputMode();
111: return this .buffer.remaining();
112: }
113:
114: protected void clear() {
115: this .buffer.clear();
116: this .mode = INPUT_MODE;
117: }
118:
119: @Override
120: public String toString() {
121: StringBuffer sb = new StringBuffer();
122: sb.append("[mode=");
123: int mode = getMode();
124: if (mode == INPUT_MODE) {
125: sb.append("in");
126: } else {
127: sb.append("out");
128: }
129: sb.append(" pos=");
130: sb.append(this .buffer.position());
131: sb.append(" lim=");
132: sb.append(this .buffer.limit());
133: sb.append(" cap=");
134: sb.append(this .buffer.capacity());
135: sb.append("]");
136: return sb.toString();
137: }
138:
139: }
|