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