• Home
  • About

Snippet IT

IT News, Programming, Internet and Blogging

  • Programming and Scripting
  • Tips and Tricks
  • Software and Hardware
  • New and Happening
You are here: Home / Programming and Scripting / Java: Use ByteBuffer As InputStream

Java: Use ByteBuffer As InputStream

January 12, 2010 by Sze Hau Leave a Comment

Sometime, your program may need to read data from a ByteBuffer buffer into a InputStream object. There is no class in Java library that provide the facility to do the conversion.

Anyway, to use a ByteBuffer object as an InputStream is pretty simple. What you need to do is to write a class wrapper that inherit InputStream and override the read() function in InputStream.

Here is the example:

public class ByteBufferInputStream extends InputStream {

  private int bbisInitPos;
  private int bbisLimit;
  private ByteBuffer bbisBuffer;

  public ByteBufferInputStream(ByteBuffer buffer) {
    this(buffer, buffer.limit() - buffer.position());
  }

  public ByteBufferInputStream(ByteBuffer buffer, int limit) {
    bbisBuffer = buffer;
    bbisLimit = limit;
    bbisInitPos = bbisBuffer.position();
  }

  @Override
  public int read() throws IOException {
    if (bbisBuffer.position() - bbisInitPos > bbisLimit)
      return -1;
    return bbisBuffer.get();
  }
}

In this class, ByteBufferInputStream, you can specify the limit to read from the ByteBuffer so that you can use the data in ByteBuffer later for other purposes. The read behavior is same as other InputStream class, where when there is no more data to read (or reached the preset limit), the read() function returns negative one.

Filed Under: Programming and Scripting, Tips and Tricks Tagged With: ByteBuffer, InputStream, Java, Tips and Tricks

About Sze Hau

Geek. Love programming. Coffee addicted. Married with two children. Working towards financial freedom.

Leave a ReplyCancel reply

Advertisement

Email News Letter

Sign up to receive updates daily and to hear what's going on with us

Software and Hardware

MD5 and SHA1 Checksum Using Windows

July 5, 2017 By Sze Hau Leave a Comment

Blog Network

  • Personal Fincance Personal Finance – Personal Money Tips, Stock Investment, Small Business and Make Money Online
  • szehau's weblog Life, Internet, Software, Gadgets, Programming and Investments

Snippet IT

This is the place where I want to share anything about information technology.

Search

Recent

  • MD5 and SHA1 Checksum Using Windows
  • MD5 and SHA1 Checksum Using Linux
  • Java: Unlimited Strength Jurisdiction Policy
  • WordPress: How To Change Admin Username
  • Linux: How To Compress And Decompress Folders And Files

Tags

advertisement blog C# Chrome DecimalFormat EGPC Float format format Integer free icons Gmail Google Google Adsense Google Chrome Google Search Engine Google search result how to HTTP Java JavaScript Linux money password performance PHP programming search engine optimization secure security short URL site value spam SQL static constructor String thread tiny URL Tips and Tricks trackback twitter video Wordpress wordpress plugin wordpress theme Youtube

Copyright © 2025 · Magazine Pro Theme on Genesis Framework · WordPress · Log in