简体   繁体   English

InputStream的read()方法如何实现?

[英]How InputStream's read() method is implemented?

For the specific task I am trying to override read() method for my custom class that extends InputStream. 对于特定的任务,我试图为扩展InputStream的自定义类重写read()方法。

So far my implementation is: 到目前为止,我的实现是:

private ArrayList<byte[]> inputBuffer = new ArrayList<>(); 
...
@Override
public int read(@NonNull byte[] b) throws IOException {
    if (inputBuffer.size() > 0) {
        b = inputBuffer.get(0);
        inputBuffer.remove(0);
    } else
        return -1;

    return b.length;
}

And I am adding data to my InputStream like this: 我正在像这样向我的InputStream添加数据:

boolean writeDataToInputStream(byte[] data) {
    int arrSize = inputBuffer.size();
    if (data.length > 0) {
        inputBuffer.add(data);
    }
    return arrSize < inputBuffer.size();
}

I've read documentation, I know how this method works by default. 我已经阅读了文档,我知道默认情况下该方法的工作方式。 But I need somehow to pass ArrayList element to input parameter byte[] b . 但是我需要以某种方式将ArrayList元素传递给输入参数byte[] b

I've been coding in java for few years and yet I've never put attention to how this method is actually implemented. 我从事Java编程已经有好几年了,但是我从未关注过这种方法的实际实现方式。 How can I pass data to an incoming parameter and return the number of bytes from my ArrayList's element written? 如何将数据传递到传入参数,并从写入的ArrayList元素中返回字节数?

I have to use custom Socket specifically for BLE w/ Input and Output stream due to my architecture, where I use WiFi socket, BT socket. 由于我的体系结构,我必须使用专门用于BLE w /输入和输出流的自定义套接字,其中使用WiFi套接字,BT套接字。

Please uncover this mystery for me. 请为我揭开这个谜。

When you create your own InputStream , the only method that you have to implement since it is an abstract method is read() which is also much less error prone than implementing read(byte[] b) and/or read(byte b[], int off, int len) . 创建自己的InputStream ,由于它是一个abstract方法,因此您唯一需要实现的方法是read() ,它比实现read(byte[] b)和/或read(byte b[], int off, int len)错误发生率要小得多。 read(byte b[], int off, int len) Moreover please note that the default implementation of read(byte b[], int off, int len) already checks the arguments for you so unless you want to revalidate the arguments yourself, you should implement read() only. 此外,请注意read(byte b[], int off, int len)的默认实现已经为您检查了参数,因此,除非您想自己重新验证参数,否则应仅实现read()

So in your case this method could be: 因此,在您的情况下,此方法可能是:

// Current index in the last byte array read
private int index;
private List<byte[]> inputBuffer = new ArrayList<>();
...
@Override
public int read() throws IOException {
   if (inputBuffer.isEmpty()) {
        return -1;
    }
    // Get first element of the List
    byte[] bytes = inputBuffer.get(0);
    // Get the byte corresponding to the index and post increment the current index
    byte result = bytes[index++];
    if (index >= bytes.length) {
        // It was the last index of the byte array so we remove it from the list
        // and reset the current index
        inputBuffer.remove(0);
        index = 0;
    }
    return result;
}

However if you really want to implement read(byte b[], int off, int len) , here is how it could look like: 但是,如果您确实要实现read(byte b[], int off, int len) ,则如下所示:

@Override
public int read(byte b[], int off, int len) throws IOException {
    // Check parameters
    if (b == null) {
        throw new NullPointerException();
    } else if (off < 0 || len < 0 || len > b.length - off) {
        throw new IndexOutOfBoundsException();
    } else if (len == 0) {
        return 0;
    }
    if (inputBuffer.isEmpty()) {
        return -1;
    }
    int read = 0;
    // Iterate as long as don't get the expected bytes amount and the list is not empty
    do {
        byte[] bytes = inputBuffer.get(0);
        int lg = Math.min(bytes.length - index, len);
        // Copy the bytes from "bytes" to "b"
        System.arraycopy(bytes, index, b, off, lg);
        // Update all counters
        read += lg;
        off += lg;
        index += lg;
        len -= lg;
        if (index >= bytes.length) {
            // It was the last index of the byte array so we remove it from the list
            // and reset the current index
            inputBuffer.remove(0);
            index = 0;
        }
    } while (read < len && !inputBuffer.isEmpty());

    return read;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM