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

How to use
ByteBuffer
in
java.nio

Best Java code snippets using java.nio.ByteBuffer (Showing top 20 results out of 59,778)

origin: spring-projects/spring-framework

private Object convertToByteBuffer(@Nullable Object source, TypeDescriptor sourceType) {
  byte[] bytes = (byte[]) (source instanceof byte[] ? source :
      this.conversionService.convert(source, sourceType, BYTE_ARRAY_TYPE));
  if (bytes == null) {
    return ByteBuffer.wrap(new byte[0]);
  }
  ByteBuffer byteBuffer = ByteBuffer.allocate(bytes.length);
  byteBuffer.put(bytes);
  // Extra cast necessary for compiling on JDK 9 plus running on JDK 8, since
  // otherwise the overridden ByteBuffer-returning rewind method would be chosen
  // which isn't available on JDK 8.
  return ((Buffer) byteBuffer).rewind();
}
origin: google/guava

@Override
public Hasher putBytes(ByteBuffer b) {
 if (b.hasArray()) {
  putBytes(b.array(), b.arrayOffset() + b.position(), b.remaining());
  b.position(b.limit());
 } else {
  for (int remaining = b.remaining(); remaining > 0; remaining--) {
   putByte(b.get());
  }
 }
 return this;
}
origin: apache/kafka

private void expandBuffer(int remainingRequired) {
  int expandSize = Math.max((int) (buffer.limit() * REALLOCATION_FACTOR), buffer.position() + remainingRequired);
  ByteBuffer temp = ByteBuffer.allocate(expandSize);
  int limit = limit();
  buffer.flip();
  temp.put(buffer);
  buffer.limit(limit);
  // reset the old buffer's position so that the partial data in the new buffer cannot be mistakenly consumed
  // we should ideally only do this for the original buffer, but the additional complexity doesn't seem worth it
  buffer.position(initialPosition);
  buffer = temp;
}
origin: google/guava

@Override
protected void processRemaining(ByteBuffer buffer) {
 b += buffer.remaining();
 for (int i = 0; buffer.hasRemaining(); i += 8) {
  finalM ^= (buffer.get() & 0xFFL) << i;
 }
}
origin: google/guava

/**
 * Flips the buffer output buffer so we can start reading bytes from it. If we are starting to
 * drain because there was overflow, and there aren't actually any characters to drain, then the
 * overflow must be due to a small output buffer.
 */
private void startDraining(boolean overflow) {
 byteBuffer.flip();
 if (overflow && byteBuffer.remaining() == 0) {
  byteBuffer = ByteBuffer.allocate(byteBuffer.capacity() * 2);
 } else {
  draining = true;
 }
}
origin: netty/netty

/**
 * Initialize an instance based upon the underlying storage from {@code value}.
 * There is a potential to share the underlying array storage if {@link ByteBuffer#hasArray()} is {@code true}.
 * if {@code copy} is {@code true} a copy will be made of the memory.
 * if {@code copy} is {@code false} the underlying storage will be shared, if possible.
 */
public AsciiString(ByteBuffer value, boolean copy) {
  this(value, value.position(), value.remaining(), copy);
}
origin: apache/incubator-dubbo

@Override
public void setBytes(int index, ByteBuffer src) {
  ByteBuffer data = buffer.duplicate();
  data.limit(index + src.remaining()).position(index);
  data.put(src);
}
origin: apache/incubator-dubbo

public static ChannelBuffer wrappedBuffer(ByteBuffer buffer) {
  if (!buffer.hasRemaining()) {
    return EMPTY_BUFFER;
  }
  if (buffer.hasArray()) {
    return wrappedBuffer(buffer.array(), buffer.arrayOffset() + buffer.position(), buffer.remaining());
  } else {
    return new ByteBufferBackedChannelBuffer(buffer);
  }
}
origin: spring-projects/spring-framework

private ByteBuffer assembleChunksAndReset() {
  ByteBuffer result;
  if (this.chunks.size() == 1) {
    result = this.chunks.remove();
  }
  else {
    result = ByteBuffer.allocate(getBufferSize());
    for (ByteBuffer partial : this.chunks) {
      result.put(partial);
    }
    result.flip();
  }
  this.chunks.clear();
  this.expectedContentLength = null;
  return result;
}
origin: bumptech/glide

 @Override
 public void updateDiskCacheKey(@NonNull MessageDigest messageDigest) {
  messageDigest.update(ID_BYTES);

  byte[] degreesData = ByteBuffer.allocate(4).putInt(degreesToRotate).array();
  messageDigest.update(degreesData);
 }
}
origin: google/guava

 /**
  * Copy as much of the byte buffer into the output array as possible, returning the (positive)
  * number of characters copied.
  */
 private int drain(byte[] b, int off, int len) {
  int remaining = Math.min(len, byteBuffer.remaining());
  byteBuffer.get(b, off, remaining);
  return remaining;
 }
}
origin: google/guava

@Override
public HashCode hashInt(int input) {
 return hashBytes(ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(input).array());
}
origin: google/guava

