View Javadoc

1   //========================================================================
2   //$Id: View.java,v 1.1 2005/10/05 14:09:25 janb Exp $
3   //Copyright 2004-2005 Mort Bay Consulting Pty. Ltd.
4   //------------------------------------------------------------------------
5   //Licensed under the Apache License, Version 2.0 (the "License");
6   //you may not use this file except in compliance with the License.
7   //You may obtain a copy of the License at
8   //http://www.apache.org/licenses/LICENSE-2.0
9   //Unless required by applicable law or agreed to in writing, software
10  //distributed under the License is distributed on an "AS IS" BASIS,
11  //WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  //See the License for the specific language governing permissions and
13  //limitations under the License.
14  //========================================================================
15  
16  package org.mortbay.io;
17  
18  /**
19   * A View on another buffer.  Allows operations that do not change the _content or
20   * indexes of the backing buffer.
21   * 
22   * @author gregw
23   * 
24   */
25  public class View extends AbstractBuffer
26  {
27      Buffer _buffer;
28  
29      /**
30       * @param buffer The <code>Buffer</code> on which we are presenting a <code>View</code>.
31       * @param mark The initial value of the {@link Buffer#markIndex mark index}
32       * @param get The initial value of the {@link Buffer#getIndex get index}
33       * @param put The initial value of the {@link Buffer#putIndex put index}
34       * @param access The access level - one of the constants from {@link Buffer}.
35       */
36      public View(Buffer buffer, int mark, int get, int put,int access)
37      {
38          super(READWRITE,!buffer.isImmutable());
39          _buffer=buffer.buffer();
40          setPutIndex(put);
41          setGetIndex(get);
42          setMarkIndex(mark);
43          _access=access;
44      }
45      
46      public View(Buffer buffer)
47      {
48          super(READWRITE,!buffer.isImmutable());
49          _buffer=buffer.buffer();
50          setPutIndex(buffer.putIndex());
51          setGetIndex(buffer.getIndex());
52          setMarkIndex(buffer.markIndex());
53          _access=buffer.isReadOnly()?READONLY:READWRITE;
54      }
55  
56      public View()
57      {
58          super(READWRITE,true);
59      }
60      
61      /**
62       * Update view to buffer
63       */
64      public void update(Buffer buffer)
65      {
66          _access=READWRITE;
67          _buffer=buffer.buffer();
68          setGetIndex(0);
69          setPutIndex(buffer.putIndex());
70          setGetIndex(buffer.getIndex());
71          setMarkIndex(buffer.markIndex());
72          _access=buffer.isReadOnly()?READONLY:READWRITE;
73      }
74  
75      public void update(int get, int put)
76      {
77          int a=_access;
78          _access=READWRITE;
79          setGetIndex(0);
80          setPutIndex(put);
81          setGetIndex(get);
82          setMarkIndex(-1);
83          _access=a;
84      }
85  
86      /**
87       * @return The {@link Buffer#array()} from the underlying buffer.
88       */
89      public byte[] array()
90      {
91          return _buffer.array();
92      }
93  
94      /**
95       * @return The {@link Buffer#buffer()} from the underlying buffer.
96       */
97      public Buffer buffer()
98      {
99          return _buffer.buffer();
100     }
101 
102     /**
103      * @return The {@link Buffer#capacity} of the underlying buffer.
104      */
105     public int capacity()
106     {
107         return _buffer.capacity();
108     }
109 
110     /**
111      *  
112      */
113     public void clear()
114     {
115         setMarkIndex(-1);
116         setGetIndex(0);
117         setPutIndex(_buffer.getIndex());
118         setGetIndex(_buffer.getIndex());
119     }
120 
121     /**
122      *  
123      */
124     public void compact()
125     {
126         // TODO
127     }
128 
129     /*
130      * (non-Javadoc)
131      * 
132      * @see java.lang.Object#equals(java.lang.Object)
133      */
134     public boolean equals(Object obj)
135     {
136         return  this==obj ||((obj instanceof Buffer)&&((Buffer)obj).equals(this)) || super.equals(obj);
137     }
138 
139     /**
140      * @return Whether the underlying buffer is {@link Buffer#isReadOnly read only}
141      */
142     public boolean isReadOnly()
143     {
144         return _buffer.isReadOnly();
145     }
146 
147     /**
148      * @return Whether the underlying buffer is {@link Buffer#isVolatile volatile}
149      */
150     public boolean isVolatile()
151     {
152         return true;
153     }
154 
155     /**
156      * @return The result of calling {@link Buffer#peek(int)} on the underlying buffer
157      */
158     public byte peek(int index)
159     {
160         return _buffer.peek(index);
161     }
162 
163     /**
164      * @return The result of calling {@link Buffer#peek(int, byte[], int, int)} on the underlying buffer
165      */
166     public int peek(int index, byte[] b, int offset, int length)
167     {
168         return _buffer.peek(index,b,offset,length);
169     }
170 
171     /**
172      * @return The result of calling {@link Buffer#peek(int, int)} on the underlying buffer
173      */
174     public Buffer peek(int index, int length)
175     {
176         return _buffer.peek(index, length);
177     }
178     
179     /**
180      * @param index
181      * @param src
182      */
183     public int poke(int index, Buffer src)
184     {
185         return _buffer.poke(index,src); 
186     }
187 
188     /**
189      * @param index
190      * @param b
191      */
192     public void poke(int index, byte b)
193     {
194         _buffer.poke(index,b);
195     }
196 
197     /**
198      * @param index
199      * @param b
200      * @param offset
201      * @param length
202      */
203     public int poke(int index, byte[] b, int offset, int length)
204     {
205         return _buffer.poke(index,b,offset,length);
206     }
207     
208     public String toString()
209     {
210         if (_buffer==null)
211             return "INVALID";
212         return super.toString();
213     }
214     
215     public static class CaseInsensitive extends View implements Buffer.CaseInsensitve
216     {
217         public CaseInsensitive()
218         {
219             super();
220         }
221 
222         public CaseInsensitive(Buffer buffer, int mark, int get, int put, int access)
223         {
224             super(buffer,mark,get,put,access);
225         }
226 
227         public CaseInsensitive(Buffer buffer)
228         {
229             super(buffer);
230         }
231         
232         public boolean equals(Object obj)
233         {
234             return  this==obj ||((obj instanceof Buffer)&&((Buffer)obj).equalsIgnoreCase(this)) || super.equals(obj);
235         }
236     }
237 }