From c989d3a928b6bfc526dc5e21161fb31f058f77dc Mon Sep 17 00:00:00 2001 From: kimchy Date: Tue, 17 Aug 2010 10:21:26 +0300 Subject: [PATCH] fix limit on UTF writing (the new mechanism) to be higher than short --- .../common/io/stream/BytesStreamInput.java | 2 +- .../common/io/stream/BytesStreamOutput.java | 14 ++++++-------- .../common/io/stream/StreamInput.java | 10 +--------- .../common/io/stream/StreamOutput.java | 18 ++++++++---------- 4 files changed, 16 insertions(+), 28 deletions(-) diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/common/io/stream/BytesStreamInput.java b/modules/elasticsearch/src/main/java/org/elasticsearch/common/io/stream/BytesStreamInput.java index 45e9d71122f..5870f1788b5 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/common/io/stream/BytesStreamInput.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/common/io/stream/BytesStreamInput.java @@ -105,7 +105,7 @@ public class BytesStreamInput extends StreamInput { } public String readUTF() throws IOException { - int utflen = readUnsignedShort(); + int utflen = readInt(); if (utflen == 0) { return ""; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/common/io/stream/BytesStreamOutput.java b/modules/elasticsearch/src/main/java/org/elasticsearch/common/io/stream/BytesStreamOutput.java index 79ebed3b013..a15183e55cb 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/common/io/stream/BytesStreamOutput.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/common/io/stream/BytesStreamOutput.java @@ -20,7 +20,6 @@ package org.elasticsearch.common.io.stream; import java.io.IOException; -import java.io.UTFDataFormatException; import java.util.Arrays; /** @@ -132,19 +131,18 @@ public class BytesStreamOutput extends StreamOutput { } } - if (utflen > 65535) - throw new UTFDataFormatException( - "encoded string too long: " + utflen + " bytes"); - - int newcount = count + utflen + 2; + int newcount = count + utflen + 4; if (newcount > buf.length) { buf = Arrays.copyOf(buf, Math.max(buf.length << 1, newcount)); } byte[] bytearr = this.buf; - bytearr[count++] = (byte) ((utflen >>> 8) & 0xFF); - bytearr[count++] = (byte) ((utflen >>> 0) & 0xFF); + // same as writeInt + bytearr[count++] = (byte) (utflen >> 24); + bytearr[count++] = (byte) (utflen >> 16); + bytearr[count++] = (byte) (utflen >> 8); + bytearr[count++] = (byte) (utflen); int i = 0; for (i = 0; i < strlen; i++) { diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java b/modules/elasticsearch/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java index 7cae19c9ba6..306842da8ce 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/common/io/stream/StreamInput.java @@ -102,18 +102,10 @@ public abstract class StreamInput extends InputStream { return i; } - protected final int readUnsignedShort() throws IOException { - int ch1 = read(); - int ch2 = read(); - if ((ch1 | ch2) < 0) - throw new EOFException(); - return (ch1 << 8) + (ch2 << 0); - } - // COPIED from DataInputStream public String readUTF() throws IOException { - int utflen = readUnsignedShort(); + int utflen = readInt(); if (utflen == 0) { return ""; } diff --git a/modules/elasticsearch/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java b/modules/elasticsearch/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java index 27f2354ceb1..d5ddd9043ed 100644 --- a/modules/elasticsearch/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java +++ b/modules/elasticsearch/src/main/java/org/elasticsearch/common/io/stream/StreamOutput.java @@ -21,7 +21,6 @@ package org.elasticsearch.common.io.stream; import java.io.IOException; import java.io.OutputStream; -import java.io.UTFDataFormatException; /** * @author kimchy (shay.banon) @@ -135,16 +134,15 @@ public abstract class StreamOutput extends OutputStream { } } - if (utflen > 65535) - throw new UTFDataFormatException( - "encoded string too long: " + utflen + " bytes"); - - if (this.bytearr == null || (this.bytearr.length < (utflen + 2))) - this.bytearr = new byte[(utflen * 2) + 2]; + if (this.bytearr == null || (this.bytearr.length < (utflen + 4))) + this.bytearr = new byte[(utflen * 2) + 4]; byte[] bytearr = this.bytearr; - bytearr[count++] = (byte) ((utflen >>> 8) & 0xFF); - bytearr[count++] = (byte) ((utflen >>> 0) & 0xFF); + // same as writeInt + bytearr[count++] = (byte) (utflen >> 24); + bytearr[count++] = (byte) (utflen >> 16); + bytearr[count++] = (byte) (utflen >> 8); + bytearr[count++] = (byte) (utflen); int i = 0; for (i = 0; i < strlen; i++) { @@ -167,7 +165,7 @@ public abstract class StreamOutput extends OutputStream { bytearr[count++] = (byte) (0x80 | ((c >> 0) & 0x3F)); } } - writeBytes(bytearr, 0, utflen + 2); + writeBytes(bytearr, 0, utflen + 4); // return utflen + 2; }