public void testFromByteArrayWithTooShortArrayInputThrowsIllegalArgumentException() {
 byte[] buffer = MANY_VALUES_STATS_VARARGS.toByteArray();
 byte[] tooShortByteArray =
   ByteBuffer.allocate(buffer.length - 1)
     .order(ByteOrder.LITTLE_ENDIAN)
     .put(buffer, 0, Stats.BYTES - 1)
     .array();
 try {
  Stats.fromByteArray(tooShortByteArray);
  fail("Expected IllegalArgumentException");
 } catch (IllegalArgumentException expected) {
 }
}
origin: google/guava

@Override
public final HashCode hash() {
 munch();
 buffer.flip();
 if (buffer.remaining() > 0) {
  processRemaining(buffer);
  buffer.position(buffer.limit());
 }
 return makeHash();
}
origin: spring-projects/spring-framework

private DataBuffer toDataBuffer(String body, Charset charset) {
  byte[] bytes = body.getBytes(charset);
  ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
  return this.bufferFactory.wrap(byteBuffer);
}
origin: google/guava

@Override
public HashCode hashLong(long input) {
 return hashBytes(ByteBuffer.allocate(8).order(ByteOrder.LITTLE_ENDIAN).putLong(input).array());
}
origin: google/guava

/** Returns a {@link HashCode} for a sequence of longs, in big-endian order. */
private static HashCode toHashCode(long... longs) {
 ByteBuffer bb = ByteBuffer.wrap(new byte[longs.length * 8]).order(ByteOrder.LITTLE_ENDIAN);
 for (long x : longs) {
  bb.putLong(x);
 }
 return HashCode.fromBytes(bb.array());
}
origin: google/guava

/**
 * This is invoked for the last bytes of the input, which are not enough to fill a whole chunk.
 * The passed {@code ByteBuffer} is guaranteed to be non-empty.
 *
 * <p>This implementation simply pads with zeros and delegates to {@link #process(ByteBuffer)}.
 */
protected void processRemaining(ByteBuffer bb) {
 bb.position(bb.limit()); // move at the end
 bb.limit(chunkSize + 7); // get ready to pad with longs
 while (bb.position() < chunkSize) {
  bb.putLong(0);
 }
 bb.limit(chunkSize);
 bb.flip();
 process(bb);
}
origin: apache/incubator-dubbo

@Override
public void getBytes(int index, ChannelBuffer dst, int dstIndex, int length) {
  if (dst instanceof ByteBufferBackedChannelBuffer) {
    ByteBufferBackedChannelBuffer bbdst = (ByteBufferBackedChannelBuffer) dst;
    ByteBuffer data = bbdst.buffer.duplicate();
    data.limit(dstIndex + length).position(dstIndex);
    getBytes(index, data);
  } else if (buffer.hasArray()) {
    dst.setBytes(dstIndex, buffer.array(), index + buffer.arrayOffset(), length);
  } else {
    dst.setBytes(dstIndex, this, index, length);
  }
}
origin: google/guava

/**
 * Gets a byte array representation of this instance.
 *
 * <p><b>Note:</b> No guarantees are made regarding stability of the representation between
 * versions.
 */
public byte[] toByteArray() {
 ByteBuffer buffer = ByteBuffer.allocate(BYTES).order(ByteOrder.LITTLE_ENDIAN);
 xStats.writeTo(buffer);
 yStats.writeTo(buffer);
 buffer.putDouble(sumOfProductsOfDeltas);
 return buffer.array();
}
java.nioByteBuffer

Javadoc

A buffer for bytes.

A byte buffer can be created in either one of the following ways:

  • #allocate(int) a new byte array and create a buffer based on it;
  • #allocateDirect(int) a memory block and create a direct buffer based on it;
  • #wrap(byte[]) an existing byte array to create a new buffer.

Most used methods

  • wrap
  • array
    Returns the byte array which this buffer is based on, if there is one.
  • get
  • allocate
    Creates a byte buffer based on a newly allocated byte array.
  • put
    Writes bytes in the given byte array, starting from the specified offset, to the current position an
  • position
  • remaining
  • limit
  • flip
  • putInt
    Writes the given int to the specified index of this buffer. The int is converted to bytes using the
  • getInt
    Returns the int at the specified index. The 4 bytes starting at the specified index are composed int
  • clear
  • getInt,
  • clear,
  • hasRemaining,
  • capacity,
  • order,
  • putLong,
  • getLong,
  • allocateDirect,
  • rewind,
  • getShort

Popular in Java

  • Making http post requests using okhttp
  • findViewById (Activity)
  • notifyDataSetChanged (ArrayAdapter)
  • getSharedPreferences (Context)
  • BorderLayout (java.awt)
    A border layout lays out a container, arranging and resizing its components to fit in five regions:
  • GridBagLayout (java.awt)
    The GridBagLayout class is a flexible layout manager that aligns components vertically and horizonta
  • ConnectException (java.net)
    A ConnectException is thrown if a connection cannot be established to a remote host on a specific po
  • LinkedList (java.util)
    Doubly-linked list implementation of the List and Dequeinterfaces. Implements all optional list oper
  • FileUtils (org.apache.commons.io)
    General file manipulation utilities. Facilities are provided in the following areas: * writing to a
  • Logger (org.slf4j)
    The org.slf4j.Logger interface is the main user entry point of SLF4J API. It is expected that loggin
  • Best IntelliJ plugins
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