Refine search
/** Returns a new CharBuffer identical to buf, except twice the capacity. */ private static CharBuffer grow(CharBuffer buf) { char[] copy = Arrays.copyOf(buf.array(), buf.capacity() * 2); CharBuffer bigger = CharBuffer.wrap(copy); bigger.position(buf.position()); bigger.limit(buf.limit()); return bigger; }
/** * 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); }
/** * {@inheritDoc} * @param readlimit max read limit (ignored) */ @Override public synchronized void mark(final int readlimit) { this.mark_cbuf = this.cbuf.position(); this.mark_bbuf = this.bbuf.position(); this.cbuf.mark(); this.bbuf.mark(); // It would be nice to be able to use mark & reset on the cbuf and bbuf; // however the bbuf is re-used so that won't work }
int read = 0; while (len > 0) { if (encoderOut.position() > 0) { encoderOut.flip(); int c = Math.min(encoderOut.remaining(), len); if (!endOfInput && (lastCoderResult == null || lastCoderResult.isUnderflow())) { encoderIn.compact(); int position = encoderIn.position(); int c = reader.read(encoderIn.array(), position, encoderIn.remaining()); if (c == -1) { endOfInput = true; } else { encoderIn.position(position + c); if (endOfInput && encoderOut.position() == 0) { break;
@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 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; } }
if (this.cbuf.position() != 0) { this.encoder.reset(); this.cbuf.rewind(); this.bbuf.rewind(); this.bbuf.limit(0); // rewind does not clear the buffer while(this.cbuf.position() < this.mark_cbuf) { this.bbuf.rewind(); // empty the buffer (we only refill when empty during normal processing) this.bbuf.limit(0); if (this.cbuf.position() != this.mark_cbuf) { throw new IllegalStateException("Unexpected CharBuffer postion: actual=" + cbuf.position() + " " + "expected=" + this.mark_cbuf); this.bbuf.position(this.mark_bbuf); this.mark_cbuf = NO_MARK; this.mark_bbuf = NO_MARK;
protected void writeString(long offset, String value, int length) throws IOException { ByteBuffer bbuf = ByteBuffer.allocate(length * 2); bbuf.order(BYTEORDER); bbuf.position(0); bbuf.limit(length * 2); CharBuffer cbuf = bbuf.asCharBuffer(); cbuf.put(value); while (cbuf.position() < cbuf.limit()) { cbuf.put(' '); } write(offset, bbuf.array()); }
/** Returns a new CharBuffer identical to buf, except twice the capacity. */ private static CharBuffer grow(CharBuffer buf) { char[] copy = Arrays.copyOf(buf.array(), buf.capacity() * 2); CharBuffer bigger = CharBuffer.wrap(copy); bigger.position(buf.position()); bigger.limit(buf.limit()); return bigger; }
/** * 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); }