001: // ========================================================================
002: // Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
003: // ------------------------------------------------------------------------
004: // Licensed under the Apache License, Version 2.0 (the "License");
005: // you may not use this file except in compliance with the License.
006: // You may obtain a copy of the License at
007: // http://www.apache.org/licenses/LICENSE-2.0
008: // Unless required by applicable law or agreed to in writing, software
009: // distributed under the License is distributed on an "AS IS" BASIS,
010: // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
011: // See the License for the specific language governing permissions and
012: // limitations under the License.
013: // ========================================================================
014:
015: package org.mortbay.io;
016:
017: import java.io.IOException;
018: import java.io.InputStream;
019: import java.io.OutputStream;
020:
021: /* ------------------------------------------------------------------------------- */
022: /**
023: * @author gregw
024: */
025: public class ByteArrayBuffer extends AbstractBuffer {
026: private byte[] _bytes;
027:
028: public ByteArrayBuffer(byte[] bytes) {
029: this (bytes, 0, bytes.length, READWRITE);
030: }
031:
032: public ByteArrayBuffer(byte[] bytes, int index, int length) {
033: this (bytes, index, length, READWRITE);
034: }
035:
036: public ByteArrayBuffer(byte[] bytes, int index, int length,
037: int access) {
038: super (READWRITE, NON_VOLATILE);
039: _bytes = bytes;
040: setPutIndex(index + length);
041: setGetIndex(index);
042: _access = access;
043: }
044:
045: public ByteArrayBuffer(byte[] bytes, int index, int length,
046: int access, boolean isVolatile) {
047: super (READWRITE, isVolatile);
048: _bytes = bytes;
049: setPutIndex(index + length);
050: setGetIndex(index);
051: _access = access;
052: }
053:
054: public ByteArrayBuffer(int size) {
055: this (new byte[size], 0, size, READWRITE);
056: setPutIndex(0);
057: }
058:
059: public ByteArrayBuffer(String value) {
060: super (READWRITE, NON_VOLATILE);
061: _bytes = Portable.getBytes(value);
062: setGetIndex(0);
063: setPutIndex(_bytes.length);
064: _access = IMMUTABLE;
065: _string = value;
066: }
067:
068: public byte[] array() {
069: return _bytes;
070: }
071:
072: public int capacity() {
073: return _bytes.length;
074: }
075:
076: public byte get() {
077: return _bytes[_get++];
078: }
079:
080: public byte peek(int index) {
081: return _bytes[index];
082: }
083:
084: public int peek(int index, byte[] b, int offset, int length) {
085: int l = length;
086: if (index + l > capacity())
087: l = capacity() - index;
088: if (l <= 0)
089: return -1;
090: Portable.arraycopy(_bytes, index, b, offset, l);
091: return l;
092: }
093:
094: public void poke(int index, byte b) {
095: if (isReadOnly())
096: throw new IllegalStateException(__READONLY);
097: if (index < 0)
098: throw new IllegalArgumentException("index<0: " + index
099: + "<0");
100: if (index > capacity())
101: throw new IllegalArgumentException("index>capacity(): "
102: + index + ">" + capacity());
103: _bytes[index] = b;
104: }
105:
106: public static class CaseInsensitive extends ByteArrayBuffer
107: implements Buffer.CaseInsensitve {
108: public CaseInsensitive(String s) {
109: super (s);
110: }
111:
112: public CaseInsensitive(byte[] b, int o, int l, int rw) {
113: super (b, o, l, rw);
114: }
115: }
116:
117: /* ------------------------------------------------------------ */
118: /** Wrap a byte array.
119: * @param b
120: * @param off
121: * @param len
122: */
123: public void wrap(byte[] b, int off, int len) {
124: if (isReadOnly())
125: throw new IllegalStateException(__READONLY);
126: if (isImmutable())
127: throw new IllegalStateException(__IMMUTABLE);
128: _bytes = b;
129: clear();
130: setGetIndex(off);
131: setPutIndex(off + len);
132: }
133:
134: /* ------------------------------------------------------------ */
135: /** Wrap a byte array
136: * @param b
137: */
138: public void wrap(byte[] b) {
139: if (isReadOnly())
140: throw new IllegalStateException(__READONLY);
141: if (isImmutable())
142: throw new IllegalStateException(__IMMUTABLE);
143: _bytes = b;
144: setGetIndex(0);
145: setPutIndex(b.length);
146: }
147:
148: /* ------------------------------------------------------------ */
149: public void writeTo(OutputStream out) throws IOException {
150: out.write(_bytes, getIndex(), length());
151: clear();
152: }
153:
154: /* ------------------------------------------------------------ */
155: public int readFrom(InputStream in, int max) throws IOException {
156: if (max > space())
157: max = space();
158: int p = putIndex();
159: int len = in.read(_bytes, p, max);
160: if (len > 0)
161: setPutIndex(p + len);
162: return len;
163: }
164: }
|