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; }
static char[] toChars(byte[] bytes) { Charset charset=Charset.forName("UTF-8"); ByteBuffer byteBuffer=ByteBuffer.wrap(bytes); CharBuffer charBuffer=charset.decode(byteBuffer); char[] chars=Arrays.copyOf(charBuffer.array(), charBuffer.limit()); Arrays.fill(charBuffer.array(), '\u0000'); // clear the cleartext Arrays.fill(byteBuffer.array(), (byte) 0); // clear the ciphertext return chars; } }
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; } }
/** 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); }
@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(); }
public void put(int index, String s) { int len = s.length(); int sLen = buffer.limit(); if (index < 0) return; char ch = len == 0 ? '\0' : s.charAt(0); if (index < sLen) buffer.put(index, ch); else { int cnt = index - sLen; CharBuffer tmp = CharBuffer.allocate(sLen + cnt + 1); tmp.put(buffer.array()); buffer = tmp; for(int i = 0; i < cnt; i++) { buffer.append('\32'); } buffer.append(ch); } }
/** 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; }
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; } }
/** * 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(); }
/** * 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; }
/** 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); }
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(); } }