Class ByteBufferUtils

java.lang.Object
uk.ac.manchester.spinnaker.utils.ByteBufferUtils

public abstract class ByteBufferUtils
extends Object
Utility methods for ByteBuffers.
  • Method Details

    • slice

      public static ByteBuffer slice​(ByteBuffer src, int from, int len)
      Make a slice of a byte buffer without modifying the original buffer.
      Parameters:
      src - The originating buffer.
      from - The offset into the originating buffer where the slice starts.
      len - The length of the slice.
      Returns:
      The little-endian slice. This will be read-only if and only if the original buffer is read-only.
    • limitSlice

      public static ByteBuffer limitSlice​(ByteBuffer src, int maxSize)
      Make a slice of a byte buffer if it exceeds a given size.
      Parameters:
      src - The originating buffer.
      maxSize - The maximum size of the resulting buffer.
      Returns:
      The original buffer or a little-endian slice. This will be read-only if and only if the original buffer is read-only.
    • sliceUp

      public static MappableIterable<ByteBuffer> sliceUp​(ByteBuffer src, int chunkSize)
      Slice up a buffer buffer into a sequence of (little-endian) byte buffers with a maximum size.
      Parameters:
      src - The buffer to slice up.
      chunkSize - Max size of each chunk. Must be positive.
      Returns:
      An iterable of little-endian chunks. Only the final chunk will be smaller than the requested chunk size.
    • read

      public static ByteBuffer read​(InputStream data, ByteBuffer workingBuffer, int maxRemaining) throws IOException
      Read a chunk of an input stream into a byte buffer.
      Parameters:
      data - Where to read from.
      workingBuffer - The buffer to use. The number of bytes remaining in the buffer is the maximum number of bytes to read unless the maximum bytes remaining is smaller.
      maxRemaining - The maximum number of bytes remaining in the input stream; bytes after that point won't be read even if they exist.
      Returns:
      A view on the buffer with the data in it (endianness undefined), or null if EOF or the limit is reached.
      Throws:
      IOException - If reading fails.