【发布时间】:2015-03-26 10:39:44
【问题描述】:
我正在尝试扩展 InputStream 类并使用自定义的 read() 方法。 这是我的课堂快照:
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 类的读取方法。
我不知道我在这里缺少什么。
【问题讨论】:
标签: java inputstream datainputstream bufferedinputstream