Skip to content

Commit

Permalink
use IOException
Browse files Browse the repository at this point in the history
  • Loading branch information
davidmoten committed May 13, 2024
1 parent 5eab35f commit dd00e7e
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ public int read() throws IOException {
throw new EOFException();
}
if (!isContinuation(a)) {
throw new IllegalStateException(
throw new IOException(
"wrong continuation bits, bytes after first in a UTF-8 character must start with bits 10");
}
bb.put(a);
Expand Down Expand Up @@ -96,12 +96,12 @@ private static boolean isContinuation(int a) {
}

// VisibleForTesting
static int numBytes(int a) {
static int numBytes(int a) throws IOException {
if (!bit(a, 1)) {
return 1;
} else {
if (!bit(a, 2)) {
throw new IllegalStateException("leading bits 10 illegal for first byte of UTF-8 character");
throw new IOException("leading bits 10 illegal for first byte of UTF-8 character");
} else if (!bit(a, 3)) {
return 2;
} else {
Expand All @@ -111,7 +111,7 @@ static int numBytes(int a) {
if (!bit(a, 5)) {
return 4;
} else {
throw new IllegalStateException("leading bits 11111 illegal for first byte of UTF-8 character");
throw new IOException("leading bits 11111 illegal for first byte of UTF-8 character");
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ public void testNotContinuation() throws IOException {
byte[] bytes = "£".getBytes(StandardCharsets.UTF_8);
byte[] b = new byte[] { bytes[0], bytes[0] };
try (Reader r = new Utf8InputStreamReader(new ByteArrayInputStream(b))) {
assertThrows(IllegalStateException.class, () -> r.read());
assertThrows(IOException.class, () -> r.read());
}
}

Expand All @@ -73,7 +73,7 @@ public void testNotContinuation2() throws IOException {
byte[] bytes = "£".getBytes(StandardCharsets.UTF_8);
byte[] b = new byte[] { bytes[0], '$' };
try (Reader r = new Utf8InputStreamReader(new ByteArrayInputStream(b))) {
assertThrows(IllegalStateException.class, () -> r.read());
assertThrows(IOException.class, () -> r.read());
}
}

Expand All @@ -82,20 +82,20 @@ public void testContinuationByteCannotBeFirstByte() throws IOException {
byte[] bytes = "£".getBytes(StandardCharsets.UTF_8);
byte[] b = new byte[] { bytes[1] };
try (Reader r = new Utf8InputStreamReader(new ByteArrayInputStream(b))) {
assertThrows(IllegalStateException.class, () -> r.read());
assertThrows(IOException.class, () -> r.read());
}
}

@Test
public void testUtf8ByteHasTooManyLeadingOnes() throws IOException {
byte[] b = new byte[] { (byte) 248 };
try (Reader r = new Utf8InputStreamReader(new ByteArrayInputStream(b))) {
assertThrows(IllegalStateException.class, () -> r.read());
assertThrows(IOException.class, () -> r.read());
}
}

@Test
public void testNumBytes() {
public void testNumBytes() throws IOException {
assertEquals(1, numBytes('$'));
}

Expand Down

0 comments on commit dd00e7e

Please sign in to comment.