All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.xiaomi.infra.galaxy.talos.client.compression.ByteBufferBackedInputStream Maven / Gradle / Ivy

There is a newer version: 2.6.1.4
Show newest version
/**
 * Copyright 2015, Xiaomi.
 * All rights reserved.
 * Author: [email protected]
 */

package com.xiaomi.infra.galaxy.talos.client.compression;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;

public class ByteBufferBackedInputStream extends InputStream{
  private ByteBuffer buffer;
  public ByteBufferBackedInputStream(ByteBuffer byteBuffer) {
    this.buffer = byteBuffer;
  }

  @Override
  public int read() throws IOException {
    if (buffer.hasRemaining()) {
      return buffer.get() & 0xFF;
    } else {
      return -1;
    }
  }

  @Override
  public int read(byte[] bytes, int off, int len) throws IOException {
    if (buffer.hasRemaining()) {
      int readLen = Math.min(len, buffer.remaining());
      buffer.get(bytes, off, readLen);
      return readLen;
    } else {
      return -1;
    }
  }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy