Tabnine Logo
IoBuffer
Code IndexAdd Tabnine to your IDE (free)

How to use
IoBuffer
in
org.apache.mina.codec

Best Java code snippets using org.apache.mina.codec.IoBuffer (Showing top 20 results out of 315)

origin: apache/mina

@SuppressWarnings("unchecked")
public <L extends GeneratedMessage> L get(Class<L> clazz, ExtensionRegistryLite registry)
    throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
  Method parseMethod = clazz.getDeclaredMethod("parseFrom", InputStream.class, ExtensionRegistryLite.class);
  return (L) parseMethod.invoke(null, input.asInputStream(), registry);
}
origin: apache/mina

/**
 * @see ByteBuffer#compact()
 */
public IoBuffer compact() {
  for (int i = 0; i < remaining(); i++) {
    put(i, get(i + position.getPosition()));
  }
  position(limit() - position());
  limit(capacity);
  mark = null;
  return this;
}
origin: apache/mina

/**
 * Extends the current IoBuffer capacity.
 * 
 * @param size
 *            the number of bytes to extend the current IoBuffer
 * @return the current {@link IoBuffer}
 */
public IoBuffer extend(int size) {
  ByteBuffer extension = isDirect() ? ByteBuffer.allocateDirect(size) : ByteBuffer.allocate(size);
  add(extension);
  return this;
}
origin: apache/mina

  @Override
  public Integer decode(IoBuffer input) {
    if (input.remaining() < 4) {
      return null;
    }
    int out = 0;
    for (int i = 0; i < 32; i += 8) {
      out |= (input.get() & 0xff) << (bo == ByteOrder.BIG_ENDIAN ? 24 - i : i);
    }
    return out;
  }
}
origin: apache/mina

/**
 * @see ByteBuffer#getChar(int)
 */
public char getChar(int index) {
  return getChar(getPointerByPosition(index));
}
origin: apache/mina

/**
 * @see ByteBuffer#getDouble(int)
 */
public double getDouble(int index) {
  return getDouble(getPointerByPosition(index));
}
origin: apache/mina

/**
 * Test the flip() method
 */
@Test
public void testFlip() {
  ByteBuffer bb1 = ByteBuffer.allocate(4);
  bb1.put("0123".getBytes());
  bb1.flip();
  ByteBuffer bb2 = ByteBuffer.allocate(4);
  bb2.put("4567".getBytes());
  bb2.flip();
  IoBuffer ioBuffer = IoBuffer.wrap(bb1, bb2);
  // Move forward a bit
  ioBuffer.get();
  ioBuffer.get();
  // Clear
  ioBuffer.clear();
  // We should be back to the origin
  assertEquals(0, ioBuffer.position());
  assertEquals(8, ioBuffer.limit());
}
origin: apache/mina

/**
 * Test the addition of 3 heap buffers, one being empty
 */
@Test
public void testAddHeapBuffersOneEmpty() {
  ByteBuffer bb1 = ByteBuffer.allocate(5);
  bb1.put("012".getBytes());
  bb1.flip();
  ByteBuffer bb2 = ByteBuffer.allocate(0);
  ByteBuffer bb3 = ByteBuffer.allocate(5);
  bb3.put("3456".getBytes());
  bb3.flip();
  IoBuffer ioBuffer = IoBuffer.newInstance();
  ioBuffer.add(bb1, bb2).add(bb3);
  assertEquals(0, ioBuffer.position());
  assertEquals(7, ioBuffer.limit());
  assertEquals(7, ioBuffer.capacity());
  for (int i = 0; i < 7; i++) {
    assertTrue(ioBuffer.hasRemaining());
    assertEquals("0123456".charAt(i), ioBuffer.get());
  }
  try {
    ioBuffer.get();
    fail();
  } catch (BufferUnderflowException bufe) {
    assertTrue(true);
  }
}
origin: apache/mina

@Test
public void testSlice() {
  ByteBuffer bb1 = ByteBuffer.allocate(5);
  bb1.put("012".getBytes());
  bb1.flip();
  ByteBuffer bb2 = ByteBuffer.allocate(5);
  bb2.put("345".getBytes());
  bb2.flip();
  ByteBuffer bb3 = ByteBuffer.allocate(5);
  bb3.put("6789".getBytes());
  bb3.flip();
  IoBuffer ioBuffer = IoBuffer.newInstance();
  ioBuffer.add(bb1, bb2).add(bb3);
  ioBuffer.position(2);
  ioBuffer.limit(8);
  IoBuffer slice = ioBuffer.slice();
  assertEquals(6, slice.remaining());
  assertEquals(0, slice.position());
  assertEquals(6, slice.limit());
  byte seg[] = "234567".getBytes();
  for (int i = 0; i < 6; i++) {
    assertEquals(seg[i], slice.get(i));
  }
}
origin: apache/mina

