fix limit on UTF writing (the new mechanism) to be higher than short

This commit is contained in:
kimchy 2010-08-17 10:21:26 +03:00
parent e1f3fee4e4
commit c989d3a928
4 changed files with 16 additions and 28 deletions

View File

@ -105,7 +105,7 @@ public class BytesStreamInput extends StreamInput {
}
public String readUTF() throws IOException {
int utflen = readUnsignedShort();
int utflen = readInt();
if (utflen == 0) {
return "";
}

View File

@ -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++) {

View File

@ -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 "";
}

View File

@ -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;
}