@Override public String read() throws IOException { CharBuffer response = CharBuffer.allocate(bufferSize); StringBuilder result = new StringBuilder(); do { response.clear(); reader.read(response); result.append(response.array(), response.arrayOffset(), response.position()); } while (response.get(response.position() - 1) != '\n'); return result.toString(); }
private void enlargeBuffer() { CharBuffer newBuf = CharBuffer.allocate(cBuf.capacity() * 2); newBuf.put(cBuf.array(), cBuf.arrayOffset(), cBuf.position()); cBuf = newBuf; } }
private int getArray(CharBuffer out) { if (out.hasArray()) { output = out.array(); outEnd = out.arrayOffset() + out.limit(); return out.arrayOffset() + out.position(); } else { outEnd = out.remaining(); if (allocatedOutput == null || outEnd > allocatedOutput.length) { allocatedOutput = new char[outEnd]; } // The array's start position is 0. output = allocatedOutput; return 0; } }
/** * Creates a new big-endian buffer whose content is a subregion of * the specified {@code string} encoded in the specified {@code charset}. * The new buffer's {@code readerIndex} and {@code writerIndex} are * {@code 0} and the length of the encoded string respectively. */ public static ByteBuf copiedBuffer( CharSequence string, int offset, int length, Charset charset) { if (string == null) { throw new NullPointerException("string"); } if (length == 0) { return EMPTY_BUFFER; } if (string instanceof CharBuffer) { CharBuffer buf = (CharBuffer) string; if (buf.hasArray()) { return copiedBuffer( buf.array(), buf.arrayOffset() + buf.position() + offset, length, charset); } buf = buf.slice(); buf.limit(length); buf.position(offset); return copiedBuffer(buf, charset); } return copiedBuffer(CharBuffer.wrap(string, offset, offset + length), charset); }
private void setPosition(CharBuffer out) { if (out.hasArray()) { out.position(out.position() + data[OUTPUT_OFFSET] - out.arrayOffset()); } else { out.put(output, 0, data[OUTPUT_OFFSET]); } // release reference to output array, which may not be ours output = null; }
private int getArray(CharBuffer in) { if (in.hasArray()) { input = in.array(); inEnd = in.arrayOffset() + in.limit(); return in.arrayOffset() + in.position(); } else { inEnd = in.remaining(); if (allocatedInput == null || inEnd > allocatedInput.length) { allocatedInput = new char[inEnd]; } // Copy the input buffer into the allocated array. int pos = in.position(); in.get(allocatedInput, 0, inEnd); in.position(pos); // The array's start position is 0 input = allocatedInput; return 0; } }
/** * Creates a new big-endian buffer whose content is a subregion of * the specified {@code string} encoded in the specified {@code charset}. * The new buffer's {@code readerIndex} and {@code writerIndex} are * {@code 0} and the length of the encoded string respectively. */ public static ByteBuf copiedBuffer( CharSequence string, int offset, int length, Charset charset) { if (string == null) { throw new NullPointerException("string"); } if (length == 0) { return EMPTY_BUFFER; } if (string instanceof CharBuffer) { CharBuffer buf = (CharBuffer) string; if (buf.hasArray()) { return copiedBuffer( buf.array(), buf.arrayOffset() + buf.position() + offset, length, charset); } buf = buf.slice(); buf.limit(length); buf.position(offset); return copiedBuffer(buf, charset); } return copiedBuffer(CharBuffer.wrap(string, offset, offset + length), charset); }
/** * Creates a new big-endian buffer whose content is a subregion of * the specified {@code string} encoded in the specified {@code charset}. * The new buffer's {@code readerIndex} and {@code writerIndex} are * {@code 0} and the length of the encoded string respectively. */ public static ByteBuf copiedBuffer( CharSequence string, int offset, int length, Charset charset) { if (string == null) { throw new NullPointerException("string"); } if (length == 0) { return EMPTY_BUFFER; } if (string instanceof CharBuffer) { CharBuffer buf = (CharBuffer) string; if (buf.hasArray()) { return copiedBuffer( buf.array(), buf.arrayOffset() + buf.position() + offset, length, charset); } buf = buf.slice(); buf.limit(length); buf.position(offset); return copiedBuffer(buf, charset); } return copiedBuffer(CharBuffer.wrap(string, offset, offset + length), charset); }
/** * For direct buffers, calls {@link Pointer#Pointer(Buffer)}, while for buffers * backed with an array, allocates enough memory for the array and copies it. * * @param buffer the Buffer to reference or copy * @see #put(char[]) */ public CharPointer(CharBuffer buffer) { super(buffer); if (buffer != null && !buffer.isDirect() && buffer.hasArray()) { char[] array = buffer.array(); allocateArray(array.length - buffer.arrayOffset()); put(array, buffer.arrayOffset(), array.length - buffer.arrayOffset()); position(buffer.position()); limit(buffer.limit()); } } /**
/** * Appends the contents of a char buffer to this string builder. * Appending null will call {@link #appendNull()}. * * @param buf the char buffer to append * @return this, to enable chaining * @since 3.4 */ public StrBuilder append(final CharBuffer buf) { if (buf == null) { return appendNull(); } if (buf.hasArray()) { final int length = buf.remaining(); final int len = length(); ensureCapacity(len + length); System.arraycopy(buf.array(), buf.arrayOffset() + buf.position(), buffer, len, length); size += length; } else { append(buf.toString()); } return this; }
/** * Fill a buffer with a repeated value. * * @param buffer the buffer to fill * @param value the value to fill * @param count the number of chars to fill * @return the buffer instance */ public static CharBuffer fill(CharBuffer buffer, int value, int count) { if (count > buffer.remaining()) { throw msg.bufferUnderflow(); } if (buffer.hasArray()) { final int offs = buffer.arrayOffset(); Arrays.fill(buffer.array(), offs + buffer.position(), offs + buffer.limit(), (char) value); skip(buffer, count); } else { for (int i = count; i > 0; i--) { buffer.put((char)value); } } return buffer; }
/** * Take a certain number of chars from the buffer and return them in an array. * * @param buffer the buffer to read * @param cnt the number of chars to take * @return the chars */ public static char[] take(CharBuffer buffer, int cnt) { if (cnt < 0) { throw msg.parameterOutOfRange("cnt"); } if (buffer.hasArray()) { final int pos = buffer.position(); final int lim = buffer.limit(); if (lim - pos < cnt) { throw new BufferUnderflowException(); } final char[] array = buffer.array(); final int offset = buffer.arrayOffset(); buffer.position(pos + cnt); final int start = offset + pos; return Arrays.copyOfRange(array, start, start + cnt); } final char[] chars = new char[cnt]; buffer.get(chars); return chars; }
nextBlock.accept(Arrays.copyOfRange(outBuf.array(), outBuf.arrayOffset(), outBuf.position()), isEndOfInput); assert outBuf.position() > 0; nextBlock.accept(Arrays.copyOfRange(outBuf.array(), outBuf.arrayOffset(), outBuf.position()), isEndOfInput);
System.arraycopy(buf.array(), buf.arrayOffset() + buf.position() + startIndex, buffer, len, length); size += length; } else {
} else if (csq instanceof CharBuffer && ((CharBuffer) csq).hasArray()) { final CharBuffer cb = (CharBuffer) csq; System.arraycopy(cb.array(), cb.arrayOffset() + cb.position() + start, termBuffer, termLength, len); } else if (csq instanceof StringBuffer) { ((StringBuffer) csq).getChars(start, end, termBuffer, termLength);
enlargeBuffer(); value = new String(cBuf.array(), cBuf.arrayOffset(), cBuf.position());
endianness, buf.array(), buf.arrayOffset() + buf.position() + offset, length, charset);
int destPosition = dst.arrayOffset() + dst.position(); int destLength = dst.arrayOffset() + dst.limit(); int destLengthASCII = destPosition + Math.min(srcLength - srcPosition, destLength - destPosition);
int arrayOffset() { switch (type) { case BYTE: return byteBuffer.arrayOffset(); case CHAR: return charBuffer.arrayOffset(); case INT: return intBuffer.arrayOffset(); } throw new UnsupportedOperationException("Not reached"); }
private void appendArrayChar(CharBuffer utf16In) { assert prevHighSurrogate == -1; char[] in = utf16In.array(); int inOffset = utf16In.arrayOffset() + utf16In.position(); int inLimit = utf16In.arrayOffset() + utf16In.limit(); char[] outChar = charBuffer.array(); int outOffset = charBuffer.arrayOffset() + charBuffer.position(); while (inOffset < inLimit) { char c = in[inOffset]; if (!Character.isHighSurrogate(c)) { outChar[outOffset] = c; } else { utf16In.position(inOffset - utf16In.arrayOffset()); charBuffer.position(outOffset - charBuffer.arrayOffset()); charToIntBuffer(utf16In.remaining()); appendArrayInt(utf16In); return; } inOffset++; outOffset++; } utf16In.position(inOffset - utf16In.arrayOffset()); charBuffer.position(outOffset - charBuffer.arrayOffset()); }