Skip to content

Commit 3fa9866

Browse files
Merge pull request #239 from mh-northlander/feature/238-align-byteorder
Set `ByteOrder.LITTLE_ENDIAN` for ByteBuffer from `Config.Resource.asByteBuffer`
2 parents f8f470b + 8b540b1 commit 3fa9866

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/main/java/com/worksap/nlp/sudachi/Config.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.lang.reflect.InvocationTargetException;
3030
import java.net.URL;
3131
import java.nio.ByteBuffer;
32+
import java.nio.ByteOrder;
3233
import java.nio.file.Files;
3334
import java.nio.file.Path;
3435
import java.util.*;
@@ -866,7 +867,8 @@ public InputStream asInputStream() throws IOException {
866867
/**
867868
* Get view of this resource as a ByteBuffer. When it is possible, the data will
868869
* be memory mapped, if it is not possible, it will be fully read into the
869-
* memory. Will not work for files more than 2^31 bytes (2 GB) in size.
870+
* memory. Will not work for files more than 2^31 bytes (2 GB) in size. The
871+
* ByteOrder is set to little endian.
870872
*
871873
* @return ByteBuffer containing the whole contents of the file
872874
* @throws IOException

src/main/java/com/worksap/nlp/sudachi/StringUtil.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.InputStreamReader;
2222
import java.net.URL;
2323
import java.nio.ByteBuffer;
24+
import java.nio.ByteOrder;
2425
import java.nio.CharBuffer;
2526
import java.nio.charset.StandardCharsets;
2627
import java.nio.file.Files;
@@ -56,12 +57,20 @@ public static String readFully(InputStream stream) throws IOException {
5657
}
5758

5859
public static ByteBuffer readAllBytes(URL url) throws IOException {
60+
return readAllBytes(url, ByteOrder.LITTLE_ENDIAN);
61+
}
62+
63+
public static ByteBuffer readAllBytes(URL url, ByteOrder order) throws IOException {
5964
try (InputStream is = url.openStream()) {
60-
return readAllBytes(is);
65+
return readAllBytes(is, order);
6166
}
6267
}
6368

6469
public static ByteBuffer readAllBytes(InputStream inputStream) throws IOException {
70+
return readAllBytes(inputStream, ByteOrder.LITTLE_ENDIAN);
71+
}
72+
73+
public static ByteBuffer readAllBytes(InputStream inputStream, ByteOrder order) throws IOException {
6574
byte[] buffer = new byte[inputStream.available() + 1024];
6675
int offset = 0;
6776

@@ -78,6 +87,7 @@ public static ByteBuffer readAllBytes(InputStream inputStream) throws IOExceptio
7887
}
7988
ByteBuffer bbuf = ByteBuffer.wrap(buffer);
8089
bbuf.limit(offset);
90+
bbuf.order(order);
8191
return bbuf;
8292
}
8393
}

0 commit comments

Comments
 (0)