Skip to content

Commit

Permalink
TODO: Fix HttpServer (again)
Browse files Browse the repository at this point in the history
  • Loading branch information
Lauriichan committed Aug 15, 2021
1 parent fa15d96 commit 2dfa93a
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
public class NbtSerializer implements Serializer<NbtNamedTag> {

public static final NbtSerializer COMPRESSED = new NbtSerializer(true);
public static final NbtSerializer UNCOMPRESSED = new NbtSerializer(true);
public static final NbtSerializer UNCOMPRESSED = new NbtSerializer(false);

private final boolean compress;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.syntaxphoenix.syntaxapi.net.http;

import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintStream;
Expand Down Expand Up @@ -245,11 +246,16 @@ protected void handleClientAsync(Socket socket) throws Throwable {

int length = ((Number) request.getHeader("Content-Length")).intValue();

StringBuilder builder = new StringBuilder();
for (int index = 0; index < length; index++) {
builder.append((char) reader.read());
ByteArrayOutputStream array = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int remain = length;
while (remain != 0) {
int amount = remain > 1024 ? 1024 : remain;
stream.read(buffer, 0, amount);
array.write(buffer, 0, amount);
remain -= amount;
}
String data = builder.toString();
byte[] data = array.toByteArray();
if (serializer != null) {
try {
request.setData(serializer.serialize(data));
Expand All @@ -263,7 +269,7 @@ protected void handleClientAsync(Socket socket) throws Throwable {
return;
}
} else {
request.setData(new CustomRequestData<>(String.class, data));
request.setData(new CustomRequestData<>(String.class, new String(data, StandardCharsets.UTF_8)));
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.syntaxphoenix.syntaxapi.net.http;

@FunctionalInterface
public interface RequestSerializer {

public RequestData<?> serialize(String data) throws Exception;

}
package com.syntaxphoenix.syntaxapi.net.http;

@FunctionalInterface
public interface RequestSerializer {

public RequestData<?> serialize(byte[] data) throws Exception;

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.syntaxphoenix.syntaxapi.net.http;

import java.nio.charset.StandardCharsets;

@FunctionalInterface
public interface RequestTextSerializer extends RequestSerializer {

default RequestData<?> serialize(byte[] data) throws Exception {
return serialize(new String(data, StandardCharsets.UTF_8));
}

RequestData<?> serialize(String data) throws Exception;

}
4 changes: 2 additions & 2 deletions net/src/test/java/net/test/RestSerializer.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@
import com.syntaxphoenix.syntaxapi.json.io.JsonParser;
import com.syntaxphoenix.syntaxapi.net.http.CustomRequestData;
import com.syntaxphoenix.syntaxapi.net.http.RequestData;
import com.syntaxphoenix.syntaxapi.net.http.RequestSerializer;
import com.syntaxphoenix.syntaxapi.net.http.RequestTextSerializer;
import com.syntaxphoenix.syntaxapi.utils.java.Files;

public class RestSerializer implements RequestSerializer {
public class RestSerializer implements RequestTextSerializer {

private final JsonParser parser = new JsonParser();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,25 +61,22 @@ public static <E> E[] subArray(IntFunction<E[]> function, E[] args, int index, i
}

public static Object[][] partition(Object[] args, int length) {
int size = (int) Math.floor(args.length / (float) length);
if (args.length % length != 0) {
size++;
}
Object[][] output = new Object[size][length];
for (int index = 0; index < size; index++) {
System.arraycopy(args, index * length, output[index], 0, length);
}
return output;
return partition((a, b) -> new Object[a][b], args, length);
}

public static <E> E[][] partition(BiFunction<Integer, Integer, E[][]> function, E[] args, int length) {
int size = (int) Math.floor(args.length / (float) length);
if (args.length % length != 0) {
size++;
int amount = (int) Math.floor((double) args.length / length);
int size = args.length % length;
if (size != 0) {
amount++;
}
E[][] output = function.apply(size, length);
for (int index = 0; index < size; index++) {
System.arraycopy(args, index * length, output[index], 0, length);
E[][] output = function.apply(amount, length);
for (int index = 0; index < amount; index++) {
if (index != amount + 1) {
System.arraycopy(args, index * length, output[index], 0, length);
continue;
}
System.arraycopy(args, index * length, output[index], 0, size);
}
return output;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import com.syntaxphoenix.syntaxapi.json.value.JsonNull;
import com.syntaxphoenix.syntaxapi.json.value.JsonShort;
import com.syntaxphoenix.syntaxapi.json.value.JsonString;
import com.syntaxphoenix.syntaxapi.utils.java.Exceptions;
import com.syntaxphoenix.syntaxapi.utils.java.Primitives;

public abstract class JsonValue<E> {
Expand Down Expand Up @@ -79,6 +80,7 @@ public String toString() {
try {
return UNPRETTY.toString(this);
} catch (IOException e) {
System.out.println(Exceptions.stackTraceToString(e));
return "";
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,7 @@ public JsonWriter setTabIndent(int indent) {

@Override
public void toWriter(JsonValue<?> object, Writer writer) throws IOException {
switch (object.getType()) {
case ARRAY:
writeArray((JsonArray) object, writer, 0);
break;
case OBJECT:
writeObject((JsonObject) object, writer, 0);
break;
default:
break;
}
writeValue(object, writer, 0);
}

public void writeEntry(JsonEntry<?> entry, Writer writer, int depth) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.syntaxphoenix.syntaxapi.test.json;

import java.io.IOException;

import org.junit.jupiter.api.Test;

import com.syntaxphoenix.syntaxapi.json.io.JsonWriter;
import com.syntaxphoenix.syntaxapi.json.value.JsonInteger;
import com.syntaxphoenix.syntaxapi.json.value.JsonString;

public class JsonValueTest {

@Test
public void tryJsonWriter() throws IOException {

System.out.println("== Write Test ==\n");

JsonWriter writer = new JsonWriter().setIndent(2).setSpaces(true);

writer.toStream(new JsonInteger(152), System.out);
writer.toStream(new JsonString("Test"), System.out);

System.out.println("\n== Write Test ==");

}

}

0 comments on commit 2dfa93a

Please sign in to comment.