@Test
public void testFloat() {
  for (ByteOrder bo : new ByteOrder[] { ByteOrder.BIG_ENDIAN, ByteOrder.LITTLE_ENDIAN }) {
    ByteBuffer bb = (ByteBuffer) ByteBuffer.allocate(5).order(bo).putFloat(-0.68f).rewind();
    IoBuffer ioBuffer = IoBuffer.wrap(bb).order(bo);
    assertEquals(5, ioBuffer.capacity());
    ioBuffer.extend(3);
    ioBuffer.position(4);
    assertEquals(8, ioBuffer.capacity());
    ioBuffer.putFloat(3.14f);
    ioBuffer.rewind();
    assertEquals(-0.68f, ioBuffer.getFloat(), 0.001f);
    assertEquals(3.14f, ioBuffer.getFloat(), 0.001f);
    ioBuffer.rewind();
    ioBuffer.putFloat(2, -12.34f);
    assertEquals(-12.34f, ioBuffer.getFloat(2), 0.001f);
  }
}
origin: apache/mina

@Test
public void testDouble() {
  for (ByteOrder bo : new ByteOrder[] { ByteOrder.BIG_ENDIAN, ByteOrder.LITTLE_ENDIAN }) {
    ByteBuffer bb = (ByteBuffer) ByteBuffer.allocate(9).order(bo).putDouble(Math.PI).rewind();
    IoBuffer ioBuffer = IoBuffer.wrap(bb).order(bo);
    assertEquals(9, ioBuffer.capacity());
    ioBuffer.extend(7);
    ioBuffer.position(8);
    assertEquals(16, ioBuffer.capacity());
    ioBuffer.putDouble(-Math.E);
    ioBuffer.rewind();
    assertEquals(Math.PI, ioBuffer.getDouble(), 1E-10);
    assertEquals(-Math.E, ioBuffer.getDouble(), 1E-10);
    ioBuffer.rewind();
    ioBuffer.putDouble(4, 12.34);
    assertEquals(12.34, ioBuffer.getDouble(4), 1E-10);
  }
}
origin: apache/mina

/**
 * Test the allocation of a new heap IoBuffer with no byte in it
 */
@Test
public void testAllocate0() {
  IoBuffer ioBuffer = IoBuffer.allocate(0);
  assertFalse(ioBuffer.isDirect());
  assertEquals(0, ioBuffer.capacity());
  assertEquals(0, ioBuffer.limit());
  assertEquals(0, ioBuffer.position());
  assertFalse(ioBuffer.hasRemaining());
}
origin: apache/mina

/**
 * Test the allocation of a new direct IoBuffer with 1024 bytes
 */
@Test
public void testAllocateDirect1024() {
  IoBuffer ioBuffer = IoBuffer.allocateDirect(1024);
  assertTrue(ioBuffer.isDirect());
  assertEquals(1024, ioBuffer.capacity());
  assertEquals(1024, ioBuffer.limit());
  assertEquals(0, ioBuffer.position());
  assertTrue(ioBuffer.hasRemaining());
}
origin: apache/mina

@Override
public OUT decode(IoBuffer input, MutableInt nextBlockSize) {
  OUT output = null;
  if (nextBlockSize.getValue() == null) {
    nextBlockSize.setValue(sizeDecoder.decode(input));
  }
  if (nextBlockSize.isDefined() && (input.remaining() >= nextBlockSize.getValue())) {
    IoBuffer buffer = input.slice();
    buffer.limit(buffer.position() + nextBlockSize.getValue());
    output = payloadDecoder.decode(buffer);
    nextBlockSize.reset();
  }
  return output;
}
origin: apache/mina

