1 /* |
|
2 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package java.net.http; |
|
27 |
|
28 import java.io.IOException; |
|
29 import java.nio.ByteBuffer; |
|
30 |
|
31 class DataFrame extends Http2Frame { |
|
32 |
|
33 public final static int TYPE = 0x0; |
|
34 |
|
35 DataFrame() { |
|
36 type = TYPE; |
|
37 } |
|
38 |
|
39 // Flags |
|
40 public static final int END_STREAM = 0x1; |
|
41 public static final int PADDED = 0x8; |
|
42 |
|
43 int padLength; |
|
44 int dataLength; |
|
45 ByteBuffer[] data; |
|
46 |
|
47 public void setData(ByteBuffer[] data) { |
|
48 this.data = data; |
|
49 setDataLength(); |
|
50 } |
|
51 |
|
52 @Override |
|
53 String flagAsString(int flag) { |
|
54 switch (flag) { |
|
55 case END_STREAM: |
|
56 return "END_STREAM"; |
|
57 case PADDED: |
|
58 return "PADDED"; |
|
59 } |
|
60 return super.flagAsString(flag); |
|
61 } |
|
62 |
|
63 public synchronized void setData(ByteBuffer data) { |
|
64 ByteBuffer[] bb; |
|
65 if (data == null) { |
|
66 bb = new ByteBuffer[0]; |
|
67 } else { |
|
68 bb = new ByteBuffer[1]; |
|
69 bb[0] = data; |
|
70 } |
|
71 setData(bb); |
|
72 } |
|
73 |
|
74 public synchronized ByteBuffer[] getData() { |
|
75 return data; |
|
76 } |
|
77 |
|
78 private void setDataLength() { |
|
79 int len = 0; |
|
80 for (ByteBuffer buf : data) { |
|
81 len += buf.remaining(); |
|
82 } |
|
83 dataLength = len; |
|
84 } |
|
85 |
|
86 @Override |
|
87 void readIncomingImpl(ByteBufferConsumer bc) throws IOException { |
|
88 if ((flags & PADDED) != 0) { |
|
89 padLength = bc.getByte(); |
|
90 dataLength = length - (padLength + 1); |
|
91 } else { |
|
92 dataLength = length; |
|
93 } |
|
94 data = bc.getBuffers(dataLength); |
|
95 } |
|
96 |
|
97 int getPadLength() { |
|
98 return padLength; |
|
99 } |
|
100 |
|
101 int getDataLength() { |
|
102 return dataLength; |
|
103 } |
|
104 |
|
105 @Override |
|
106 void writeOutgoing(ByteBufferGenerator bg) { |
|
107 super.writeOutgoing(bg); |
|
108 if ((flags & PADDED) != 0) { |
|
109 ByteBuffer buf = bg.getBuffer(1); |
|
110 buf.put((byte)getPadLength()); |
|
111 } |
|
112 for (int i=0; i<data.length; i++) { |
|
113 bg.addByteBuffer(data[i]); |
|
114 } |
|
115 if ((flags & PADDED) != 0) { |
|
116 bg.addPadding(padLength); |
|
117 } |
|
118 } |
|
119 |
|
120 @Override |
|
121 void computeLength() { |
|
122 length = dataLength; |
|
123 if ((flags & PADDED) != 0) { |
|
124 length += (1 + padLength); |
|
125 } |
|
126 } |
|
127 } |
|