001: //========================================================================
002: //$Id: View.java,v 1.1 2005/10/05 14:09:25 janb Exp $
003: //Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
004: //------------------------------------------------------------------------
005: //Licensed under the Apache License, Version 2.0 (the "License");
006: //you may not use this file except in compliance with the License.
007: //You may obtain a copy of the License at
008: //http://www.apache.org/licenses/LICENSE-2.0
009: //Unless required by applicable law or agreed to in writing, software
010: //distributed under the License is distributed on an "AS IS" BASIS,
011: //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: //See the License for the specific language governing permissions and
013: //limitations under the License.
014: //========================================================================
015:
016: package org.mortbay.io;
017:
018: /**
019: * A View on another buffer. Allows operations that do not change the _content or
020: * indexes of the backing buffer.
021: *
022: * @author gregw
023: *
024: */
025: public class View extends AbstractBuffer {
026: Buffer _buffer;
027:
028: /**
029: * @param buffer The <code>Buffer</code> on which we are presenting a <code>View</code>.
030: * @param mark The initial value of the {@link Buffer#markIndex mark index}
031: * @param get The initial value of the {@link Buffer#getIndex get index}
032: * @param put The initial value of the {@link Buffer#putIndex put index}
033: * @param access The access level - one of the constants from {@link Buffer}.
034: */
035: public View(Buffer buffer, int mark, int get, int put, int access) {
036: super (READWRITE, !buffer.isImmutable());
037: _buffer = buffer.buffer();
038: setPutIndex(put);
039: setGetIndex(get);
040: setMarkIndex(mark);
041: _access = access;
042: }
043:
044: public View(Buffer buffer) {
045: super (READWRITE, !buffer.isImmutable());
046: _buffer = buffer.buffer();
047: setPutIndex(buffer.putIndex());
048: setGetIndex(buffer.getIndex());
049: setMarkIndex(buffer.markIndex());
050: _access = buffer.isReadOnly() ? READONLY : READWRITE;
051: }
052:
053: public View() {
054: super (READWRITE, true);
055: }
056:
057: /**
058: * Update view to buffer
059: */
060: public void update(Buffer buffer) {
061: _access = READWRITE;
062: _buffer = buffer.buffer();
063: setGetIndex(0);
064: setPutIndex(buffer.putIndex());
065: setGetIndex(buffer.getIndex());
066: setMarkIndex(buffer.markIndex());
067: _access = buffer.isReadOnly() ? READONLY : READWRITE;
068: }
069:
070: public void update(int get, int put) {
071: int a = _access;
072: _access = READWRITE;
073: setGetIndex(0);
074: setPutIndex(put);
075: setGetIndex(get);
076: setMarkIndex(-1);
077: _access = a;
078: }
079:
080: /**
081: * @return The {@link Buffer#array()} from the underlying buffer.
082: */
083: public byte[] array() {
084: return _buffer.array();
085: }
086:
087: /**
088: * @return The {@link Buffer#buffer()} from the underlying buffer.
089: */
090: public Buffer buffer() {
091: return _buffer.buffer();
092: }
093:
094: /**
095: * @return The {@link Buffer#capacity} of the underlying buffer.
096: */
097: public int capacity() {
098: return _buffer.capacity();
099: }
100:
101: /**
102: *
103: */
104: public void clear() {
105: setMarkIndex(-1);
106: setGetIndex(0);
107: setPutIndex(_buffer.getIndex());
108: setGetIndex(_buffer.getIndex());
109: }
110:
111: /**
112: *
113: */
114: public void compact() {
115: // TODO
116: }
117:
118: /*
119: * (non-Javadoc)
120: *
121: * @see java.lang.Object#equals(java.lang.Object)
122: */
123: public boolean equals(Object arg0) {
124: return this == arg0 || super .equals(arg0);
125: }
126:
127: /**
128: * @return Whether the underlying buffer is {@link Buffer#isReadOnly read only}
129: */
130: public boolean isReadOnly() {
131: return _buffer.isReadOnly();
132: }
133:
134: /**
135: * @return Whether the underlying buffer is {@link Buffer#isVolatile volatile}
136: */
137: public boolean isVolatile() {
138: return true;
139: }
140:
141: /**
142: * @return The result of calling {@link Buffer#peek(int)} on the underlying buffer
143: */
144: public byte peek(int index) {
145: return _buffer.peek(index);
146: }
147:
148: /**
149: * @return The result of calling {@link Buffer#peek(int, byte[], int, int)} on the underlying buffer
150: */
151: public int peek(int index, byte[] b, int offset, int length) {
152: return _buffer.peek(index, b, offset, length);
153: }
154:
155: /**
156: * @return The result of calling {@link Buffer#peek(int, int)} on the underlying buffer
157: */
158: public Buffer peek(int index, int length) {
159: return _buffer.peek(index, length);
160: }
161:
162: /**
163: * @param index
164: * @param src
165: */
166: public int poke(int index, Buffer src) {
167: return _buffer.poke(index, src);
168: }
169:
170: /**
171: * @param index
172: * @param b
173: */
174: public void poke(int index, byte b) {
175: _buffer.poke(index, b);
176: }
177:
178: /**
179: * @param index
180: * @param b
181: * @param offset
182: * @param length
183: */
184: public int poke(int index, byte[] b, int offset, int length) {
185: return _buffer.poke(index, b, offset, length);
186: }
187:
188: public String toString() {
189: if (_buffer == null)
190: return "INVALID";
191: return super.toString();
192: }
193: }
|