@Test
public void testLong() {
  for (ByteOrder bo : new ByteOrder[] { ByteOrder.BIG_ENDIAN, ByteOrder.LITTLE_ENDIAN }) {
    ByteBuffer bb = (ByteBuffer) ByteBuffer.allocate(9).order(bo).putLong(123456789012l).rewind();
    IoBuffer ioBuffer = IoBuffer.wrap(bb).order(bo);
    assertEquals(9, ioBuffer.capacity());
    ioBuffer.extend(7);
    ioBuffer.position(8);
    assertEquals(16, ioBuffer.capacity());
    ioBuffer.putLong(-23456789023l);
    ioBuffer.rewind();
    assertEquals(123456789012l, ioBuffer.getLong());
    assertEquals(-23456789023l, ioBuffer.getLong());
    ioBuffer.rewind();
    ioBuffer.putLong(4, 1234567890);
    assertEquals(1234567890, ioBuffer.getLong(4));
    try {
      ioBuffer.putLong(9, 1);
      fail("Not enough place on the buffer");
    } catch (BufferUnderflowException e) {
      // Should come here
    }
    try {
      ioBuffer.getLong(9);
      fail("Not enough place on the buffer");
    } catch (BufferUnderflowException e) {
      // Should come here
    }
  }
}
origin: apache/mina

