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 public Buffer buffer()
96 {
97 return _buffer.buffer();
98 }
99
100 /**
101 * @return The {@link Buffer#capacity} of the underlying buffer.
102 */
103 public int capacity()
104 {
105 return _buffer.capacity();
106 }
107
108 /**
109 *
110 */
111 public void clear()
112 {
113 setMarkIndex(-1);
114 setGetIndex(0);
115 setPutIndex(_buffer.getIndex());
116 setGetIndex(_buffer.getIndex());
117 }
118
119 /**
120 *
121 */
122 public void compact()
123 {
124 // TODO
125 }
126
127 /*
128 * (non-Javadoc)
129 *
130 * @see java.lang.Object#equals(java.lang.Object)
131 */
132 public boolean equals(Object obj)
133 {
134 return this==obj ||((obj instanceof Buffer)&& obj.equals(this)) || super.equals(obj);
135 }
136
137 /**
138 * @return Whether the underlying buffer is {@link Buffer#isReadOnly read only}
139 */
140 public boolean isReadOnly()
141 {
142 return _buffer.isReadOnly();
143 }
144
145 /**
146 * @return Whether the underlying buffer is {@link Buffer#isVolatile volatile}
147 */
148 public boolean isVolatile()
149 {
150 return true;
151 }
152
153 /**
154 * @return The result of calling {@link Buffer#peek(int)} on the underlying buffer
155 */
156 public byte peek(int index)
157 {
158 return _buffer.peek(index);
159 }
160
161 /**
162 * @return The result of calling {@link Buffer#peek(int, byte[], int, int)} on the underlying buffer
163 */
164 public int peek(int index, byte[] b, int offset, int length)
165 {
166 return _buffer.peek(index,b,offset,length);
167 }
168
169 /**
170 * @return The result of calling {@link Buffer#peek(int, int)} on the underlying buffer
171 */
172 public Buffer peek(int index, int length)
173 {
174 return _buffer.peek(index, length);
175 }
176
177 /**
178 * @param index
179 * @param src
180 */
181 public int poke(int index, Buffer src)
182 {
183 return _buffer.poke(index,src);
184 }
185
186 /**
187 * @param index
188 * @param b
189 */
190 public void poke(int index, byte b)
191 {
192 _buffer.poke(index,b);
193 }
194
195 /**
196 * @param index
197 * @param b
198 * @param offset
199 * @param length
200 */
201 public int poke(int index, byte[] b, int offset, int length)
202 {
203 return _buffer.poke(index,b,offset,length);
204 }
205
206 public String toString()
207 {
208 if (_buffer==null)
209 return "INVALID";
210 return super.toString();
211 }
212
213 public static class CaseInsensitive extends View implements Buffer.CaseInsensitve
214 {
215 public CaseInsensitive()
216 {
217 super();
218 }
219
220 public CaseInsensitive(Buffer buffer, int mark, int get, int put, int access)
221 {
222 super(buffer,mark,get,put,access);
223 }
224
225 public CaseInsensitive(Buffer buffer)
226 {
227 super(buffer);
228 }
229
230 public boolean equals(Object obj)
231 {
232 return this==obj ||((obj instanceof Buffer)&&((Buffer)obj).equalsIgnoreCase(this)) || super.equals(obj);
233 }
234 }
235 }