Skip to content

Commit

Permalink
Merge pull request #239 from mh-northlander/feature/238-align-byteorder
Browse files Browse the repository at this point in the history
Set `ByteOrder.LITTLE_ENDIAN` for ByteBuffer from `Config.Resource.asByteBuffer`
  • Loading branch information
mh-northlander authored Nov 5, 2024
2 parents f8f470b + 8b540b1 commit 3fa9866
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
4 changes: 3 additions & 1 deletion src/main/java/com/worksap/nlp/sudachi/Config.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.lang.reflect.InvocationTargetException;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
Expand Down Expand Up @@ -866,7 +867,8 @@ public InputStream asInputStream() throws IOException {
/**
* Get view of this resource as a ByteBuffer. When it is possible, the data will
* be memory mapped, if it is not possible, it will be fully read into the
* memory. Will not work for files more than 2^31 bytes (2 GB) in size.
* memory. Will not work for files more than 2^31 bytes (2 GB) in size. The
* ByteOrder is set to little endian.
*
* @return ByteBuffer containing the whole contents of the file
* @throws IOException
Expand Down
12 changes: 11 additions & 1 deletion src/main/java/com/worksap/nlp/sudachi/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.CharBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand Down Expand Up @@ -56,12 +57,20 @@ public static String readFully(InputStream stream) throws IOException {
}

public static ByteBuffer readAllBytes(URL url) throws IOException {
return readAllBytes(url, ByteOrder.LITTLE_ENDIAN);
}

public static ByteBuffer readAllBytes(URL url, ByteOrder order) throws IOException {
try (InputStream is = url.openStream()) {
return readAllBytes(is);
return readAllBytes(is, order);
}
}

public static ByteBuffer readAllBytes(InputStream inputStream) throws IOException {
return readAllBytes(inputStream, ByteOrder.LITTLE_ENDIAN);
}

public static ByteBuffer readAllBytes(InputStream inputStream, ByteOrder order) throws IOException {
byte[] buffer = new byte[inputStream.available() + 1024];
int offset = 0;

Expand All @@ -78,6 +87,7 @@ public static ByteBuffer readAllBytes(InputStream inputStream) throws IOExceptio
}
ByteBuffer bbuf = ByteBuffer.wrap(buffer);
bbuf.limit(offset);
bbuf.order(order);
return bbuf;
}
}

0 comments on commit 3fa9866

Please sign in to comment.