for (ByteOrder bo : new ByteOrder[] { ByteOrder.BIG_ENDIAN, ByteOrder.LITTLE_ENDIAN }) {
  ByteBuffer bb = (ByteBuffer) ByteBuffer.allocate(3).order(bo).putChar('\u00EB').rewind();
  IoBuffer ioBuffer = IoBuffer.wrap(bb).order(bo);
  assertEquals(3, ioBuffer.capacity());
  ioBuffer.extend(1);
  ioBuffer.order(bo);
  ioBuffer.position(2);
  assertEquals(4, ioBuffer.capacity());
  ioBuffer.putChar('\u00FC');
  ioBuffer.rewind();
  assertEquals('\u00EB', ioBuffer.getChar());
  assertEquals('\u00FC', ioBuffer.getChar());
  ioBuffer.rewind();
  ioBuffer.putChar(1, '\u00E7');
  assertEquals('\u00E7', ioBuffer.getChar(1));
    ioBuffer.putChar(3, '\u00F1');
    fail("Not enough place on the buffer");
  } catch (BufferUnderflowException e) {
    ioBuffer.getChar(3);
    fail("Not enough place on the buffer");
  } catch (BufferUnderflowException e) {
origin: apache/mina

@Test
public void testShort() {
  for (ByteOrder bo : new ByteOrder[] { ByteOrder.BIG_ENDIAN, ByteOrder.LITTLE_ENDIAN }) {
    ByteBuffer bb = (ByteBuffer) ByteBuffer.allocate(3).order(bo).putShort((short) 12345).rewind();
    IoBuffer ioBuffer = IoBuffer.wrap(bb).order(bo);
    assertEquals(3, ioBuffer.capacity());
    ioBuffer.extend(1);
    ioBuffer.position(2);
    assertEquals(4, ioBuffer.capacity());
    ioBuffer.putShort((short) -23456);
    ioBuffer.rewind();
    assertEquals(12345, ioBuffer.getShort());
    assertEquals(-23456, ioBuffer.getShort());
    ioBuffer.rewind();
    ioBuffer.putShort(1, (short) 12345);
    assertEquals((short) 12345, ioBuffer.getShort(1));
    try {
      ioBuffer.putShort(3, (short) 1);
      fail("Not enough place on the buffer");
    } catch (BufferUnderflowException e) {
      // Should come here
    }
    try {
      ioBuffer.getShort(3);
      fail("Not enough place on the buffer");
    } catch (BufferUnderflowException e) {
      // Should come here
    }
  }
}
origin: apache/mina

@Test
public void testInt() {
  for (ByteOrder bo : new ByteOrder[] { ByteOrder.BIG_ENDIAN, ByteOrder.LITTLE_ENDIAN }) {
    ByteBuffer bb = (ByteBuffer) ByteBuffer.allocate(5).order(bo).putInt(123456).rewind();
    IoBuffer ioBuffer = IoBuffer.wrap(bb).order(bo);
    assertEquals(5, ioBuffer.capacity());
    ioBuffer.extend(3);
    ioBuffer.position(4);
    assertEquals(8, ioBuffer.capacity());
    ioBuffer.putInt(-23456789);
    ioBuffer.rewind();
    assertEquals(123456, ioBuffer.getInt());
    assertEquals(-23456789, ioBuffer.getInt());
    ioBuffer.rewind();
    ioBuffer.putInt(2, 1234567890);
    assertEquals(1234567890, ioBuffer.getInt(2));
    try {
      ioBuffer.putInt(5, 1);
      fail("Not enough place on the buffer");
    } catch (BufferUnderflowException e) {
      // Should come here
    }
    try {
      ioBuffer.getInt(5);
      fail("Not enough place on the buffer");
    } catch (BufferUnderflowException e) {
      // Should come here
    }
  }
}
origin: apache/mina

@Test
public void testCompact() {
  ByteBuffer bb1 = ByteBuffer.allocate(5);
  bb1.put("012".getBytes());
  bb1.flip();
  ByteBuffer bb2 = ByteBuffer.allocate(5);
  bb2.put("345".getBytes());
  bb2.flip();
  ByteBuffer bb3 = ByteBuffer.allocate(5);
  bb3.put("6789".getBytes());
  bb3.flip();
  IoBuffer ioBuffer = IoBuffer.newInstance();
  ioBuffer.add(bb1, bb2).add(bb3);
  ioBuffer.position(2);
  ioBuffer.limit(8);
  ioBuffer.compact();
  assertEquals(ioBuffer.capacity(), ioBuffer.limit());
  assertEquals(6, ioBuffer.position());
  byte seg[] = "234567".getBytes();
  for (int i = 0; i < 6; i++) {
    assertEquals(seg[i], ioBuffer.get(i));
  }
}
origin: apache/mina

/**
 * @see ByteBuffer#put(ByteBuffer)
 */
public IoBuffer put(IoBuffer src) {
  if (src == this) { // NOSONAR, checking the instance
    throw new IllegalArgumentException();
  }
  if (remaining() < src.remaining()) {
    throw new BufferOverflowException();
  }
  if (isReadOnly()) {
    throw new ReadOnlyBufferException();
  }
  while (src.hasRemaining()) {
    put(src.get());
  }
  return this;
}
org.apache.mina.codecIoBuffer

Javadoc

A proxy class used to manage ByteBuffers as if they were just a big ByteBuffer. We can add as many buffers as needed when accumulating data. From the user point of view, the methods are the very same as ByteBuffer provides.

IoBuffer instances are *not* thread safe.

The IoBuffer uses a singly linked list to handle the multiple Buffers. Thus sequential access is very efficient and random access is not. It fits well with the common usage patterns of IoBuffer.

Most used methods

  • asInputStream
    Provides an input stream which is actually reading the IoBufferinstance. Further reads on the return
  • get
  • remaining
  • add
    Add one or more ByteBuffer to the current IoBuffer
  • array
  • capacity
  • getChar
  • getDouble
  • getFloat
  • getInt
  • getLong
  • getShort
  • getLong,
  • getShort,
  • hasRemaining,
  • isDirect,
  • limit,
  • order,
  • position,
  • put,
  • putChar,
  • putDouble

Popular in Java

  • Updating database using SQL prepared statement
  • getOriginalFilename (MultipartFile)
    Return the original filename in the client's filesystem.This may contain path information depending
  • scheduleAtFixedRate (ScheduledExecutorService)
  • setScale (BigDecimal)
  • Pointer (com.sun.jna)
    An abstraction for a native pointer data type. A Pointer instance represents, on the Java side, a na
  • HttpURLConnection (java.net)
    An URLConnection for HTTP (RFC 2616 [http://tools.ietf.org/html/rfc2616]) used to send and receive d
  • URLEncoder (java.net)
    This class is used to encode a string using the format required by application/x-www-form-urlencoded
  • LinkedList (java.util)
    Doubly-linked list implementation of the List and Dequeinterfaces. Implements all optional list oper
  • Scanner (java.util)
    A parser that parses a text string of primitive types and strings with the help of regular expressio
  • StringTokenizer (java.util)
    Breaks a string into tokens; new code should probably use String#split.> // Legacy code: StringTo
  • From CI to AI: The AI layer in your organization
Tabnine Logo
  • Products

    Search for Java codeSearch for JavaScript code
  • IDE Plugins

    IntelliJ IDEAWebStormVisual StudioAndroid StudioEclipseVisual Studio CodePyCharmSublime TextPhpStormVimGoLandRubyMineEmacsJupyter NotebookJupyter LabRiderDataGripAppCode
  • Company

    About UsContact UsCareers
  • Resources

    FAQBlogTabnine AcademyTerms of usePrivacy policyJava Code IndexJavascript Code Index
Get Tabnine for your IDE now