ByteArrayInputStream类

Posted kanhin

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ByteArrayInputStream类相关的知识,希望对你有一定的参考价值。

这个类是利用内存数组去存储,底层传输数据利用Systm.arrycopy

 

 

package io;

import java.io.IOException;

import java.util.Arrays;

/**
 * @author LH
 * 这个是利用内存数组存储byte数组
 */
public class LHByteArrayInputStream extends LHInputStream {


    /**
     * 内存数组
     */
    protected byte buf[];

    /**
     * 位置参数
     */
    protected int pos;

    /**
     * 标记参数
     */
    protected int mark;

    /**
     * 个数参数
     */
    protected int count;

    /**
     * 单个参数构造函数
     *
     * @param buf
     */
    public LHByteArrayInputStream(byte[] buf) {
        this.buf = buf;
        this.pos = 0;
        this.count = buf.length;
    }

    /**
     * 多参数构造函数
     *
     * @param buf
     * @param offset 偏移
     * @param length 长度
     */
    public LHByteArrayInputStream(byte[] buf, int offset, int length) {
        this.buf = buf;
        this.pos = offset;
        this.count = Math.min(offset + length, buf.length);
        this.mark = offset;
    }

    @Override
    public synchronized int read() throws IOException {
        //0xff作用是为了裁剪
        return (pos < count) ? (buf[pos++] & 0xff) : -1;
    }

    @Override
    public synchronized int read(byte[] b, int off, int len) {

        //检查参数合法性
        boolean argsIllegaled = checkParamArgumentLegaled(b, off, len);

        if (!argsIllegaled) {
            return 0;
        }

        //如果当前的位置大于总尺寸 则返回-1
        if (pos >= count) {
            return -1;
        }

        int avail = count - pos;

        //如果要读取的长度大于可读 则把刻度的大小传递给长度
        if (len > avail) {
            len = avail;
        }

        //数组拷贝
        System.arraycopy(buf, pos, b, off, len);
        //位置偏移
        pos += len;
        //返回度过的长度
        return len;

    }

    /**
     * 跳过和丢弃
     *
     * @param n
     * @return
     */
    @Override
    public synchronized long skip(long n) {
        //查询当前剩下的位置个数
        long k = count - pos;

        //判断参数
        if (n < k) {
            k = n < 0 ? 0 : n;
        }

        pos += k;

        return k;
    }

    @Override
    public synchronized int available() {
        return count - pos;
    }

    @Override
    public boolean markSupported() {
        return true;
    }

    /**
     * 参数并没有什么卵用
     *
     * @param readAheadLimit
     */
    @Override
    public void mark(int readAheadLimit) {
        mark = pos;
    }

    @Override
    public synchronized void reset() {
        pos = mark;
    }

    @Override
    public void close() throws IOException {

    }

    public static void main(String[] args) throws IOException {
        byte[] source = "HelloWorld".getBytes();
        LHInputStream inputStream = new LHByteArrayInputStream(source);

        byte[] target = new byte[512];
        
        int length = inputStream.read(target);

        System.out.println(new String(target, 0, length));
    }
}

  

以上是关于ByteArrayInputStream类的主要内容,如果未能解决你的问题,请参考以下文章

JAVA IO流相关代码(ByteArrayInputStream类和ByteArrayOutputStream类相关代码)

ByteArrayInputStream类

java内存流:java.io.ByteArrayInputStreamjava.io.ByteArrayOutputStreamjava.io.CharArrayReaderjava.io(代码片段

Java 流(Stream)文件(File)和IO -- Java ByteArrayInputStream类

Java 输入流ByteArrayInputStream

字节数组流ByteArrayInputStream&ByteArrayOutputStream