gpt4 book ai didi

java - 扩展 InputStream 类时出现问题

转载 作者:行者123 更新时间:2023-12-01 11:58:25 25 4
gpt4 key购买 nike

我正在尝试扩展InputStream类并使用自定义的read()方法。这是我的类(class)快照:

class MyClass
{
/** Input stream */
private final MyInputStream in = new MyInputStream();
/**get the InputStream
public InputStream getInputStream()
{
return in;
}

/** Inner class for MyInputStream */
class MyInputStream extends InputStream
{
//here i am keeping implementation of read methods
public synchronized int read( byte b[] ) throws IOException
{
//..................
}
}
}

这是我的客户类别

public class MyClient {
//InStreams
protected BufferedInputStream mBufInStream;
protected DataInputStream mInStream;

public int read(byte[] buffer)
{
MyClass obj1 = new MyClass();
mBufInStream = new BufferedInputStream(obj1.getInputStream());
mInStream = new DataInputStream(mBufInStream);
try
{
int i = mBufInStream.read(buffer);
return i;
}
catch (IOException ex)
{
return -1;
}
}

public static void main(String args[])
{
MyClient cl1 = new MyClient();
int ret = 0;
byte[] data = {};

ret = cl1.read(data);

}
}

我想做的是在 cl1.read 完成时调用 MyInputStream 类的读取方法。

我不知道我在这里错过了什么。

最佳答案

我使用 MyInputStream 创建了 DataInputStream 对象并使其正常工作。这是更新后的代码:

public class MyClient {
//InStreams
protected DataInputStream mInStream;

public int read(byte[] buffer)
{
MyClass obj1 = new MyClass();
mInStream = new DataInputStream(obj1.getInputStream());
try
{
int i = mInStream.read(buffer);
return i;
}
catch (IOException ex)
{
return -1;
}
}

public static void main(String args[])
{
MyClient cl1 = new MyClient();
int ret = 0;
byte[] data = {};

ret = cl1.read(data);

}
}

关于java - 扩展 InputStream 类时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28176858/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com