author | erikj |
Tue, 12 Sep 2017 19:03:39 +0200 | |
changeset 47216 | 71c04702a3d5 |
parent 45938 | jdk/src/java.base/share/classes/sun/security/util/IOUtils.java@8e9f94d3dcba |
child 47418 | 6993f9f1ff83 |
permissions | -rw-r--r-- |
4188 | 1 |
/* |
45938
8e9f94d3dcba
8183591: Incorrect behavior when reading DER value with Integer.MAX_VALUE length
apetcher
parents:
32933
diff
changeset
|
2 |
* Copyright (c) 2009, 2017 Oracle and/or its affiliates. All rights reserved. |
4188 | 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 |
4188 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
4188 | 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. |
|
4188 | 24 |
*/ |
25 |
||
26 |
/** |
|
27 |
* IOUtils: A collection of IO-related public static methods. |
|
28 |
*/ |
|
29 |
||
32933 | 30 |
package sun.security.util; |
4188 | 31 |
|
32 |
import java.io.EOFException; |
|
33 |
import java.io.IOException; |
|
34 |
import java.io.InputStream; |
|
35 |
import java.util.Arrays; |
|
36 |
||
37 |
public class IOUtils { |
|
38 |
||
39 |
/** |
|
40 |
* Read up to <code>length</code> of bytes from <code>in</code> |
|
41 |
* until EOF is detected. |
|
30655 | 42 |
* @param is input stream, must not be null |
45938
8e9f94d3dcba
8183591: Incorrect behavior when reading DER value with Integer.MAX_VALUE length
apetcher
parents:
32933
diff
changeset
|
43 |
* @param length number of bytes to read |
4188 | 44 |
* @param readAll if true, an EOFException will be thrown if not enough |
45938
8e9f94d3dcba
8183591: Incorrect behavior when reading DER value with Integer.MAX_VALUE length
apetcher
parents:
32933
diff
changeset
|
45 |
* bytes are read. |
4188 | 46 |
* @return bytes read |
47 |
* @throws IOException Any IO error or a premature EOF is detected |
|
48 |
*/ |
|
49 |
public static byte[] readFully(InputStream is, int length, boolean readAll) |
|
50 |
throws IOException { |
|
45938
8e9f94d3dcba
8183591: Incorrect behavior when reading DER value with Integer.MAX_VALUE length
apetcher
parents:
32933
diff
changeset
|
51 |
if (length < 0) { |
8e9f94d3dcba
8183591: Incorrect behavior when reading DER value with Integer.MAX_VALUE length
apetcher
parents:
32933
diff
changeset
|
52 |
throw new IOException("Invalid length"); |
8e9f94d3dcba
8183591: Incorrect behavior when reading DER value with Integer.MAX_VALUE length
apetcher
parents:
32933
diff
changeset
|
53 |
} |
4188 | 54 |
byte[] output = {}; |
55 |
int pos = 0; |
|
56 |
while (pos < length) { |
|
57 |
int bytesToRead; |
|
58 |
if (pos >= output.length) { // Only expand when there's no room |
|
59 |
bytesToRead = Math.min(length - pos, output.length + 1024); |
|
60 |
if (output.length < pos + bytesToRead) { |
|
61 |
output = Arrays.copyOf(output, pos + bytesToRead); |
|
62 |
} |
|
63 |
} else { |
|
64 |
bytesToRead = output.length - pos; |
|
65 |
} |
|
66 |
int cc = is.read(output, pos, bytesToRead); |
|
67 |
if (cc < 0) { |
|
45938
8e9f94d3dcba
8183591: Incorrect behavior when reading DER value with Integer.MAX_VALUE length
apetcher
parents:
32933
diff
changeset
|
68 |
if (readAll) { |
4188 | 69 |
throw new EOFException("Detect premature EOF"); |
70 |
} else { |
|
71 |
if (output.length != pos) { |
|
72 |
output = Arrays.copyOf(output, pos); |
|
73 |
} |
|
74 |
break; |
|
75 |
} |
|
76 |
} |
|
77 |
pos += cc; |
|
78 |
} |
|
79 |
return output; |
|
80 |
} |
|
81 |
} |