Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
Unknow authored and Unknow committed Oct 15, 2024
1 parent 7dd06f3 commit 8cacc5b
Show file tree
Hide file tree
Showing 14 changed files with 170 additions and 258 deletions.
32 changes: 18 additions & 14 deletions unknow-server-jaxrs/src/main/java/io/protostuff/JsonXIOUtil2.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;

public class JsonXIOUtil2 {
Expand Down Expand Up @@ -64,13 +65,16 @@ public static <T> void writeListTo(OutputStream out, Collection<T> messages, Sch
throw new IllegalArgumentException("Buffer previously used and had not been reset.");

if (messages.isEmpty()) {
System.arraycopy(EMPTY_ARRAY, 0, buffer.buffer, buffer.offset, EMPTY_ARRAY.length);
buffer.offset += EMPTY_ARRAY.length;
byte[] b = fmt.start();
System.arraycopy(b, 0, buffer.buffer, buffer.offset, b.length);
buffer.offset += b.length;
b = fmt.end();
System.arraycopy(b, 0, buffer.buffer, buffer.offset, b.length);
buffer.offset += b.length;
return;
}

final JsonXOutput output = new JsonXOutput(buffer, out, numeric, schema);

boolean first = true;
for (T m : messages) {
if (first) {
Expand Down Expand Up @@ -105,34 +109,34 @@ public static <T> void mergeFrom(byte[] data, int offset, int length, T message,
*/
public static <T> void mergeFrom(InputStream in, T message, Schema<T> schema, boolean numeric) throws IOException {
JsonXInput input = new JsonXInput(in, numeric);
input.readStartObject();
input.readNext('{');
schema.mergeFrom(input, message);
}

/**
* Parses the {@code messages} from the stream using the given {@code schema}.
*/
public static <T> List<T> parseListFrom(InputStream in, Schema<T> schema, boolean numeric) throws IOException {
public static <T> List<T> parseListFrom(InputStream in, Schema<T> schema, boolean numeric, ListFormat fmt) throws IOException {
final JsonXInput input = new JsonXInput(in, numeric);
input.readStartArray();

final List<T> list = new ArrayList<T>();
if (input.isNext(']'))
return list;
input.readNext(fmt.start());

do {
input.readStartObject();
if (input.isNext(fmt.end()))
return Collections.emptyList();

final List<T> list = new ArrayList<T>();
do {
input.readNext('{');
final T message = schema.newMessage();
schema.mergeFrom(input, message);
list.add(message);
input.reset();
} while (input.isNext(','));
input.readEndArray();
} while (input.isNext(fmt.delimiter));
input.readNext(fmt.end());
return list;
}

public static enum ListFormat {
public enum ListFormat {
JSON(new byte[] { '[' }, new byte[] { ',' }, new byte[] { ']' }), NDJSON(new byte[] {}, new byte[] { '\n' }, new byte[] {});

private final byte[] start;
Expand Down
44 changes: 25 additions & 19 deletions unknow-server-jaxrs/src/main/java/io/protostuff/JsonXInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,6 @@ public int getLastNumber() {
return lastNumber;
}

public void readStartObject() throws IOException {
readNext('{');
}

public void readEndObject() throws IOException {
readNext('}');
}

public void readStartArray() throws IOException {
readNext('[');
}

public void readEndArray() throws IOException {
readNext(']');
}

public void reset() {
lastRepeated = false;
lastSchema = null;
Expand Down Expand Up @@ -253,7 +237,7 @@ public long readUInt64() throws IOException {

@Override
public <T> T mergeObject(T value, final Schema<T> schema) throws IOException {
readStartObject();
readNext('{');

final int previousNumber = this.lastNumber;
final boolean previousRepeated = this.lastRepeated;
Expand Down Expand Up @@ -353,7 +337,7 @@ private boolean readNull() throws IOException {
return false;
}

private void throwUnexpectedContent(char expected, int actual) throws JsonInputException {
private void throwUnexpectedContent(int expected, int actual) throws JsonInputException {
throwUnexpectedContent(Character.toString(expected), actual == -1 ? "EOF" : Character.toString(actual));
}

Expand All @@ -368,12 +352,34 @@ private void throwEOF() throws JsonInputException {
throwUnexpectedContent("data", "EOF");
}

private void readNext(char expected) throws IOException {
public void readNext(char expected) throws IOException {
byte b = readNext();
if (expected != b)
throwUnexpectedContent(expected, b);
}

public void readNext(byte[] expected) throws IOException {
byte b = readNext();
checkBuffer(expected.length);
for (int i = 0; i < expected.length; i++) {
if (b != expected[i])
throwUnexpectedContent(expected[i], b);
if (++o == l)
throwEOF();
}
}

public boolean isNext(byte[] c) throws IOException {
if (c.length == 0)
return false;
readNext();
--o;
checkBuffer(c.length);
if (l - o < c.length)
return false;
return Arrays.equals(c, 0, c.length, buf, o, c.length);
}

public boolean isNext(char c) {
return buf[o] == c;
}
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Loading

0 comments on commit 8cacc5b

Please sign in to comment.