Skip to content

Commit

Permalink
Remove BOM
Browse files Browse the repository at this point in the history
  • Loading branch information
Scoin0 committed Mar 20, 2024
2 parents 1cdd9a6 + 154aa07 commit aae0446
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 27 deletions.
2 changes: 0 additions & 2 deletions src/main/java/usagibot/UsagiBot.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ public class UsagiBot {
static PircBotX bot;
static MemoryReaderConnections memoryReader;

private static boolean isRunning = false;

public static Configuration getConfig() {
return config;
}
Expand Down
35 changes: 25 additions & 10 deletions src/main/java/usagibot/osu/memreaders/MemoryReaderConnections.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
import usagibot.osu.memreaders.streamcompanion.StreamCompanionReader;
import usagibot.osu.memreaders.tosu.TOsuReader;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URI;
import java.net.URISyntaxException;
Expand Down Expand Up @@ -113,18 +111,35 @@ public static String fetchJsonData(String apiUrl) throws IOException {
connection.setRequestProperty("Accept", "application/json");

// Read the response
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {
StringBuilder response = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
response.append(line);
try (InputStream inputStream = connection.getInputStream()) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
byte[] buffer = new byte[4096];
int bytesRead;
boolean bomSkipped = false;

// Check for BOM (Byte Order Mark)
while ((bytesRead = inputStream.read(buffer)) != -1) {
if (!bomSkipped) {
if (bytesRead >= 3 && buffer[0] == (byte) 0xEF && buffer[1] == (byte) 0xBB && buffer[2] == (byte) 0xBF) {
// Skip BOM
outputStream.write(buffer, 3, bytesRead - 3);
bomSkipped = true;
continue;
} else {
// No BOM found, write the buffer as is
outputStream.write(buffer, 0, bytesRead);
bomSkipped = true;
continue;
}
}
outputStream.write(buffer, 0, bytesRead);
}
return response.toString();

return outputStream.toString("UTF-8");
} finally {
connection.disconnect();
}
}

public static String fetchJsonDataWebSocket(String webSocketURI) {
try {
WebSocketConnector webSocketConnector = new WebSocketConnector(new URI(webSocketURI));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ public class StreamCompanionModel {
public long score;
@JsonProperty("currentMaxCombo")
public int currentMaxCombo;
@JsonProperty("playerHp")
public float playerHp;
// Ignore Player HP
@JsonProperty("ppIfMapEndsNow")
public float ppIfMapEndsNow;
// Ignore Aim PP If Map Ends Now
Expand Down Expand Up @@ -155,7 +154,7 @@ public class StreamCompanionModel {
@JsonProperty("minBpm")
public float minBPM;
@JsonProperty("bpm")
public float bpm;
public String bpm;
@JsonProperty("mainBpm")
public float mainBPM;
// Ignore Tags
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
package usagibot.osu.memreaders.streamcompanion;

import com.fasterxml.jackson.databind.ObjectMapper;
import kotlin.text.Charsets;
import lombok.extern.slf4j.Slf4j;
import usagibot.UsagiBot;
import usagibot.osu.api.Beatmap;
import usagibot.osu.memreaders.IMemoryReader;
import usagibot.osu.memreaders.MemoryReaderConnections;

import java.io.IOException;

@Slf4j
public class StreamCompanionReader implements IMemoryReader {

static Beatmap beatmap;
Expand All @@ -16,7 +18,6 @@ public class StreamCompanionReader implements IMemoryReader {
public Beatmap getSong() {
try {
String json = MemoryReaderConnections.fetchJsonData(MemoryReaderConnections.webHookPath);
json = removeBOM(json);
ObjectMapper mapper = new ObjectMapper();
StreamCompanionModel model = mapper.readValue(json, StreamCompanionModel.class);
String beatmapId = String.valueOf(model.mapId);
Expand All @@ -31,7 +32,6 @@ public Beatmap getSong() {
public String getMods() {
try {
String json = MemoryReaderConnections.fetchJsonData(MemoryReaderConnections.webHookPath);
json = removeBOM(json);
ObjectMapper mapper = new ObjectMapper();
StreamCompanionModel model = mapper.readValue(json, StreamCompanionModel.class);
String mods = String.valueOf(model.mods);
Expand All @@ -44,12 +44,4 @@ public String getMods() {
return null;
}
}

private String removeBOM(String json) {
if (json != null && json.startsWith("\uFEFF")) {
return json.substring(1);
}
return json;
}

}
}

0 comments on commit aae0446

Please sign in to comment.