author | dfuchs |
Fri, 30 Aug 2019 15:42:27 +0100 | |
branch | JDK-8229867-branch |
changeset 57968 | 8595871a5446 |
parent 47216 | 71c04702a3d5 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
57968 | 2 |
* Copyright (c) 2004, 2019, Oracle and/or its affiliates. All rights reserved. |
2 | 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 |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 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 |
* |
|
5506 | 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. |
|
2 | 24 |
*/ |
25 |
package sun.net.www.http; |
|
26 |
||
27 |
import java.io.*; |
|
57968 | 28 |
import java.util.concurrent.locks.Lock; |
29 |
import java.util.concurrent.locks.ReentrantLock; |
|
2 | 30 |
|
31 |
/** |
|
32 |
* OutputStream that sends the output to the underlying stream using chunked |
|
33 |
* encoding as specified in RFC 2068. |
|
34 |
*/ |
|
35 |
public class ChunkedOutputStream extends PrintStream { |
|
36 |
||
37 |
/* Default chunk size (including chunk header) if not specified */ |
|
38 |
static final int DEFAULT_CHUNK_SIZE = 4096; |
|
1635
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
39 |
private static final byte[] CRLF = {'\r', '\n'}; |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
40 |
private static final int CRLF_SIZE = CRLF.length; |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
41 |
private static final byte[] FOOTER = CRLF; |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
42 |
private static final int FOOTER_SIZE = CRLF_SIZE; |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
43 |
private static final byte[] EMPTY_CHUNK_HEADER = getHeader(0); |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
44 |
private static final int EMPTY_CHUNK_HEADER_SIZE = getHeaderSize(0); |
2 | 45 |
|
46 |
/* internal buffer */ |
|
47 |
private byte buf[]; |
|
1635
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
48 |
/* size of data (excluding footers and headers) already stored in buf */ |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
49 |
private int size; |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
50 |
/* current index in buf (i.e. buf[count] */ |
2 | 51 |
private int count; |
1635
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
52 |
/* number of bytes to be filled up to complete a data chunk |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
53 |
* currently being built */ |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
54 |
private int spaceInCurrentChunk; |
2 | 55 |
|
56 |
/* underlying stream */ |
|
57 |
private PrintStream out; |
|
58 |
||
59 |
/* the chunk size we use */ |
|
1635
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
60 |
private int preferredChunkDataSize; |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
61 |
private int preferedHeaderSize; |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
62 |
private int preferredChunkGrossSize; |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
63 |
/* header for a complete Chunk */ |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
64 |
private byte[] completeHeader; |
2 | 65 |
|
57968 | 66 |
private final Lock writeLock = new ReentrantLock(); |
67 |
||
2 | 68 |
/* return the size of the header for a particular chunk size */ |
1635
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
69 |
private static int getHeaderSize(int size) { |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
70 |
return (Integer.toHexString(size)).length() + CRLF_SIZE; |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
71 |
} |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
72 |
|
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
73 |
/* return a header for a particular chunk size */ |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
74 |
private static byte[] getHeader(int size){ |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
75 |
try { |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
76 |
String hexStr = Integer.toHexString(size); |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
77 |
byte[] hexBytes = hexStr.getBytes("US-ASCII"); |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
78 |
byte[] header = new byte[getHeaderSize(size)]; |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
79 |
for (int i=0; i<hexBytes.length; i++) |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
80 |
header[i] = hexBytes[i]; |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
81 |
header[hexBytes.length] = CRLF[0]; |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
82 |
header[hexBytes.length+1] = CRLF[1]; |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
83 |
return header; |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
84 |
} catch (java.io.UnsupportedEncodingException e) { |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
85 |
/* This should never happen */ |
10419
12c063b39232
7084245: Update usages of InternalError to use exception chaining
sherman
parents:
5506
diff
changeset
|
86 |
throw new InternalError(e.getMessage(), e); |
1635
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
87 |
} |
2 | 88 |
} |
89 |
||
90 |
public ChunkedOutputStream(PrintStream o) { |
|
91 |
this(o, DEFAULT_CHUNK_SIZE); |
|
92 |
} |
|
93 |
||
94 |
public ChunkedOutputStream(PrintStream o, int size) { |
|
95 |
super(o); |
|
96 |
out = o; |
|
97 |
||
98 |
if (size <= 0) { |
|
99 |
size = DEFAULT_CHUNK_SIZE; |
|
100 |
} |
|
1635
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
101 |
|
2 | 102 |
/* Adjust the size to cater for the chunk header - eg: if the |
103 |
* preferred chunk size is 1k this means the chunk size should |
|
1635
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
104 |
* be 1017 bytes (differs by 7 from preferred size because of |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
105 |
* 3 bytes for chunk size in hex and CRLF (header) and CRLF (footer)). |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
106 |
* |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
107 |
* If headerSize(adjusted_size) is shorter then headerSize(size) |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
108 |
* then try to use the extra byte unless headerSize(adjusted_size+1) |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
109 |
* increases back to headerSize(size) |
2 | 110 |
*/ |
111 |
if (size > 0) { |
|
1635
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
112 |
int adjusted_size = size - getHeaderSize(size) - FOOTER_SIZE; |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
113 |
if (getHeaderSize(adjusted_size+1) < getHeaderSize(size)){ |
2 | 114 |
adjusted_size++; |
115 |
} |
|
116 |
size = adjusted_size; |
|
117 |
} |
|
118 |
||
119 |
if (size > 0) { |
|
1635
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
120 |
preferredChunkDataSize = size; |
2 | 121 |
} else { |
1635
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
122 |
preferredChunkDataSize = DEFAULT_CHUNK_SIZE - |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
123 |
getHeaderSize(DEFAULT_CHUNK_SIZE) - FOOTER_SIZE; |
2 | 124 |
} |
125 |
||
1635
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
126 |
preferedHeaderSize = getHeaderSize(preferredChunkDataSize); |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
127 |
preferredChunkGrossSize = preferedHeaderSize + preferredChunkDataSize |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
128 |
+ FOOTER_SIZE; |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
129 |
completeHeader = getHeader(preferredChunkDataSize); |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
130 |
|
2 | 131 |
/* start with an initial buffer */ |
18173
cf70549231af
8015421: NegativeArraySizeException occurs in ChunkedOutputStream() with Integer.MAX_VALUE
khazra
parents:
14342
diff
changeset
|
132 |
buf = new byte[preferredChunkGrossSize]; |
1635
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
133 |
reset(); |
2 | 134 |
} |
135 |
||
136 |
/* |
|
1635
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
137 |
* Flush a buffered, completed chunk to an underlying stream. If the data in |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
138 |
* the buffer is insufficient to build up a chunk of "preferredChunkSize" |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
139 |
* then the data do not get flushed unless flushAll is true. If flushAll is |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
140 |
* true then the remaining data builds up a last chunk which size is smaller |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
141 |
* than preferredChunkSize, and then the last chunk gets flushed to |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
142 |
* underlying stream. If flushAll is true and there is no data in a buffer |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
143 |
* at all then an empty chunk (containing a header only) gets flushed to |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
144 |
* underlying stream. |
2 | 145 |
*/ |
1635
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
146 |
private void flush(boolean flushAll) { |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
147 |
if (spaceInCurrentChunk == 0) { |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
148 |
/* flush a completed chunk to underlying stream */ |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
149 |
out.write(buf, 0, preferredChunkGrossSize); |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
150 |
out.flush(); |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
151 |
reset(); |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
152 |
} else if (flushAll){ |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
153 |
/* complete the last chunk and flush it to underlying stream */ |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
154 |
if (size > 0){ |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
155 |
/* adjust a header start index in case the header of the last |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
156 |
* chunk is shorter then preferedHeaderSize */ |
2 | 157 |
|
1635
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
158 |
int adjustedHeaderStartIndex = preferedHeaderSize - |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
159 |
getHeaderSize(size); |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
160 |
|
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
161 |
/* write header */ |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
162 |
System.arraycopy(getHeader(size), 0, buf, |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
163 |
adjustedHeaderStartIndex, getHeaderSize(size)); |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
164 |
|
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
165 |
/* write footer */ |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
166 |
buf[count++] = FOOTER[0]; |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
167 |
buf[count++] = FOOTER[1]; |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
168 |
|
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
169 |
//send the last chunk to underlying stream |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
170 |
out.write(buf, adjustedHeaderStartIndex, count - adjustedHeaderStartIndex); |
2 | 171 |
} else { |
1635
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
172 |
//send an empty chunk (containing just a header) to underlying stream |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
173 |
out.write(EMPTY_CHUNK_HEADER, 0, EMPTY_CHUNK_HEADER_SIZE); |
2 | 174 |
} |
175 |
||
176 |
out.flush(); |
|
1635
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
177 |
reset(); |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
178 |
} |
2 | 179 |
} |
180 |
||
1635
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
181 |
@Override |
2 | 182 |
public boolean checkError() { |
183 |
return out.checkError(); |
|
184 |
} |
|
185 |
||
186 |
/* Check that the output stream is still open */ |
|
187 |
private void ensureOpen() { |
|
188 |
if (out == null) |
|
189 |
setError(); |
|
190 |
} |
|
191 |
||
1635
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
192 |
/* |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
193 |
* Writes data from b[] to an internal buffer and stores the data as data |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
194 |
* chunks of a following format: {Data length in Hex}{CRLF}{data}{CRLF} |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
195 |
* The size of the data is preferredChunkSize. As soon as a completed chunk |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
196 |
* is read from b[] a process of reading from b[] suspends, the chunk gets |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
197 |
* flushed to the underlying stream and then the reading process from b[] |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
198 |
* continues. When there is no more sufficient data in b[] to build up a |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
199 |
* chunk of preferredChunkSize size the data get stored as an incomplete |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
200 |
* chunk of a following format: {space for data length}{CRLF}{data} |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
201 |
* The size of the data is of course smaller than preferredChunkSize. |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
202 |
*/ |
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
203 |
@Override |
57968 | 204 |
public void write(byte b[], int off, int len) { |
205 |
writeLock.lock(); |
|
206 |
try { |
|
207 |
ensureOpen(); |
|
208 |
if ((off < 0) || (off > b.length) || (len < 0) || |
|
209 |
((off + len) > b.length) || ((off + len) < 0)) { |
|
210 |
throw new IndexOutOfBoundsException(); |
|
211 |
} else if (len == 0) { |
|
212 |
return; |
|
2 | 213 |
} |
88
b2fea49cad6b
6631048: Problem when writing on output stream of HttpURLConnection
chegar
parents:
2
diff
changeset
|
214 |
|
57968 | 215 |
/* if b[] contains enough data then one loop cycle creates one complete |
216 |
* data chunk with a header, body and a footer, and then flushes the |
|
217 |
* chunk to the underlying stream. Otherwise, the last loop cycle |
|
218 |
* creates incomplete data chunk with empty header and with no footer |
|
219 |
* and stores this incomplete chunk in an internal buffer buf[] |
|
220 |
*/ |
|
221 |
int bytesToWrite = len; |
|
222 |
int inputIndex = off; /* the index of the byte[] currently being written */ |
|
223 |
||
224 |
do { |
|
225 |
/* enough data to complete a chunk */ |
|
226 |
if (bytesToWrite >= spaceInCurrentChunk) { |
|
227 |
||
228 |
/* header */ |
|
229 |
for (int i = 0; i < completeHeader.length; i++) |
|
230 |
buf[i] = completeHeader[i]; |
|
231 |
||
232 |
/* data */ |
|
233 |
System.arraycopy(b, inputIndex, buf, count, spaceInCurrentChunk); |
|
234 |
inputIndex += spaceInCurrentChunk; |
|
235 |
bytesToWrite -= spaceInCurrentChunk; |
|
236 |
count += spaceInCurrentChunk; |
|
2 | 237 |
|
57968 | 238 |
/* footer */ |
239 |
buf[count++] = FOOTER[0]; |
|
240 |
buf[count++] = FOOTER[1]; |
|
241 |
spaceInCurrentChunk = 0; //chunk is complete |
|
242 |
||
243 |
flush(false); |
|
244 |
if (checkError()) { |
|
245 |
break; |
|
246 |
} |
|
247 |
} |
|
1635
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
248 |
|
57968 | 249 |
/* not enough data to build a chunk */ |
250 |
else { |
|
251 |
/* header */ |
|
252 |
/* do not write header if not enough bytes to build a chunk yet */ |
|
253 |
||
254 |
/* data */ |
|
255 |
System.arraycopy(b, inputIndex, buf, count, bytesToWrite); |
|
256 |
count += bytesToWrite; |
|
257 |
size += bytesToWrite; |
|
258 |
spaceInCurrentChunk -= bytesToWrite; |
|
259 |
bytesToWrite = 0; |
|
260 |
||
261 |
/* footer */ |
|
262 |
/* do not write header if not enough bytes to build a chunk yet */ |
|
263 |
} |
|
264 |
} while (bytesToWrite > 0); |
|
265 |
} finally { |
|
266 |
writeLock.unlock(); |
|
267 |
} |
|
2 | 268 |
} |
269 |
||
1635
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
270 |
@Override |
57968 | 271 |
public void write(int _b) { |
272 |
writeLock.lock(); |
|
273 |
try { |
|
274 |
byte b[] = {(byte) _b}; |
|
275 |
write(b, 0, 1); |
|
276 |
} finally { |
|
277 |
writeLock.unlock(); |
|
278 |
} |
|
2 | 279 |
} |
280 |
||
57968 | 281 |
public void reset() { |
282 |
writeLock.lock(); |
|
283 |
try { |
|
284 |
count = preferedHeaderSize; |
|
285 |
size = 0; |
|
286 |
spaceInCurrentChunk = preferredChunkDataSize; |
|
287 |
} finally { |
|
288 |
writeLock.unlock(); |
|
289 |
} |
|
2 | 290 |
} |
291 |
||
292 |
public int size() { |
|
1635
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
293 |
return size; |
2 | 294 |
} |
295 |
||
1635
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
296 |
@Override |
57968 | 297 |
public void close() { |
298 |
writeLock.lock(); |
|
299 |
try { |
|
300 |
ensureOpen(); |
|
2 | 301 |
|
57968 | 302 |
/* if we have buffer a chunked send it */ |
303 |
if (size > 0) { |
|
304 |
flush(true); |
|
305 |
} |
|
306 |
||
307 |
/* send a zero length chunk */ |
|
1635
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
308 |
flush(true); |
57968 | 309 |
|
310 |
/* don't close the underlying stream */ |
|
311 |
out = null; |
|
312 |
} finally { |
|
313 |
writeLock.unlock(); |
|
2 | 314 |
} |
315 |
} |
|
316 |
||
1635
8ca7ecc0226d
6720866: Slow performance using HttpURLConnection for upload
chegar
parents:
715
diff
changeset
|
317 |
@Override |
57968 | 318 |
public void flush() { |
319 |
writeLock.lock(); |
|
320 |
try { |
|
321 |
ensureOpen(); |
|
322 |
if (size > 0) { |
|
323 |
flush(true); |
|
324 |
} |
|
325 |
} finally { |
|
326 |
writeLock.unlock(); |
|
2 | 327 |
} |
328 |
} |
|
329 |
} |