1 //========================================================================
2 //Copyright 2011-2012 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.spdy.api;
15
16 import java.nio.ByteBuffer;
17 import java.nio.charset.Charset;
18 import java.util.concurrent.atomic.AtomicInteger;
19
20 /**
21 * <p>A container for DATA frames metadata and content bytes.</p>
22 * <p>Specialized subclasses (like {@link StringDataInfo}) may be used by applications
23 * to send specific types of content.</p>
24 * <p>Applications may send multiple instances of {@link DataInfo}, usually of the same
25 * type, via {@link Stream#data(DataInfo)}. The last instance must have the
26 * {@link #isClose() close flag} set, so that the client knows that no more content is
27 * expected.</p>
28 * <p>Receivers of {@link DataInfo} via {@link StreamFrameListener#onData(Stream, DataInfo)}
29 * have two different APIs to read the data content bytes: a {@link #readInto(ByteBuffer) read}
30 * API that does not interact with flow control, and a {@link #consumeInto(ByteBuffer) drain}
31 * API that interacts with flow control.</p>
32 * <p>Flow control is defined so that when the sender wants to sends a number of bytes larger
33 * than the {@link Settings.ID#INITIAL_WINDOW_SIZE} value, it will stop sending as soon as it
34 * has sent a number of bytes equal to the window size. The receiver has to <em>consume</em>
35 * the data bytes that it received in order to tell the sender to send more bytes.</p>
36 * <p>Consuming the data bytes can be done only via {@link #consumeInto(ByteBuffer)} or by a combination
37 * of {@link #readInto(ByteBuffer)} and {@link #consume(int)} (possibly at different times).</p>
38 */
39 public abstract class DataInfo
40 {
41 /**
42 * <p>Flag that indicates that this {@link DataInfo} is the last frame in the stream.</p>
43 *
44 * @see #isClose()
45 * @see #getFlags()
46 */
47 public final static byte FLAG_CLOSE = 1;
48 /**
49 * <p>Flag that indicates that this {@link DataInfo}'s data is compressed.</p>
50 *
51 * @see #isCompress()
52 * @see #getFlags()
53 */
54 public final static byte FLAG_COMPRESS = 2;
55
56 private final AtomicInteger consumed = new AtomicInteger();
57 private boolean close;
58 private boolean compress;
59
60 /**
61 * <p>Creates a new {@link DataInfo} with the given close flag and no compression flag.</p>
62 *
63 * @param close the value of the close flag
64 */
65 public DataInfo(boolean close)
66 {
67 setClose(close);
68 }
69
70 /**
71 * <p>Creates a new {@link DataInfo} with the given close flag and given compression flag.</p>
72 *
73 * @param close the close flag
74 * @param compress the compress flag
75 */
76 public DataInfo(boolean close, boolean compress)
77 {
78 setClose(close);
79 setCompress(compress);
80 }
81
82 /**
83 * @return the value of the compress flag
84 * @see #setCompress(boolean)
85 */
86 public boolean isCompress()
87 {
88 return compress;
89 }
90
91 /**
92 * @param compress the value of the compress flag
93 * @see #isCompress()
94 */
95 public void setCompress(boolean compress)
96 {
97 this.compress = compress;
98 }
99
100 /**
101 * @return the value of the close flag
102 * @see #setClose(boolean)
103 */
104 public boolean isClose()
105 {
106 return close;
107 }
108
109 /**
110 * @param close the value of the close flag
111 * @see #isClose()
112 */
113 public void setClose(boolean close)
114 {
115 this.close = close;
116 }
117
118 /**
119 * @return the close and compress flags as integer
120 * @see #FLAG_CLOSE
121 * @see #FLAG_COMPRESS
122 */
123 public byte getFlags()
124 {
125 byte flags = isClose() ? FLAG_CLOSE : 0;
126 flags |= isCompress() ? FLAG_COMPRESS : 0;
127 return flags;
128 }
129
130 /**
131 * @return the total number of content bytes
132 * @see #available()
133 */
134 public abstract int length();
135
136 /**
137 * <p>Returns the available content bytes that can be read via {@link #readInto(ByteBuffer)}.</p>
138 * <p>Each invocation to {@link #readInto(ByteBuffer)} modifies the value returned by this method,
139 * until no more content bytes are available.</p>
140 *
141 * @return the available content bytes
142 * @see #readInto(ByteBuffer)
143 */
144 public abstract int available();
145
146 /**
147 * <p>Copies the content bytes of this {@link DataInfo} into the given {@link ByteBuffer}.</p>
148 * <p>If the given {@link ByteBuffer} cannot contain the whole content of this {@link DataInfo}
149 * then after the read {@link #available()} will return a positive value, and further content
150 * may be retrieved by invoking again this method with a new output buffer.</p>
151 *
152 * @param output the {@link ByteBuffer} to copy to bytes into
153 * @return the number of bytes copied
154 * @see #available()
155 * @see #consumeInto(ByteBuffer)
156 */
157 public abstract int readInto(ByteBuffer output);
158
159 /**
160 * <p>Reads and consumes the content bytes of this {@link DataInfo} into the given {@link ByteBuffer}.</p>
161 *
162 * @param output the {@link ByteBuffer} to copy the bytes into
163 * @return the number of bytes copied
164 * @see #consume(int)
165 */
166 public int consumeInto(ByteBuffer output)
167 {
168 int read = readInto(output);
169 consume(read);
170 return read;
171 }
172
173 /**
174 * <p>Consumes the given number of bytes from this {@link DataInfo}.</p>
175 *
176 * @param delta the number of bytes consumed
177 */
178 public void consume(int delta)
179 {
180 if (delta < 0)
181 throw new IllegalArgumentException();
182 int read = length() - available();
183 int newConsumed = consumed() + delta;
184 // if (newConsumed > read)
185 // throw new IllegalStateException("Consuming without reading: consumed " + newConsumed + " but only read " + read);
186 consumed.addAndGet(delta);
187 }
188
189 /**
190 * @return the number of bytes consumed
191 */
192 public int consumed()
193 {
194 return consumed.get();
195 }
196
197 /**
198 *
199 * @param charset the charset used to convert the bytes
200 * @param consume whether to consume the content
201 * @return a String with the content of this {@link DataInfo}
202 */
203 public String asString(String charset, boolean consume)
204 {
205 ByteBuffer buffer = asByteBuffer(consume);
206 return Charset.forName(charset).decode(buffer).toString();
207 }
208
209 /**
210 * @return a byte array with the content of this {@link DataInfo}
211 * @param consume whether to consume the content
212 */
213 public byte[] asBytes(boolean consume)
214 {
215 ByteBuffer buffer = asByteBuffer(consume);
216 byte[] result = new byte[buffer.remaining()];
217 buffer.get(result);
218 return result;
219 }
220
221 /**
222 * @return a {@link ByteBuffer} with the content of this {@link DataInfo}
223 * @param consume whether to consume the content
224 */
225 public ByteBuffer asByteBuffer(boolean consume)
226 {
227 ByteBuffer buffer = allocate(available());
228 if (consume)
229 consumeInto(buffer);
230 else
231 readInto(buffer);
232 buffer.flip();
233 return buffer;
234 }
235
236 protected ByteBuffer allocate(int size)
237 {
238 return ByteBuffer.allocate(size);
239 }
240
241 @Override
242 public String toString()
243 {
244 return String.format("DATA @%x available=%d consumed=%d close=%b compress=%b", hashCode(), available(), consumed(), isClose(), isCompress());
245 }
246 }