RFR [9]8134424: BlockDataInputStream.readUTFBody: examine sizing local StringBuffer with the given length
Chris Hegarty
chris.hegarty at oracle.com
Mon Feb 8 11:15:49 UTC 2016
Low hanging fruit to avoid unnecessary allocations when deserializing.
readUTF knows the string size ahead of the read and can avoid
expandCapacity() by constructing the StringBuilder with the expected size.
It is an implementation detail, but if the size is larger than Integer.MAX_VALUE,
then OOM can be thrown, as is the case in the implementation today.
diff --git a/src/java.base/share/classes/java/io/ObjectInputStream.java b/src/java.base/share/classes/java/io/ObjectInputStream.java
--- a/src/java.base/share/classes/java/io/ObjectInputStream.java
+++ b/src/java.base/share/classes/java/io/ObjectInputStream.java
@@ -3144,7 +3144,9 @@
* utflen bytes.
*/
private String readUTFBody(long utflen) throws IOException {
- StringBuilder sbuf = new StringBuilder();
+ if (utflen > Integer.MAX_VALUE)
+ throw new OutOfMemoryError("UTF length, " + utflen + ", too big.");
+ StringBuilder sbuf = new StringBuilder((int)utflen);
if (!blkmode) {
end = pos = 0;
}
-Chris.
More information about the core-libs-dev
mailing list