26
26
import java .io .IOException ;
27
27
import java .io .Writer ;
28
28
import java .util .Arrays ;
29
+ import java .util .Objects ;
29
30
import org .jetbrains .annotations .NotNull ;
30
31
31
32
/**
@@ -37,17 +38,29 @@ public final class TagStringIO {
37
38
private static final TagStringIO INSTANCE = new TagStringIO (new Builder ());
38
39
39
40
/**
40
- * Get an instance of {@link TagStringIO} that creates reads and writes using standard options.
41
+ * Get an instance of {@link TagStringIO} that reads and writes using standard options.
41
42
*
42
43
* @return the basic instance
43
44
* @since 4.0.0
45
+ * @deprecated For removal since 4.22.0, use {@link #tagStringIO()} instead
44
46
*/
47
+ @ Deprecated
45
48
public static @ NotNull TagStringIO get () {
49
+ return tagStringIO ();
50
+ }
51
+
52
+ /**
53
+ * Gets an instance of {@link TagStringIO} that reads and writes using standard options.
54
+ *
55
+ * @return the basic instance
56
+ * @since 4.22.0
57
+ */
58
+ public static @ NotNull TagStringIO tagStringIO () {
46
59
return INSTANCE ;
47
60
}
48
61
49
62
/**
50
- * Create an new builder to configure IO.
63
+ * Create a new builder to configure IO.
51
64
*
52
65
* @return a builder
53
66
* @since 4.0.0
@@ -77,7 +90,8 @@ private TagStringIO(final @NotNull Builder builder) {
77
90
* @throws IOException on any syntax errors
78
91
* @since 4.0.0
79
92
*/
80
- public CompoundBinaryTag asCompound (final String input ) throws IOException {
93
+ public @ NotNull CompoundBinaryTag asCompound (final @ NotNull String input ) throws IOException {
94
+ Objects .requireNonNull (input , "input" );
81
95
try {
82
96
final CharBuffer buffer = new CharBuffer (input );
83
97
final TagStringReader parser = new TagStringReader (buffer );
@@ -92,6 +106,81 @@ public CompoundBinaryTag asCompound(final String input) throws IOException {
92
106
}
93
107
}
94
108
109
+ /**
110
+ * Read the string into a tag.
111
+ *
112
+ * <p>When working with untrusted input (such as from the network), users should be careful
113
+ * to validate that the {@code input} string is of a reasonable size.</p>
114
+ *
115
+ * @param input Input data
116
+ * @return the parsed tag
117
+ * @throws IOException on any syntax errors
118
+ * @since 4.22.0
119
+ */
120
+ public @ NotNull BinaryTag asTag (final @ NotNull String input ) throws IOException {
121
+ Objects .requireNonNull (input , "input" );
122
+ try {
123
+ final CharBuffer buffer = new CharBuffer (input );
124
+ final TagStringReader parser = new TagStringReader (buffer );
125
+ parser .legacy (this .acceptLegacy );
126
+ final BinaryTag tag = parser .tag ();
127
+ if (buffer .skipWhitespace ().hasMore ()) {
128
+ throw new IOException ("Document had trailing content after first Tag" );
129
+ }
130
+ return tag ;
131
+ } catch (final StringTagParseException ex ) {
132
+ throw new IOException (ex );
133
+ }
134
+ }
135
+
136
+ /**
137
+ * Read the string into an embedded compound tag, returning the remainder of the input.
138
+ *
139
+ * @param input the input string
140
+ * @param remainder the appendable to write the remainder to
141
+ * @return the parsed tag with the remainder
142
+ * @throws IOException on any syntax errors
143
+ * @since 4.22.0
144
+ */
145
+ public @ NotNull CompoundBinaryTag asCompound (final @ NotNull String input , final @ NotNull Appendable remainder ) throws IOException {
146
+ Objects .requireNonNull (input , "input" );
147
+ Objects .requireNonNull (remainder , "remainder" );
148
+ try {
149
+ final CharBuffer buffer = new CharBuffer (input );
150
+ final TagStringReader parser = new TagStringReader (buffer );
151
+ parser .legacy (this .acceptLegacy );
152
+ final CompoundBinaryTag tag = parser .compound ();
153
+ remainder .append (buffer .takeRest ());
154
+ return tag ;
155
+ } catch (final StringTagParseException ex ) {
156
+ throw new IOException (ex );
157
+ }
158
+ }
159
+
160
+ /**
161
+ * Read the string into an embedded tag, returning the remainder of the input.
162
+ *
163
+ * @param input the input string
164
+ * @param remainder the appendable to write the remainder to
165
+ * @return the parsed tag with the remainder
166
+ * @throws IOException on any syntax errors
167
+ * @since 4.22.0
168
+ */
169
+ public @ NotNull BinaryTag asTag (final @ NotNull String input , final @ NotNull Appendable remainder ) throws IOException {
170
+ Objects .requireNonNull (input , "input" );
171
+ Objects .requireNonNull (remainder , "remainder" );
172
+ try {
173
+ final CharBuffer buffer = new CharBuffer (input );
174
+ final TagStringReader parser = new TagStringReader (buffer );
175
+ parser .legacy (this .acceptLegacy );
176
+ final BinaryTag tag = parser .tag ();
177
+ remainder .append (buffer .takeRest ());
178
+ return tag ;
179
+ } catch (final StringTagParseException ex ) {
180
+ throw new IOException (ex );
181
+ }
182
+ }
183
+
95
184
/**
96
185
* Get a string representation of the provided tag.
97
186
*
@@ -100,7 +189,7 @@ public CompoundBinaryTag asCompound(final String input) throws IOException {
100
189
* @throws IOException if any errors occur writing to string
101
190
* @since 4.0.0
102
191
*/
103
- public String asString (final CompoundBinaryTag input ) throws IOException {
192
+ public @ NotNull String asString (final @ NotNull CompoundBinaryTag input ) throws IOException {
104
193
return this .asString ((BinaryTag ) input );
105
194
}
106
195
@@ -112,7 +201,8 @@ public String asString(final CompoundBinaryTag input) throws IOException {
112
201
* @throws IOException if any errors occur writing to string
113
202
* @since 4.20.0
114
203
*/
115
- public String asString (final BinaryTag input ) throws IOException {
204
+ public @ NotNull String asString (final @ NotNull BinaryTag input ) throws IOException {
205
+ Objects .requireNonNull (input , "input" );
116
206
final StringBuilder sb = new StringBuilder ();
117
207
try (final TagStringWriter emit = new TagStringWriter (sb , this .indent )) {
118
208
emit .legacy (this .emitLegacy );
@@ -122,7 +212,7 @@ public String asString(final BinaryTag input) throws IOException {
122
212
}
123
213
124
214
/**
125
- * Writes a tag to in string format.
215
+ * Writes a compound tag to in string format.
126
216
*
127
217
* <p>The provided {@link Writer} will remain open after reading a tag.</p>
128
218
*
@@ -131,7 +221,23 @@ public String asString(final BinaryTag input) throws IOException {
131
221
* @throws IOException if any IO or syntax errors occur while parsing
132
222
* @since 4.0.0
133
223
*/
134
- public void toWriter (final CompoundBinaryTag input , final Writer dest ) throws IOException {
224
+ public void toWriter (final @ NotNull CompoundBinaryTag input , final @ NotNull Writer dest ) throws IOException {
225
+ this .toWriter ((BinaryTag ) input , dest );
226
+ }
227
+
228
+ /**
229
+ * Writes a tag to in string format.
230
+ *
231
+ * <p>The provided {@link Writer} will remain open after reading a tag.</p>
232
+ *
233
+ * @param input Tag to write
234
+ * @param dest Writer to write to
235
+ * @throws IOException if any IO or syntax errors occur while parsing
236
+ * @since 4.22.0
237
+ */
238
+ public void toWriter (final @ NotNull BinaryTag input , final @ NotNull Writer dest ) throws IOException {
239
+ Objects .requireNonNull (input , "input" );
240
+ Objects .requireNonNull (dest , "dest" );
135
241
try (final TagStringWriter emit = new TagStringWriter (dest , this .indent )) {
136
242
emit .legacy (this .emitLegacy );
137
243
emit .writeTag (input );
@@ -163,7 +269,7 @@ public static class Builder {
163
269
public @ NotNull Builder indent (final int spaces ) {
164
270
if (spaces == 0 ) {
165
271
this .indent = "" ;
166
- } else if ((this .indent .length () > 0 && this .indent .charAt (0 ) != ' ' ) || spaces != this .indent .length ()) {
272
+ } else if ((! this .indent .isEmpty () && this .indent .charAt (0 ) != ' ' ) || spaces != this .indent .length ()) {
167
273
final char [] indent = new char [spaces ];
168
274
Arrays .fill (indent , ' ' );
169
275
this .indent = String .copyValueOf (indent );
@@ -183,7 +289,7 @@ public static class Builder {
183
289
public @ NotNull Builder indentTab (final int tabs ) {
184
290
if (tabs == 0 ) {
185
291
this .indent = "" ;
186
- } else if ((this .indent .length () > 0 && this .indent .charAt (0 ) != '\t' ) || tabs != this .indent .length ()) {
292
+ } else if ((! this .indent .isEmpty () && this .indent .charAt (0 ) != '\t' ) || tabs != this .indent .length ()) {
187
293
final char [] indent = new char [tabs ];
188
294
Arrays .fill (indent , '\t' );
189
295
this .indent = String .copyValueOf (indent );
0 commit comments