deprecate readUTF, use readString instead (better named)

This commit is contained in:
Shay Banon 2012-07-08 21:21:17 +02:00
parent d3e9506a1d
commit 1dd30bd388
6 changed files with 112 additions and 11 deletions

View File

@ -89,6 +89,11 @@ public abstract class AdapterStreamInput extends StreamInput {
return in.readUTF();
}
@Override
public String readString() throws IOException {
return in.readString();
}
@Override
public int read(byte[] b) throws IOException {
return in.read(b);

View File

@ -107,6 +107,11 @@ public class AdapterStreamOutput extends StreamOutput {
out.writeUTF(str);
}
@Override
public void writeString(String str) throws IOException {
out.writeString(str);
}
@Override
public void writeFloat(float v) throws IOException {
out.writeFloat(v);

View File

@ -41,6 +41,7 @@ public class HandlesStreamInput extends AdapterStreamInput {
}
@Override
@Deprecated
public String readUTF() throws IOException {
byte b = in.readByte();
if (b == 0) {
@ -64,6 +65,30 @@ public class HandlesStreamInput extends AdapterStreamInput {
}
}
@Override
public String readString() throws IOException {
byte b = in.readByte();
if (b == 0) {
// full string with handle
int handle = in.readVInt();
String s = in.readString();
handles.put(handle, s);
return s;
} else if (b == 1) {
return handles.get(in.readVInt());
} else if (b == 2) {
// full string with handle
int handle = in.readVInt();
String s = in.readString();
identityHandles.put(handle, s);
return s;
} else if (b == 3) {
return identityHandles.get(in.readVInt());
} else {
throw new IOException("Expected handle header, got [" + b + "]");
}
}
@Override
public void reset() throws IOException {
super.reset();

View File

@ -49,6 +49,7 @@ public class HandlesStreamOutput extends AdapterStreamOutput {
}
@Override
@Deprecated
public void writeUTF(String s) throws IOException {
if (s.length() < identityThreshold) {
int handle = handles.get(s);
@ -76,6 +77,34 @@ public class HandlesStreamOutput extends AdapterStreamOutput {
}
}
@Override
public void writeString(String s) throws IOException {
if (s.length() < identityThreshold) {
int handle = handles.get(s);
if (handle == -1) {
handle = handles.size();
handles.put(s, handle);
out.writeByte((byte) 0);
out.writeVInt(handle);
out.writeUTF(s);
} else {
out.writeByte((byte) 1);
out.writeVInt(handle);
}
} else {
int handle = identityHandles.lookup(s);
if (handle == -1) {
handle = identityHandles.assign(s);
out.writeByte((byte) 2);
out.writeVInt(handle);
out.writeUTF(s);
} else {
out.writeByte((byte) 3);
out.writeVInt(handle);
}
}
}
@Override
public void reset() throws IOException {
handles.clear();

View File

@ -143,7 +143,11 @@ public abstract class StreamInput extends InputStream {
return i | ((b & 0x7FL) << 56);
}
/**
* @deprecated use {@link #readOptionalString()}
*/
@Nullable
@Deprecated
public String readOptionalUTF() throws IOException {
if (readBoolean()) {
return readUTF();
@ -151,7 +155,15 @@ public abstract class StreamInput extends InputStream {
return null;
}
public String readUTF() throws IOException {
@Nullable
public String readOptionalString() throws IOException {
if (readBoolean()) {
return readString();
}
return null;
}
public String readString() throws IOException {
int charCount = readVInt();
char[] chars = CachedStreamInput.getCharArray(charCount);
int c, charIndex = 0;
@ -180,6 +192,14 @@ public abstract class StreamInput extends InputStream {
return new String(chars, 0, charCount);
}
/**
* @deprecated use {@link #readString()}
*/
@Deprecated
public String readUTF() throws IOException {
return readString();
}
public final float readFloat() throws IOException {
return Float.intBitsToFloat(readInt());
@ -227,7 +247,7 @@ public abstract class StreamInput extends InputStream {
}
String[] ret = new String[size];
for (int i = 0; i < size; i++) {
ret[i] = readUTF();
ret[i] = readString();
}
return ret;
}
@ -245,7 +265,7 @@ public abstract class StreamInput extends InputStream {
case -1:
return null;
case 0:
return readUTF();
return readString();
case 1:
return readInt();
case 2:
@ -279,14 +299,14 @@ public abstract class StreamInput extends InputStream {
int size9 = readVInt();
Map map9 = new LinkedHashMap(size9);
for (int i = 0; i < size9; i++) {
map9.put(readUTF(), readGenericValue());
map9.put(readString(), readGenericValue());
}
return map9;
case 10:
int size10 = readVInt();
Map map10 = new HashMap(size10);
for (int i = 0; i < size10; i++) {
map10.put(readUTF(), readGenericValue());
map10.put(readString(), readGenericValue());
}
return map10;
case 11:

View File

@ -129,6 +129,7 @@ public abstract class StreamOutput extends OutputStream {
writeByte((byte) i);
}
@Deprecated
public void writeOptionalUTF(@Nullable String str) throws IOException {
if (str == null) {
writeBoolean(false);
@ -138,10 +139,16 @@ public abstract class StreamOutput extends OutputStream {
}
}
/**
* Writes a string.
*/
public void writeUTF(String str) throws IOException {
public void writeOptionalString(@Nullable String str) throws IOException {
if (str == null) {
writeBoolean(false);
} else {
writeBoolean(true);
writeString(str);
}
}
public void writeString(String str) throws IOException {
int charCount = str.length();
writeVInt(charCount);
int c;
@ -160,6 +167,16 @@ public abstract class StreamOutput extends OutputStream {
}
}
/**
* Writes a string.
*
* @deprecated use {@link #writeString(String)}
*/
@Deprecated
public void writeUTF(String str) throws IOException {
writeString(str);
}
public void writeFloat(float v) throws IOException {
writeInt(Float.floatToIntBits(v));
}
@ -204,7 +221,7 @@ public abstract class StreamOutput extends OutputStream {
public void writeStringArray(String[] array) throws IOException {
writeVInt(array.length);
for (String s : array) {
writeUTF(s);
writeString(s);
}
}
@ -220,7 +237,7 @@ public abstract class StreamOutput extends OutputStream {
Class type = value.getClass();
if (type == String.class) {
writeByte((byte) 0);
writeUTF((String) value);
writeString((String) value);
} else if (type == Integer.class) {
writeByte((byte) 1);
writeInt((Integer) value);