/** * Creates a {@link CharStream} given an opened {@link ReadableByteChannel} * containing UTF-8 bytes. * * Reads the entire contents of the {@code channel} into * the result before returning, then closes the {@code channel}. */ public static CharStream fromChannel(ReadableByteChannel channel) throws IOException { return fromChannel(channel, StandardCharsets.UTF_8); }
/** * Creates a {@link CharStream} given an opened {@link ReadableByteChannel} and the * charset of the bytes contained in the channel. * * Reads the entire contents of the {@code channel} into * the result before returning, then closes the {@code channel}. */ public static CharStream fromChannel(ReadableByteChannel channel, Charset charset) throws IOException { return fromChannel( channel, DEFAULT_BUFFER_SIZE, CodingErrorAction.REPLACE, IntStream.UNKNOWN_SOURCE_NAME); }
public static CharStream fromStream(InputStream is, Charset charset, long inputSize) throws IOException { try (ReadableByteChannel channel = Channels.newChannel(is)) { return fromChannel( channel, charset, DEFAULT_BUFFER_SIZE, CodingErrorAction.REPLACE, IntStream.UNKNOWN_SOURCE_NAME, inputSize); } }
/** * Creates a {@link CharStream} given a path to a file on disk and the * charset of the bytes contained in the file. * * Reads the entire contents of the file into the result before returning. */ public static CharStream fromPath(Path path, Charset charset) throws IOException { long size = Files.size(path); try (ReadableByteChannel channel = Files.newByteChannel(path)) { return fromChannel( channel, charset, DEFAULT_BUFFER_SIZE, CodingErrorAction.REPLACE, path.toString(), size); } }
public static CodePointCharStream fromChannel( ReadableByteChannel channel, Charset charset, int bufferSize, CodingErrorAction decodingErrorAction, String sourceName, long inputSize) throws IOException { try { ByteBuffer utf8BytesIn = ByteBuffer.allocate(bufferSize); CharBuffer utf16CodeUnitsOut = CharBuffer.allocate(bufferSize); if (inputSize == -1) { inputSize = bufferSize; } else if (inputSize > Integer.MAX_VALUE) { // ByteBuffer et al don't support long sizes throw new IOException(String.format("inputSize %d larger than max %d", inputSize, Integer.MAX_VALUE)); } CodePointBuffer.Builder codePointBufferBuilder = CodePointBuffer.builder((int) inputSize); CharsetDecoder decoder = charset .newDecoder() .onMalformedInput(decodingErrorAction) .onUnmappableCharacter(decodingErrorAction); boolean endOfInput = false; while (!endOfInput) { int bytesRead = channel.read(utf8BytesIn); endOfInput = (bytesRead == -1); utf8BytesIn.flip(); CoderResult result = decoder.decode(
/** * Creates a {@link CharStream} given an opened {@link ReadableByteChannel} * containing UTF-8 bytes. * * Reads the entire contents of the {@code channel} into * the result before returning, then closes the {@code channel}. */ public static CharStream fromChannel(ReadableByteChannel channel) throws IOException { return fromChannel(channel, StandardCharsets.UTF_8); }
/** * Creates a {@link CharStream} given an opened {@link ReadableByteChannel} and the * charset of the bytes contained in the channel. * * Reads the entire contents of the {@code channel} into * the result before returning, then closes the {@code channel}. */ public static CharStream fromChannel(ReadableByteChannel channel, Charset charset) throws IOException { return fromChannel( channel, DEFAULT_BUFFER_SIZE, CodingErrorAction.REPLACE, IntStream.UNKNOWN_SOURCE_NAME); }
/** * Creates a {@link CharStream} given an opened {@link ReadableByteChannel} and the * charset of the bytes contained in the channel. * * Reads the entire contents of the {@code channel} into * the result before returning, then closes the {@code channel}. */ public static CharStream fromChannel(ReadableByteChannel channel, Charset charset) throws IOException { return fromChannel( channel, DEFAULT_BUFFER_SIZE, CodingErrorAction.REPLACE, IntStream.UNKNOWN_SOURCE_NAME); }
/** * Creates a {@link CharStream} given an opened {@link ReadableByteChannel} * containing UTF-8 bytes. * * Reads the entire contents of the {@code channel} into * the result before returning, then closes the {@code channel}. */ public static CharStream fromChannel(ReadableByteChannel channel) throws IOException { return fromChannel(channel, Charset.forName("UTF-8")); }
public static CharStream fromStream(InputStream is, Charset charset, String sourceName, long inputSize) throws IOException { return fromChannel( Channels.newChannel(is), charset, DEFAULT_BUFFER_SIZE, CodingErrorAction.REPLACE, sourceName, inputSize); }
public static CharStream fromStream(InputStream is, Charset charset, long inputSize) throws IOException { try (ReadableByteChannel channel = Channels.newChannel(is)) { return fromChannel( channel, charset, DEFAULT_BUFFER_SIZE, CodingErrorAction.REPLACE, IntStream.UNKNOWN_SOURCE_NAME, inputSize); } }
/** * Creates a {@link CharStream} given a path to a file on disk and the * charset of the bytes contained in the file. * * Reads the entire contents of the file into the result before returning. */ public static CharStream fromPath(Path path, Charset charset) throws IOException { long size = Files.size(path); try (ReadableByteChannel channel = Files.newByteChannel(path)) { return fromChannel( channel, charset, DEFAULT_BUFFER_SIZE, CodingErrorAction.REPLACE, path.toString(), size); } }
public static CodePointCharStream fromChannel( ReadableByteChannel channel, Charset charset, int bufferSize, CodingErrorAction decodingErrorAction, String sourceName, long inputSize) throws IOException { CodePointBuffer codePointBuffer = bufferFromChannel(channel, charset, bufferSize, decodingErrorAction, inputSize); return CodePointCharStream.fromBuffer(codePointBuffer, sourceName); }
public static CodePointCharStream fromChannel( ReadableByteChannel channel, Charset charset, int bufferSize, CodingErrorAction decodingErrorAction, String sourceName, long inputSize) throws IOException { try { ByteBuffer utf8BytesIn = ByteBuffer.allocate(bufferSize); CharBuffer utf16CodeUnitsOut = CharBuffer.allocate(bufferSize); if (inputSize == -1) { inputSize = bufferSize; } else if (inputSize > Integer.MAX_VALUE) { // ByteBuffer et al don't support long sizes throw new IOException(String.format("inputSize %d larger than max %d", inputSize, Integer.MAX_VALUE)); } CodePointBuffer.Builder codePointBufferBuilder = CodePointBuffer.builder((int) inputSize); CharsetDecoder decoder = charset .newDecoder() .onMalformedInput(decodingErrorAction) .onUnmappableCharacter(decodingErrorAction); boolean endOfInput = false; while (!endOfInput) { int bytesRead = channel.read(utf8BytesIn); endOfInput = (bytesRead == -1); utf8BytesIn.flip(); CoderResult result = decoder.decode(