Skip to content

Commit

Permalink
8267670: Update java.io, java.math, and java.text to use switch expre…
Browse files Browse the repository at this point in the history
…ssions

Reviewed-by: darcy, chegar, naoto, iris, dfuchs, lancea, vtewari
  • Loading branch information
pconcannon committed Jun 1, 2021
1 parent f5634fe commit 4eb2168
Show file tree
Hide file tree
Showing 11 changed files with 161 additions and 349 deletions.
14 changes: 7 additions & 7 deletions src/java.base/share/classes/java/io/DataInputStream.java
Expand Up @@ -623,12 +623,12 @@ public static final String readUTF(DataInput in) throws IOException {
while (count < utflen) {
c = (int) bytearr[count] & 0xff;
switch (c >> 4) {
case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7:
case 0, 1, 2, 3, 4, 5, 6, 7 -> {
/* 0xxxxxxx*/
count++;
chararr[chararr_count++]=(char)c;
break;
case 12: case 13:
}
case 12, 13 -> {
/* 110x xxxx 10xx xxxx*/
count += 2;
if (count > utflen)
Expand All @@ -640,8 +640,8 @@ public static final String readUTF(DataInput in) throws IOException {
"malformed input around byte " + count);
chararr[chararr_count++]=(char)(((c & 0x1F) << 6) |
(char2 & 0x3F));
break;
case 14:
}
case 14 -> {
/* 1110 xxxx 10xx xxxx 10xx xxxx */
count += 3;
if (count > utflen)
Expand All @@ -655,8 +655,8 @@ public static final String readUTF(DataInput in) throws IOException {
chararr[chararr_count++]=(char)(((c & 0x0F) << 12) |
((char2 & 0x3F) << 6) |
((char3 & 0x3F) << 0));
break;
default:
}
default ->
/* 10xx xxxx, 1111 xxxx */
throw new UTFDataFormatException(
"malformed input around byte " + count);
Expand Down
119 changes: 37 additions & 82 deletions src/java.base/share/classes/java/io/ObjectInputStream.java
Expand Up @@ -1778,21 +1778,13 @@ String readTypeString() throws IOException {
int oldHandle = passHandle;
try {
byte tc = bin.peekByte();
switch (tc) {
case TC_NULL:
return (String) readNull();

case TC_REFERENCE:
return (String) readHandle(false);

case TC_STRING:
case TC_LONGSTRING:
return readString(false);

default:
throw new StreamCorruptedException(
return switch (tc) {
case TC_NULL -> (String) readNull();
case TC_REFERENCE -> (String) readHandle(false);
case TC_STRING, TC_LONGSTRING -> readString(false);
default -> throw new StreamCorruptedException(
String.format("invalid type code: %02X", tc));
}
};
} finally {
passHandle = oldHandle;
}
Expand Down Expand Up @@ -1872,27 +1864,20 @@ private ObjectStreamClass readClassDesc(boolean unshared)
throws IOException
{
byte tc = bin.peekByte();
ObjectStreamClass descriptor;
switch (tc) {
case TC_NULL:
descriptor = (ObjectStreamClass) readNull();
break;
case TC_REFERENCE:
descriptor = (ObjectStreamClass) readHandle(unshared);

return switch (tc) {
case TC_NULL -> (ObjectStreamClass) readNull();
case TC_PROXYCLASSDESC -> readProxyDesc(unshared);
case TC_CLASSDESC -> readNonProxyDesc(unshared);
case TC_REFERENCE -> {
var d = (ObjectStreamClass) readHandle(unshared);
// Should only reference initialized class descriptors
descriptor.checkInitialized();
break;
case TC_PROXYCLASSDESC:
descriptor = readProxyDesc(unshared);
break;
case TC_CLASSDESC:
descriptor = readNonProxyDesc(unshared);
break;
default:
throw new StreamCorruptedException(
d.checkInitialized();
yield d;
}
default -> throw new StreamCorruptedException(
String.format("invalid type code: %02X", tc));
}
return descriptor;
};
}

private boolean isCustomSubclass() {
Expand Down Expand Up @@ -2052,21 +2037,13 @@ private ObjectStreamClass readNonProxyDesc(boolean unshared)
* assigned handle.
*/
private String readString(boolean unshared) throws IOException {
String str;
byte tc = bin.readByte();
switch (tc) {
case TC_STRING:
str = bin.readUTF();
break;

case TC_LONGSTRING:
str = bin.readLongUTF();
break;

default:
throw new StreamCorruptedException(
String str = switch (tc) {
case TC_STRING -> bin.readUTF();
case TC_LONGSTRING -> bin.readLongUTF();
default -> throw new StreamCorruptedException(
String.format("invalid type code: %02X", tc));
}
};
passHandle = handles.assign(unshared ? unsharedMarker : str);
handles.finish(passHandle);
return str;
Expand Down Expand Up @@ -3680,28 +3657,17 @@ private long readUTFSpan(StringBuilder sbuf, long utflen)
int b1, b2, b3;
b1 = buf[pos++] & 0xFF;
switch (b1 >> 4) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7: // 1 byte format: 0xxxxxxx
case 0, 1, 2, 3, 4, 5, 6, 7 -> // 1 byte format: 0xxxxxxx
cbuf[cpos++] = (char) b1;
break;

case 12:
case 13: // 2 byte format: 110xxxxx 10xxxxxx
case 12, 13 -> { // 2 byte format: 110xxxxx 10xxxxxx
b2 = buf[pos++];
if ((b2 & 0xC0) != 0x80) {
throw new UTFDataFormatException();
}
cbuf[cpos++] = (char) (((b1 & 0x1F) << 6) |
((b2 & 0x3F) << 0));
break;

case 14: // 3 byte format: 1110xxxx 10xxxxxx 10xxxxxx
}
case 14 -> { // 3 byte format: 1110xxxx 10xxxxxx 10xxxxxx
b3 = buf[pos + 1];
b2 = buf[pos + 0];
pos += 2;
Expand All @@ -3711,10 +3677,8 @@ private long readUTFSpan(StringBuilder sbuf, long utflen)
cbuf[cpos++] = (char) (((b1 & 0x0F) << 12) |
((b2 & 0x3F) << 6) |
((b3 & 0x3F) << 0));
break;

default: // 10xx xxxx, 1111 xxxx
throw new UTFDataFormatException();
}
default -> throw new UTFDataFormatException(); // 10xx xxxx, 1111 xxxx
}
}
} catch (ArrayIndexOutOfBoundsException ex) {
Expand Down Expand Up @@ -3748,19 +3712,11 @@ private int readUTFChar(StringBuilder sbuf, long utflen)
int b1, b2, b3;
b1 = readByte() & 0xFF;
switch (b1 >> 4) {
case 0:
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
case 7: // 1 byte format: 0xxxxxxx
case 0, 1, 2, 3, 4, 5, 6, 7 -> { // 1 byte format: 0xxxxxxx
sbuf.append((char) b1);
return 1;

case 12:
case 13: // 2 byte format: 110xxxxx 10xxxxxx
}
case 12, 13 -> { // 2 byte format: 110xxxxx 10xxxxxx
if (utflen < 2) {
throw new UTFDataFormatException();
}
Expand All @@ -3771,8 +3727,8 @@ private int readUTFChar(StringBuilder sbuf, long utflen)
sbuf.append((char) (((b1 & 0x1F) << 6) |
((b2 & 0x3F) << 0)));
return 2;

case 14: // 3 byte format: 1110xxxx 10xxxxxx 10xxxxxx
}
case 14 -> { // 3 byte format: 1110xxxx 10xxxxxx 10xxxxxx
if (utflen < 3) {
if (utflen == 2) {
readByte(); // consume remaining byte
Expand All @@ -3785,12 +3741,11 @@ private int readUTFChar(StringBuilder sbuf, long utflen)
throw new UTFDataFormatException();
}
sbuf.append((char) (((b1 & 0x0F) << 12) |
((b2 & 0x3F) << 6) |
((b2 & 0x3F) << 6) |
((b3 & 0x3F) << 0)));
return 3;

default: // 10xx xxxx, 1111 xxxx
throw new UTFDataFormatException();
}
default -> throw new UTFDataFormatException(); // 10xx xxxx, 1111 xxxx
}
}

Expand Down

1 comment on commit 4eb2168

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.