1:
37:
38:
39: package ;
40:
41: import ;
42: import ;
43:
44: import ;
45:
46: import ;
47: import ;
48: import ;
49:
50:
54: public final class CompressionMethodList implements Iterable<CompressionMethod>
55: {
56: private final ByteBuffer buffer;
57: private int modCount;
58:
59: public CompressionMethodList (final ByteBuffer buffer)
60: {
61: this.buffer = buffer;
62: modCount = 0;
63: }
64:
65:
70: public int size ()
71: {
72: return (buffer.get (0) & 0xFF);
73: }
74:
75:
83: public CompressionMethod get (final int index)
84: {
85: int size = size ();
86: if (index < 0 || index >= size)
87: throw new IndexOutOfBoundsException ("limit: " + size
88: + "; requested: " + index);
89: return CompressionMethod.getInstance (buffer.get (1 + index));
90: }
91:
92:
106: public void put (final int index, final CompressionMethod method)
107: {
108: int size = size ();
109: if (index < 0 || index >= size)
110: throw new IndexOutOfBoundsException ("limit: " + size
111: + "; requested: " + index);
112: buffer.position (1 + index);
113: buffer.put ((byte) method.getValue ());
114: modCount++;
115: }
116:
117:
131: public void setSize (final int newSize)
132: {
133: if (newSize < 0 || newSize > 255)
134: throw new IllegalArgumentException ("size must be between 0 and 255");
135: if (newSize + 1 > buffer.capacity ())
136: throw new IllegalArgumentException ("limit: " + buffer.capacity ()
137: + "; requested: " + newSize);
138: buffer.put (0, (byte) newSize);
139: modCount++;
140: }
141:
142: public String toString ()
143: {
144: return toString (null);
145: }
146:
147: public String toString (final String prefix)
148: {
149: StringWriter str = new StringWriter ();
150: PrintWriter out = new PrintWriter (str);
151: if (prefix != null)
152: out.print (prefix);
153: out.print ("[");
154: out.print (size ());
155: out.println ("] {");
156: for (Iterator it = new Iterator (); it.hasNext (); )
157: {
158: CompressionMethod method = (CompressionMethod) it.next ();
159: if (prefix != null)
160: out.print (prefix);
161: out.print (" ");
162: out.print (method);
163: if (it.hasNext ())
164: out.print (",");
165: out.println ();
166: }
167: if (prefix != null)
168: out.print (prefix);
169: out.print ("};");
170: return str.toString ();
171: }
172:
173: public boolean equals (Object o)
174: {
175: if (!(o instanceof CompressionMethodList))
176: return false;
177: CompressionMethodList that = (CompressionMethodList) o;
178:
179: if (size () != that.size ())
180: return false;
181:
182: for (Iterator it1 = new Iterator (), it2 = that.new Iterator ();
183: it1.hasNext () && it2.hasNext (); )
184: {
185: if (!it1.next ().equals (it2.next ()))
186: return false;
187: }
188: return true;
189: }
190:
191: public java.util.Iterator<CompressionMethod> iterator ()
192: {
193: return new Iterator ();
194: }
195:
196:
203: public class Iterator implements ListIterator<CompressionMethod>
204: {
205: private int index;
206: private final int modCount;
207:
208: Iterator ()
209: {
210: index = 0;
211: modCount = CompressionMethodList.this.modCount;
212: }
213:
214: public void add (CompressionMethod cm)
215: {
216: throw new UnsupportedOperationException ();
217: }
218:
219: public boolean hasNext ()
220: {
221: return (index < size ());
222: }
223:
224: public boolean hasPrevious ()
225: {
226: return (index > 0);
227: }
228:
229: public CompressionMethod next () throws NoSuchElementException
230: {
231: if (modCount != CompressionMethodList.this.modCount)
232: throw new ConcurrentModificationException ();
233: try
234: {
235: return get (index++);
236: }
237: catch (IndexOutOfBoundsException ioobe)
238: {
239: throw new NoSuchElementException ();
240: }
241: }
242:
243: public int nextIndex ()
244: {
245: if (hasNext ())
246: return (index + 1);
247: return -1;
248: }
249:
250: public CompressionMethod previous () throws NoSuchElementException
251: {
252: if (index == 0)
253: throw new NoSuchElementException ();
254: if (modCount != CompressionMethodList.this.modCount)
255: throw new ConcurrentModificationException ();
256: try
257: {
258: return get (--index);
259: }
260: catch (IndexOutOfBoundsException ioobe)
261: {
262: throw new NoSuchElementException ();
263: }
264: }
265:
266: public int previousIndex ()
267: {
268: return (index - 1);
269: }
270:
271: public void remove ()
272: {
273: throw new UnsupportedOperationException ();
274: }
275:
276: public void set (final CompressionMethod cm)
277: {
278: put (index, cm);
279: }
280: }
281: }