View Javadoc

1   // ========================================================================
2   // Copyright (c) 2004-2009 Mort Bay Consulting Pty. Ltd.
3   // ------------------------------------------------------------------------
4   // All rights reserved. This program and the accompanying materials
5   // are made available under the terms of the Eclipse Public License v1.0
6   // and Apache License v2.0 which accompanies this distribution.
7   // The Eclipse Public License is available at 
8   // http://www.eclipse.org/legal/epl-v10.html
9   // The Apache License v2.0 is available at
10  // http://www.opensource.org/licenses/apache2.0.php
11  // You may elect to redistribute this code under either of these licenses. 
12  // ========================================================================
13  
14  package org.eclipse.jetty.io;
15  
16  /**
17   * A View on another buffer.  Allows operations that do not change the _content or
18   * indexes of the backing buffer.
19   * 
20   * 
21   * 
22   */
23  public class View extends AbstractBuffer
24  {
25      Buffer _buffer;
26  
27      /**
28       * @param buffer The <code>Buffer</code> on which we are presenting a <code>View</code>.
29       * @param mark The initial value of the {@link Buffer#markIndex mark index}
30       * @param get The initial value of the {@link Buffer#getIndex get index}
31       * @param put The initial value of the {@link Buffer#putIndex put index}
32       * @param access The access level - one of the constants from {@link Buffer}.
33       */
34      public View(Buffer buffer, int mark, int get, int put,int access)
35      {
36          super(READWRITE,!buffer.isImmutable());
37          _buffer=buffer.buffer();
38          setPutIndex(put);
39          setGetIndex(get);
40          setMarkIndex(mark);
41          _access=access;
42      }
43      
44      public View(Buffer buffer)
45      {
46          super(READWRITE,!buffer.isImmutable());
47          _buffer=buffer.buffer();
48          setPutIndex(buffer.putIndex());
49          setGetIndex(buffer.getIndex());
50          setMarkIndex(buffer.markIndex());
51          _access=buffer.isReadOnly()?READONLY:READWRITE;
52      }
53  
54      public View()
55      {
56          super(READWRITE,true);
57      }
58      
59      /**
60       * Update view to buffer
61       */
62      public void update(Buffer buffer)
63      {
64          _access=READWRITE;
65          _buffer=buffer.buffer();
66          setGetIndex(0);
67          setPutIndex(buffer.putIndex());
68          setGetIndex(buffer.getIndex());
69          setMarkIndex(buffer.markIndex());
70          _access=buffer.isReadOnly()?READONLY:READWRITE;
71      }
72  
73      public void update(int get, int put)
74      {
75          int a=_access;
76          _access=READWRITE;
77          setGetIndex(0);
78          setPutIndex(put);
79          setGetIndex(get);
80          setMarkIndex(-1);
81          _access=a;
82      }
83  
84      /**
85       * @return The {@link Buffer#array()} from the underlying buffer.
86       */
87      public byte[] array()
88      {
89          return _buffer.array();
90      }
91  
92      /**
93       * @return The {@link Buffer#buffer()} from the underlying buffer.
94       */
95      @Override
96      public Buffer buffer()
97      {
98          return _buffer.buffer();
99      }
100 
101     /**
102      * @return The {@link Buffer#capacity} of the underlying buffer.
103      */
104     public int capacity()
105     {
106         return _buffer.capacity();
107     }
108 
109     /**
110      *  
111      */
112     @Override
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     @Override
125     public void compact()
126     {
127         // TODO
128     }
129 
130     /*
131      * (non-Javadoc)
132      * 
133      * @see java.lang.Object#equals(java.lang.Object)
134      */
135     @Override
136     public boolean equals(Object obj)
137     {
138         return  this==obj ||((obj instanceof Buffer)&& obj.equals(this)) || super.equals(obj);
139     }
140 
141     /**
142      * @return Whether the underlying buffer is {@link Buffer#isReadOnly read only}
143      */
144     @Override
145     public boolean isReadOnly()
146     {
147         return _buffer.isReadOnly();
148     }
149 
150     /**
151      * @return Whether the underlying buffer is {@link Buffer#isVolatile volatile}
152      */
153     @Override
154     public boolean isVolatile()
155     {
156         return true;
157     }
158 
159     /**
160      * @return The result of calling {@link Buffer#peek(int)} on the underlying buffer
161      */
162     public byte peek(int index)
163     {
164         return _buffer.peek(index);
165     }
166 
167     /**
168      * @return The result of calling {@link Buffer#peek(int, byte[], int, int)} on the underlying buffer
169      */
170     public int peek(int index, byte[] b, int offset, int length)
171     {
172         return _buffer.peek(index,b,offset,length);
173     }
174 
175     /**
176      * @return The result of calling {@link Buffer#peek(int, int)} on the underlying buffer
177      */
178     @Override
179     public Buffer peek(int index, int length)
180     {
181         return _buffer.peek(index, length);
182     }
183     
184     /**
185      * @param index
186      * @param src
187      */
188     @Override
189     public int poke(int index, Buffer src)
190     {
191         return _buffer.poke(index,src); 
192     }
193 
194     /**
195      * @param index
196      * @param b
197      */
198     public void poke(int index, byte b)
199     {
200         _buffer.poke(index,b);
201     }
202 
203     /**
204      * @param index
205      * @param b
206      * @param offset
207      * @param length
208      */
209     @Override
210     public int poke(int index, byte[] b, int offset, int length)
211     {
212         return _buffer.poke(index,b,offset,length);
213     }
214     
215     @Override
216     public String toString()
217     {
218         if (_buffer==null)
219             return "INVALID";
220         return super.toString();
221     }
222     
223     public static class CaseInsensitive extends View implements Buffer.CaseInsensitve
224     {
225         public CaseInsensitive()
226         {
227             super();
228         }
229 
230         public CaseInsensitive(Buffer buffer, int mark, int get, int put, int access)
231         {
232             super(buffer,mark,get,put,access);
233         }
234 
235         public CaseInsensitive(Buffer buffer)
236         {
237             super(buffer);
238         }
239         
240         @Override
241         public boolean equals(Object obj)
242         {
243             return  this==obj ||((obj instanceof Buffer)&&((Buffer)obj).equalsIgnoreCase(this)) || super.equals(obj);
244         }
245     }
246 }