Skip to content

Commit

Permalink
add byteorder arg to readAllBytes
Browse files Browse the repository at this point in the history
  • Loading branch information
mh-northlander committed Oct 24, 2024
1 parent 297aace commit ecb02df
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
6 changes: 4 additions & 2 deletions 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 Expand Up @@ -958,7 +960,7 @@ public ByteBuffer asByteBuffer() throws IOException {
if (Objects.equals(url.getProtocol(), "file")) {
return MMap.map(url.getPath());
}
return StringUtil.readAllBytes(url);
return StringUtil.readAllBytes(url, ByteOrder.LITTLE_ENDIAN);
}

@Override
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.BIG_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.BIG_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 ecb02df

Please sign in to comment.