/** * Creates a new input stream that will encode the characters from {@code reader} into bytes using * the given character set encoder. * * @param reader input source * @param encoder character set encoder used for encoding chars to bytes * @param bufferSize size of internal input and output buffers * @throws IllegalArgumentException if bufferSize is non-positive */ ReaderInputStream(Reader reader, CharsetEncoder encoder, int bufferSize) { this.reader = checkNotNull(reader); this.encoder = checkNotNull(encoder); checkArgument(bufferSize > 0, "bufferSize must be positive: %s", bufferSize); encoder.reset(); charBuffer = CharBuffer.allocate(bufferSize); charBuffer.flip(); byteBuffer = ByteBuffer.allocate(bufferSize); }
/** * Construct a new {@link ReaderInputStream}. * * @param reader the target {@link Reader} * @param encoder the charset encoder * @param bufferSize the size of the input buffer in number of characters * @since 2.1 */ public ReaderInputStream(final Reader reader, final CharsetEncoder encoder, final int bufferSize) { this.reader = reader; this.encoder = encoder; this.encoderIn = CharBuffer.allocate(bufferSize); this.encoderIn.flip(); this.encoderOut = ByteBuffer.allocate(128); this.encoderOut.flip(); }
/** * Fills the internal char buffer from the reader. * * @throws IOException * If an I/O error occurs */ private void fillBuffer() throws IOException { if (!endOfInput && (lastCoderResult == null || lastCoderResult.isUnderflow())) { encoderIn.compact(); final int position = encoderIn.position(); // We don't use Reader#read(CharBuffer) here because it is more efficient // to write directly to the underlying char array (the default implementation // copies data to a temporary char array). final int c = reader.read(encoderIn.array(), position, encoderIn.remaining()); if (c == EOF) { endOfInput = true; } else { encoderIn.position(position+c); } encoderIn.flip(); } encoderOut.compact(); lastCoderResult = encoder.encode(encoderIn, encoderOut, endOfInput); encoderOut.flip(); }
@SuppressWarnings("ObjectToString") public static String decodeString(ByteBuffer src, Charset charset) { CharsetDecoder decoder = getDecoder(charset); CharBuffer dst = CharBuffer.allocate((int) ((double) src.remaining() * decoder.maxCharsPerByte())); try { CoderResult cr = decoder.decode(src, dst, true); if (!cr.isUnderflow()) { cr.throwException(); } cr = decoder.flush(dst); if (!cr.isUnderflow()) { cr.throwException(); } } catch (CharacterCodingException x) { throw new IllegalStateException(x); } return dst.flip().toString(); }
private void fillBuffer() throws IOException { int len = (int) Math.min(charBuffer.capacity() - charBuffer.position(), remaining); if (len > 0) { len = reader.read(charBuffer.array(), charBuffer.position(), len); } if (len > 0) { remaining -= len; } else { len = 0; remaining = 0; } length += len; charBuffer.limit(charBuffer.position() + len); charBuffer.rewind(); byteBuffer = ByteBuffer.allocate(Constants.IO_BUFFER_SIZE); boolean end = remaining == 0; encoder.encode(charBuffer, byteBuffer, end); if (end && byteBuffer.position() == 0) { // EOF byteBuffer = null; return; } byteBuffer.flip(); charBuffer.compact(); charBuffer.flip(); charBuffer.position(charBuffer.limit()); }
private void advance() throws IOException { assert !endOfInput; assert !bbuf.hasRemaining() : "advance() should be called when output byte buffer is empty. bbuf: " + bbuf + ", as string: " + bbuf.asCharBuffer().toString(); assert cbuf.remaining() < 2; // given that bbuf.capacity = 3 x cbuf.capacity, the only time that we should have a // remaining char is if the last char read was the 1st half of a surrogate pair if (cbuf.remaining() == 0) { cbuf.clear(); } else { cbuf.compact(); } int n = reader.read(cbuf); // read #1 cbuf.flip(); CoderResult result; endOfInput = n == -1; bbuf.clear(); result = encoder.encode(cbuf, bbuf, endOfInput); checkEncodeResult(result); if (endOfInput) { result = encoder.flush(bbuf); checkEncodeResult(result); } bbuf.flip(); }
@Override public String getSinkContents() throws IOException { File file = getFile(); Reader reader = new InputStreamReader(new FileInputStream(file), Charsets.UTF_8); StringBuilder builder = new StringBuilder(); CharBuffer buffer = CharBuffer.allocate(100); while (reader.read(buffer) != -1) { buffer.flip(); builder.append(buffer); buffer.clear(); } return builder.toString(); } }
private ByteBuffer processAsciiCommand(RequestReader request, Cache cache) { ByteBuffer buffer = request.getRequest(); CharBuffer flb = getFirstLineBuffer(); getAsciiDecoder().reset(); getAsciiDecoder().decode(buffer, flb, false); flb.flip(); String firstLine = getFirstLine(); String[] firstLineElements = firstLine.split(" "); boolean isGets = firstLineElements[0].equals("gets"); Set<String> keys = new HashSet<String>(); for (int i = 1; i < firstLineElements.length; i++) { keys.add(stripNewline(firstLineElements[i])); } Region<Object, ValueWrapper> r = getMemcachedRegion(cache); Map<Object, ValueWrapper> results = r.getAll(keys); return composeReply(results, isGets); }
protected void dispatchReader(WebSocket webSocket, Reader r) throws IOException { int read = 0; CharBuffer cb = webSocket.cb; try { while (read > -1) { cb.position(cb.position() + read); if (cb.remaining() == 0) { cb = resizeCharBuffer(webSocket); } read = r.read(cb.array(), cb.position(), cb.remaining()); } cb.flip(); invokeWebSocketProtocol(webSocket, cb.toString()); } finally { cb.clear(); } }
@Override protected CoderResult encodeLoop(CharBuffer in, ByteBuffer out) { if (!bomWritten) { CharBuffer bom = CharBuffer.allocate(1); bom.put(BOM); bom.flip(); CoderResult result = baseEncoder.encode(bom, out, false); if (!result.isUnderflow()) return result; bomWritten = true; } return baseEncoder.encode(in, out, false); }
private Map<String, Object> buildStringKeyMap(ByteBuffer input) { int payloadSize = input.remaining(); if (chars == null || chars.remaining() < payloadSize) { chars = CharBuffer.allocate(payloadSize); } final CoderResult coderResult = charset.newDecoder() .onMalformedInput(CodingErrorAction.REPLACE) .onUnmappableCharacter(CodingErrorAction.REPLACE) .decode(input, chars, true); Map<String, Object> theMap; if (coderResult.isUnderflow()) { chars.flip(); try { theMap = parseString(chars.toString()); } finally { chars.clear(); } } else { throw new ParseException("Failed with CoderResult[%s]", coderResult); } return theMap; }
/** * Construct a new {@link ReaderInputStream}. * * @param reader the target {@link java.io.Reader} * @param encoder the charset encoder * @param bufferSize the size of the input buffer in number of characters * @since 2.1 */ public ReaderInputStream(Reader reader, CharsetEncoder encoder, int bufferSize) { this.reader = reader; this.encoder = encoder; this.encoderIn = CharBuffer.allocate(bufferSize); this.encoderIn.flip(); this.encoderOut = ByteBuffer.allocate(128); this.encoderOut.flip(); }
private void fillBuffer() throws IOException { int len = (int) Math.min(charBuffer.capacity() - charBuffer.position(), remaining); if (len > 0) { len = reader.read(charBuffer.array(), charBuffer.position(), len); } if (len > 0) { remaining -= len; } else { len = 0; remaining = 0; } length += len; charBuffer.limit(charBuffer.position() + len); charBuffer.rewind(); byteBuffer = ByteBuffer.allocate(Constants.IO_BUFFER_SIZE); boolean end = remaining == 0; encoder.encode(charBuffer, byteBuffer, end); if (end && byteBuffer.position() == 0) { // EOF byteBuffer = null; return; } byteBuffer.flip(); charBuffer.compact(); charBuffer.flip(); charBuffer.position(charBuffer.limit()); }
/** * Fills the internal char buffer from the reader. * * @throws java.io.IOException If an I/O error occurs */ private void fillBuffer() throws IOException { if (!endOfInput && (lastCoderResult == null || lastCoderResult.isUnderflow())) { encoderIn.compact(); int position = encoderIn.position(); // We don't use Reader#read(CharBuffer) here because it is more efficient // to write directly to the underlying char array (the default implementation // copies data to a temporary char array). int c = reader.read(encoderIn.array(), position, encoderIn.remaining()); if (c == -1) { endOfInput = true; } else { encoderIn.position(position + c); } encoderIn.flip(); } encoderOut.compact(); lastCoderResult = encoder.encode(encoderIn, encoderOut, endOfInput); encoderOut.flip(); }
/** * Construct a new {@link ReaderInputStream}. * * @param reader the target {@link Reader} * @param charset the charset encoding * @param bufferSize the size of the input buffer in number of characters */ private ReaderInputStream(final Reader reader, final Charset charset, final int bufferSize) { this.reader = reader; encoder = charset.newEncoder(); encoderIn = CharBuffer.allocate(bufferSize); encoderIn.flip(); }
private ByteBuffer processAsciiCommand(ByteBuffer buffer, Cache cache) { CharBuffer flb = getFirstLineBuffer(); getAsciiDecoder().reset(); getAsciiDecoder().decode(buffer, flb, false); flb.flip(); String firstLine = getFirstLine(); String[] firstLineElements = firstLine.split(" "); String key = firstLineElements[1]; int flags = Integer.parseInt(firstLineElements[2]); long expTime = Long.parseLong(firstLineElements[3]); int numBytes = Integer.parseInt(firstLineElements[4]); long casVersion = Long.parseLong(stripNewline(firstLineElements[5])); byte[] value = new byte[numBytes]; buffer.position(firstLine.length()); for (int i = 0; i < numBytes; i++) { value[i] = buffer.get(); } String reply = Reply.EXISTS.toString(); Region<Object, ValueWrapper> r = getMemcachedRegion(cache); ValueWrapper expected = ValueWrapper.getDummyValue(casVersion); if (r.replace(key, expected, ValueWrapper.getWrappedValue(value, flags))) { reply = Reply.STORED.toString(); } return asciiCharset.encode(reply); } }
@Nonnull private static String decodeCharset(@Nonnull ByteBuffer in, @Nonnegative int length, @Nonnull Charset charset) { CharBuffer out = CharBuffer.allocate(length); // TODO: It isn't clear to me whether this reads an extra byte from 'in' and stores it in the decoder. // We could also assume UCS-2, in which case byte-length = char-length * 2. // In that case, we can limit the ByteBuffer and avoid the potential decode over-read. charset.newDecoder().decode(in, out, true); out.flip(); return out.toString(); }