MCPcopy Create free account
hub / github.com/Hiro420/NikkeTools / readerToUTF8Stream

Method readerToUTF8Stream

UnLuac/src/unluac/util/FileUtils.java:38–79  ·  view source on GitHub ↗
(Reader r)

Source from the content-addressed store, hash-verified

36 }
37
38 public static InputStream readerToUTF8Stream(Reader r) {
39 final CharsetEncoder encoder = Charset.forName("UTF-8").newEncoder();
40 final CharBuffer chars = CharBuffer.allocate(2);
41 final ByteBuffer buffer = ByteBuffer.allocate(8);
42 buffer.limit(0);
43 return new InputStream() {
44
45 @Override
46 public int read() throws IOException {
47 if(buffer.position() < buffer.limit()) {
48 return 0xff & buffer.get();
49 }
50 int result = r.read();
51 if(result <= 127) {
52 return result;
53 }
54 char c = (char)result;
55 chars.position(0);
56 chars.limit(1);
57 chars.put(c);
58 if(Character.isSurrogate(c)) {
59 result = r.read();
60 if(result >= 0) {
61 chars.limit(2);
62 chars.put((char) result);
63 }
64 }
65 chars.position(0);
66 buffer.limit(buffer.capacity());
67 buffer.position(0);
68 encoder.reset();
69 CoderResult coderResult = encoder.encode(chars, buffer, true);
70 if(coderResult.isError() || coderResult.isOverflow()) {
71 throw new IllegalStateException(coderResult.toString());
72 }
73 buffer.limit(buffer.position());
74 buffer.position(0);
75 return read();
76 }
77
78 };
79 }
80
81 private FileUtils() {}
82

Callers 1

Calls

no outgoing calls

Tested by

no test coverage detected