Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
Loading