Skip to content

Commit 47c1b51

Browse files
remove more IOExceptions
Signed-off-by: Anthony Petrov <anthony@swirldslabs.com>
1 parent 6685bb5 commit 47c1b51

3 files changed

Lines changed: 34 additions & 36 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// SPDX-License-Identifier: Apache-2.0
2+
package com.hedera.pbj.runtime;
3+
4+
/**
5+
* Thrown during the UTF-8 encoding process when it is malformed.
6+
*/
7+
public class MalformedUtf8Exception extends RuntimeException {
8+
9+
/**
10+
* Construct new MalformedUtf8Exception
11+
*
12+
* @param message error message
13+
*/
14+
public MalformedUtf8Exception(final String message) {
15+
super(message);
16+
}
17+
18+
public MalformedUtf8Exception(final String message, final Throwable cause) {
19+
super(message, cause);
20+
}
21+
}

pbj-core/pbj-runtime/src/main/java/com/hedera/pbj/runtime/ProtoArrayWriterTools.java

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@
3232
import com.hedera.pbj.runtime.io.buffer.Bytes;
3333
import edu.umd.cs.findbugs.annotations.NonNull;
3434
import edu.umd.cs.findbugs.annotations.Nullable;
35-
import java.io.IOException;
3635
import java.lang.invoke.MethodHandles;
3736
import java.lang.invoke.VarHandle;
3837
import java.nio.ByteOrder;
@@ -138,15 +137,13 @@ public static int writeTag(
138137
* @param value the string value to write
139138
* @param skipDefault default value results in no-op for non-oneOf
140139
* @return the number of bytes written
141-
* @throws IOException If a I/O error occurs
142140
*/
143141
public static int writeString(
144142
@NonNull byte[] output,
145143
final int offset,
146144
@NonNull final FieldDefinition field,
147145
final String value,
148-
final boolean skipDefault)
149-
throws IOException {
146+
final boolean skipDefault) {
150147
assert field.type() == FieldType.STRING : "Not a string type " + field;
151148
assert !field.repeated() : "Use writeStringList with repeated types";
152149
return writeStringNoChecks(output, offset, field, value, skipDefault);
@@ -161,15 +158,13 @@ public static int writeString(
161158
* @param value the string value to write
162159
* @param skipDefault default value results in no-op for non-oneOf
163160
* @return the number of bytes written
164-
* @throws IOException If a I/O error occurs
165161
*/
166162
private static int writeStringNoChecks(
167163
@NonNull byte[] output,
168164
final int offset,
169165
@NonNull final FieldDefinition field,
170166
final String value,
171-
final boolean skipDefault)
172-
throws IOException {
167+
final boolean skipDefault) {
173168
int bytesWritten = 0;
174169
// When not a oneOf don't write default value
175170
if (skipDefault && !field.oneOf() && (value == null || value.isEmpty())) {
@@ -189,14 +184,12 @@ private static int writeStringNoChecks(
189184
* @param field the field definition for the string field
190185
* @param value the optional string value to write
191186
* @return the number of bytes written
192-
* @throws IOException If a I/O error occurs
193187
*/
194188
public static int writeOptionalString(
195189
@NonNull byte[] output,
196190
final int offset,
197191
@NonNull final FieldDefinition field,
198-
@Nullable final String value)
199-
throws IOException {
192+
@Nullable final String value) {
200193
int bytesWritten = 0;
201194
if (value != null) {
202195
bytesWritten += writeTag(output, offset, field, WIRE_TYPE_DELIMITED);
@@ -663,15 +656,13 @@ public static int writeEnum(
663656
* @param message the message to write
664657
* @param codec the codec for the given message type
665658
* @return the number of bytes written
666-
* @throws IOException If a I/O error occurs
667659
*/
668660
public static <T> int writeMessage(
669661
@NonNull final byte[] output,
670662
final int offset,
671663
@NonNull final FieldDefinition field,
672664
final T message,
673-
final Codec<T> codec)
674-
throws IOException {
665+
final Codec<T> codec) {
675666
assert field.type() == FieldType.MESSAGE : "Not a message type " + field;
676667
assert !field.repeated() : "Use writeMessageList with repeated types";
677668
return writeMessageNoChecks(output, offset, field, message, codec);
@@ -687,15 +678,13 @@ public static <T> int writeMessage(
687678
* @param message the message to write
688679
* @param codec the codec for the given message type
689680
* @return the number of bytes written
690-
* @throws IOException If a I/O error occurs
691681
*/
692682
private static <T> int writeMessageNoChecks(
693683
@NonNull final byte[] output,
694684
final int offset,
695685
@NonNull final FieldDefinition field,
696686
final T message,
697-
final Codec<T> codec)
698-
throws IOException {
687+
final Codec<T> codec) {
699688
// When not a oneOf don't write default value
700689
int bytesWritten = 0;
701690
if (field.oneOf() && message == null) {
@@ -722,15 +711,13 @@ private static <T> int writeMessageNoChecks(
722711
* @param value the bytes value to write
723712
* @param skipDefault default value results in no-op for non-oneOf
724713
* @return the number of bytes written
725-
* @throws IOException If a I/O error occurs
726714
*/
727715
public static int writeBytes(
728716
@NonNull final byte[] output,
729717
final int offset,
730718
@NonNull final FieldDefinition field,
731719
final Bytes value,
732-
boolean skipDefault)
733-
throws IOException {
720+
boolean skipDefault) {
734721
assert field.type() == FieldType.BYTES : "Not a byte[] type " + field;
735722
assert !field.repeated() : "Use writeBytesList with repeated types";
736723
return writeBytesNoChecks(output, offset, field, value, skipDefault);
@@ -745,15 +732,13 @@ public static int writeBytes(
745732
* @param value the bytes value to write
746733
* @param skipZeroLength this is true for normal single bytes and false for repeated lists
747734
* @return the number of bytes written
748-
* @throws IOException If a I/O error occurs
749735
*/
750736
private static int writeBytesNoChecks(
751737
@NonNull final byte[] output,
752738
final int offset,
753739
@NonNull final FieldDefinition field,
754740
final Bytes value,
755-
final boolean skipZeroLength)
756-
throws IOException {
741+
final boolean skipZeroLength) {
757742
// When not a oneOf don't write default value
758743
if (!field.oneOf() && (skipZeroLength && (value.length() == 0))) {
759744
return 0;
@@ -1100,11 +1085,9 @@ public static int writeEnumList(
11001085
* @param field the descriptor for the field we are writing
11011086
* @param list the list of strings value to write
11021087
* @return the number of bytes written
1103-
* @throws IOException If a I/O error occurs
11041088
*/
11051089
public static int writeStringList(
1106-
@NonNull final byte[] output, final int offset, FieldDefinition field, List<String> list)
1107-
throws IOException {
1090+
@NonNull final byte[] output, final int offset, FieldDefinition field, List<String> list) {
11081091
assert field.type() == FieldType.STRING : "Not a string type " + field;
11091092
assert field.repeated() : "Use writeString with non-repeated types";
11101093
// When not a oneOf don't write default value
@@ -1131,12 +1114,10 @@ public static int writeStringList(
11311114
* @param list the list of messages value to write
11321115
* @param codec the codec for the message type
11331116
* @return the number of bytes written
1134-
* @throws IOException If a I/O error occurs
11351117
* @param <T> type of message
11361118
*/
11371119
public static <T> int writeMessageList(
1138-
@NonNull final byte[] output, final int offset, FieldDefinition field, List<T> list, Codec<T> codec)
1139-
throws IOException {
1120+
@NonNull final byte[] output, final int offset, FieldDefinition field, List<T> list, Codec<T> codec) {
11401121
assert field.type() == FieldType.MESSAGE : "Not a message type " + field;
11411122
assert field.repeated() : "Use writeMessage with non-repeated types";
11421123
// When not a oneOf don't write default value
@@ -1159,11 +1140,9 @@ public static <T> int writeMessageList(
11591140
* @param field the descriptor for the field we are writing
11601141
* @param list the list of bytes objects value to write
11611142
* @return the number of bytes written
1162-
* @throws IOException If a I/O error occurs
11631143
*/
11641144
public static int writeBytesList(
1165-
@NonNull final byte[] output, final int offset, FieldDefinition field, List<? extends Bytes> list)
1166-
throws IOException {
1145+
@NonNull final byte[] output, final int offset, FieldDefinition field, List<? extends Bytes> list) {
11671146
assert field.type() == FieldType.BYTES : "Not a message type " + field;
11681147
assert field.repeated() : "Use writeBytes with non-repeated types";
11691148
// When not a oneOf don't write default value

pbj-core/pbj-runtime/src/main/java/com/hedera/pbj/runtime/Utf8Tools.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,9 @@ static void encodeUtf8(final CharSequence in, final WritableSequentialData out)
123123
* @param offset The offset in the byte array to start writing at
124124
* @param in The input character sequence to encode
125125
* @return The number of bytes written
126-
* @throws MalformedProtobufException if the input contains unpaired surrogates
126+
* @throws MalformedUtf8Exception if the input contains unpaired surrogates
127127
*/
128-
public static int encodeUtf8(@NonNull final byte[] out, final int offset, final String in)
129-
throws MalformedProtobufException {
128+
public static int encodeUtf8(@NonNull final byte[] out, final int offset, final String in) {
130129
int utf16Length = in.length();
131130
int i = 0;
132131
int j = offset;
@@ -156,8 +155,7 @@ public static int encodeUtf8(@NonNull final byte[] out, final int offset, final
156155
// four UTF-8 bytes
157156
final char low;
158157
if (i + 1 == in.length() || !Character.isSurrogatePair(c, (low = in.charAt(++i)))) {
159-
throw new MalformedProtobufException(
160-
"Unpaired surrogate at index " + (i - 1) + " of " + utf16Length);
158+
throw new MalformedUtf8Exception("Unpaired surrogate at index " + (i - 1) + " of " + utf16Length);
161159
}
162160
int codePoint = Character.toCodePoint(c, low);
163161
out[j++] = (byte) ((0xF << 4) | (codePoint >>> 18));

0 commit comments

Comments
 (0)