/** 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 static CharBuffer ensureCapacity(CharBuffer buffer, int capacity) { if (buffer == null || buffer.capacity() < capacity) { buffer = CharBuffer.allocate(capacity); } return buffer; }
/** 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; }
/** 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 void prepareForNextIteration(final CharBuffer buffer) { buffer.position(buffer.limit()) .limit(buffer.capacity()); }
/** 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 void expandBuffer() { int oldPosition = buffer.position(); int oldCapacity = buffer.capacity(); int oldLimit = buffer.limit(); int newCapacity = oldCapacity * 2; char[] newBuffer = new char[newCapacity]; System.arraycopy(buffer.array(), 0, newBuffer, 0, oldLimit); buffer = CharBuffer.wrap(newBuffer, 0, newCapacity); buffer.position(oldPosition); buffer.limit(oldLimit); }
protected void ensureCapacity(int l) { if (charBuffer.position() == 0) { if (charBuffer.capacity() < l) { charBuffer = allocateCharBuffer(l); } } else if (charBuffer.remaining() < l) { CharBuffer newCharBuffer = allocateCharBuffer(charBuffer.position() + l); charBuffer.flip(); newCharBuffer.put(charBuffer); charBuffer = newCharBuffer; } }
private void enlargeBuffer() { CharBuffer newBuf = CharBuffer.allocate(cBuf.capacity() * 2); newBuf.put(cBuf.array(), cBuf.arrayOffset(), cBuf.position()); cBuf = newBuf; } }
public CharBuffer decodeUnsafely(byte[] byteS, int start, int len) { // Prepare buffers if (byteBuffer.capacity() < len) { byteBuffer = ByteBuffer.allocate(len * 2); } byteBuffer.clear(); byteBuffer.put(byteS, start, len); byteBuffer.flip(); int maxChars = (int) (byteBuffer.capacity() * decoder.maxCharsPerByte()); if (charBuffer.capacity() < maxChars) { charBuffer = CharBuffer.allocate(maxChars); } charBuffer.clear(); // Decode UTF-8 decoder.reset(); decoder.decode(byteBuffer, charBuffer, true); decoder.flush(charBuffer); charBuffer.flip(); return charBuffer; } }
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()); }
public CharBuffer decodeUnsafely(byte[] byteS, int start, int len) { // Prepare buffers if (byteBuffer.capacity() < len) { byteBuffer = ByteBuffer.allocate(len * 2); } byteBuffer.clear(); byteBuffer.put(byteS, start, len); byteBuffer.flip(); int maxChars = (int) (byteBuffer.capacity() * decoder.maxCharsPerByte()); if (charBuffer.capacity() < maxChars) { charBuffer = CharBuffer.allocate(maxChars); } charBuffer.clear(); // Decode UTF-8 decoder.reset(); decoder.decode(byteBuffer, charBuffer, true); decoder.flush(charBuffer); charBuffer.flip(); return charBuffer; } }
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()); }
if (matcher.find()) { if (inputExhausted || matcher.end() != bufferLength || bufferLength < buffer.capacity()) { matchSuccessful = true; findStartIndex = matcher.end();
@Test public void testCopyStartsAtBufferPosition() throws Exception { final CharBuffer buff = CharBuffer.wrap(new char[10]); final int START_POSITION = 5; buff.position(START_POSITION); // set start position final StringBuilder text = createText(15); final int length = TextEncoderHelper.copy(text, 0, buff); assertEquals("partial copy", buff.capacity() - START_POSITION, length); for (int i = 0; i < length; i++) { assertEquals("char at " + i, text.charAt(i), buff.get(START_POSITION + i)); } assertEquals("buffer position at end", buff.capacity(), buff.position()); }
@Test public void testCopyUpToRemainingSpace() throws Exception { final CharBuffer buff = CharBuffer.wrap(new char[3]); final StringBuilder text = createText(15); final int length = TextEncoderHelper.copy(text, 0, buff); assertEquals("partial copy", buff.capacity(), length); for (int i = 0; i < length; i++) { assertEquals("char at " + i, text.charAt(i), buff.get(i)); } assertEquals("no space remaining", 0, buff.remaining()); assertEquals("position at end", buff.capacity(), buff.position()); }
@Test public void testCopyDoesNotWriteBeyondStringText() throws Exception { final CharBuffer buff = CharBuffer.wrap(new char[5]); assertEquals("initial buffer position", 0, buff.position()); final StringBuilder text = createText(2); final int length = TextEncoderHelper.copy(text, 0, buff); assertEquals("full copy", text.length(), length); for (int i = 0; i < length; i++) { assertEquals("char at " + i, text.charAt(i), buff.get(i)); } assertEquals("resulting buffer position", text.length(), buff.position()); for (int i = length; i < buff.capacity(); i++) { assertEquals("unset char at " + i, 0, buff.get(i)); } }
public void testGet_io() throws IOException { assertEquals(-1, ArbitraryInstances.get(InputStream.class).read()); assertEquals(-1, ArbitraryInstances.get(ByteArrayInputStream.class).read()); assertEquals(-1, ArbitraryInstances.get(Readable.class).read(CharBuffer.allocate(1))); assertEquals(-1, ArbitraryInstances.get(Reader.class).read()); assertEquals(-1, ArbitraryInstances.get(StringReader.class).read()); assertEquals(0, ArbitraryInstances.get(Buffer.class).capacity()); assertEquals(0, ArbitraryInstances.get(CharBuffer.class).capacity()); assertEquals(0, ArbitraryInstances.get(ByteBuffer.class).capacity()); assertEquals(0, ArbitraryInstances.get(ShortBuffer.class).capacity()); assertEquals(0, ArbitraryInstances.get(IntBuffer.class).capacity()); assertEquals(0, ArbitraryInstances.get(LongBuffer.class).capacity()); assertEquals(0, ArbitraryInstances.get(FloatBuffer.class).capacity()); assertEquals(0, ArbitraryInstances.get(DoubleBuffer.class).capacity()); ArbitraryInstances.get(PrintStream.class).println("test"); ArbitraryInstances.get(PrintWriter.class).println("test"); assertNotNull(ArbitraryInstances.get(File.class)); assertFreshInstanceReturned( ByteArrayOutputStream.class, OutputStream.class, Writer.class, StringWriter.class, PrintStream.class, PrintWriter.class); assertEquals(ByteSource.empty(), ArbitraryInstances.get(ByteSource.class)); assertEquals(CharSource.empty(), ArbitraryInstances.get(CharSource.class)); assertNotNull(ArbitraryInstances.get(ByteSink.class)); assertNotNull(ArbitraryInstances.get(CharSink.class)); }