• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.fasterxml.jackson.databind.util;
2 
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.nio.ByteBuffer;
6 
7 /**
8  * Simple {@link InputStream} implementation that exposes currently
9  * available content of a {@link ByteBuffer}.
10  */
11 public class ByteBufferBackedInputStream extends InputStream {
12     protected final ByteBuffer _b;
13 
ByteBufferBackedInputStream(ByteBuffer buf)14     public ByteBufferBackedInputStream(ByteBuffer buf) { _b = buf; }
15 
available()16     @Override public int available() { return _b.remaining(); }
17 
18     @Override
read()19     public int read() throws IOException { return _b.hasRemaining() ? (_b.get() & 0xFF) : -1; }
20 
21     @Override
read(byte[] bytes, int off, int len)22     public int read(byte[] bytes, int off, int len) throws IOException {
23         if (!_b.hasRemaining()) return -1;
24         len = Math.min(len, _b.remaining());
25         _b.get(bytes, off, len);
26         return len;
27     }
28 }