- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
对于特定任务,我尝试重写扩展 InputStream 的自定义类的 read()
方法。
到目前为止我的实现是:
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;
}
我正在将数据添加到我的 InputStream
中,如下所示:
boolean writeDataToInputStream(byte[] data) {
int arrSize = inputBuffer.size();
if (data.length > 0) {
inputBuffer.add(data);
}
return arrSize < inputBuffer.size();
}
我已阅读文档,我知道此方法默认情况下如何工作。但我需要以某种方式将 ArrayList
元素传递给输入参数 byte[] b
。
我已经用java编写了几年代码,但我从未关注过这个方法是如何实际实现的。如何将数据传递给传入参数并返回 ArrayList 元素写入的字节数?
由于我的架构,我必须使用专门用于带输入和输出流的 BLE 的自定义套接字,其中我使用 WiFi 套接字、BT 套接字。
请为我揭开这个谜团。
最佳答案
当您创建自己的InputStream
时,由于它是一个抽象
方法,所以您必须实现的唯一方法是 read()
这也比实现 read(byte[] b) 和/或 read(byte b[], int off, int len) 更不容易出错。此外,请注意 read(byte b[], int off, int len)
的默认实现已经为您检查了参数,因此除非您想自己重新验证参数,否则您应该实现 仅 read()
。
所以在你的情况下这个方法可能是:
// 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;
}
但是,如果您确实想实现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;
}
关于java - InputStream的read()方法是如何实现的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40916355/
我在一个类中有两个方法: private static InputStream getSongStream(String ip, String id){ try { U
我创建了一个扩展 InputStream 的新类并且必须 @Override read()。我正在尝试使用方法 read(int b),但是当我使用它时,它会转到方法read() 和我不能使用参数,我
我正在尝试编写一个函数,该函数将接受带有压缩文件数据的 InputStream 并返回另一个带有解压缩数据的 InputStream。 压缩后的文件将只包含一个文件,因此不需要创建目录等... 我尝试
我想知道是否有任何理想的方式可以将多个 InputStream 链接到 Java(或 Scala)中的一个连续 InputStream 中。 我需要它来解析我从 FTP 服务器通过网络加载的平面文件。
我想做的是:打开大文件的 InputStream,按 10MB block 读取它,上传一个 block ,读取下一个 block 。 val chunkCount = Math.ceil(total
我不知道怎么理解: { if (inputStream **!= null**) { inputStream.close(); 来自那个例子: public c
我想知道 InputStream 是否为空,但不使用 read() 方法。有没有办法不读取就知道它是否为空? 最佳答案 不,你不能。 InputStream 设计用于处理远程资源,因此在实际读取它之前
我制作了一个蓝牙输入流监听器,只需询问 if(InputStream.isAvailable > 0) 即可检查是否有传入数据,然后创建一个循环将传入数据存储到 int[] 直到没有更多并且它工作完美
这是我的代码流程,文件内容丢失,我认为可能是 IOUtils.toByteArray() 行有问题,请指导这里实际出了什么问题。 文件内容丢失: InputStream stream = someCl
我从 HTTP 请求的响应开始: InputStream responseInputStream = response.getEntityInputStream() 我需要对该响应进行 gzip 压缩
用户将一个大文件上传到我的网站,我想对该文件进行 gzip 压缩并将其存储在 blob 中。所以我有一个未压缩的 InputStream,而 blob 需要一个 InputStream。我知道如何使用
我调用了一个返回压缩文件的服务。我从响应中将数据作为 InputStream(由 javax.activation.DataHandler.getInputStream(); 提供)提供。 我想做的是
我正在尝试压缩一个 InputStream 并返回一个 InputStream: public InputStream compress (InputStream in){ // Read "in
我最近在 Kotlin 中看到了将 InputStream 的全部内容读入 String 的代码,例如: // input is of type InputStream val baos = Byte
我正在尝试使用以下代码从 IHTTPSession.getInputStream() 读取 InputStream,但它每次都给出 Socket TimeOut Exception。 private
如 How to use Jersey interceptors to get request body 中所述,我正在修改 ContainerRequestFilter 中的 EntityInput
我正在编写一个需要与蓝牙 2.1 设备交换数据的应用程序。我已经做过好几次了,但这次发生了一些奇怪的事情。 Log.d("TAG", "connectToDevice"); if(ma
我只是在犹豫这是好主意还是坏主意: InputStreamReader in = new InputStreamReader(socket.getInputStream()); BufferedRea
我正在开发一个 Android 应用程序,它的 View 包含多个图库。图库的内容(位图)是来自 Internet 的红色。 对于第一个画廊,一切正常,但是当尝试下载第二个画廊的第一张图片时,Bitm
在Dart中,我想读取BMP,所以可能是BIG文件。 我这样做是这样的: var inputStream = imageFile.openInputStream(); inputStream.onDa
我是一名优秀的程序员,十分优秀!