扩展 InputStream 时出现问题 class
issue while extending InputStream class
我正在尝试扩展 InputStream class 并使用自定义的 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
{
//..................
}
}
}
这是我的客户class
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 Class 的读取方法。
我不知道我在这里错过了什么。
如果你正在扩展输入流class那么你需要给出以下方法的具体定义:
public abstract int read() throws IOException
您的 class 具有签名为的读取方法:
public int read(byte[] b) throws IOException
所以除了 read(byte[] b)
之外,请实施 read()
。我做了一些修改,现在可以使用了...
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.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 = obj1.getInputStream().read(buffer);
return i;
} catch (IOException ex) {
return -1;
}
}
public static void main(String args[]) {
MyClient cl1 = new MyClient();
int ret = 0;
byte[] data = {'a','b'};
ret = cl1.read(data);
System.out.println(ret);
}
}
import java.io.IOException;
import java.io.InputStream;
class MyClass {
/** Input stream */
private final MyInputStream in = new MyInputStream();
//get the InputStream
public InputStream getInputStream() {
return in;
}
class MyInputStream extends InputStream {
//here i am keeping implementation of read methods
public int read( byte b[] ) throws IOException {
System.out.println("Inside my read()");
return b.length;
//..................
}
@Override
public int read() throws IOException {
// TODO Auto-generated method stub
return 0;
}
}
}
我使用 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);
}
}
我正在尝试扩展 InputStream class 并使用自定义的 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
{
//..................
}
}
}
这是我的客户class
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 Class 的读取方法。
我不知道我在这里错过了什么。
如果你正在扩展输入流class那么你需要给出以下方法的具体定义:
public abstract int read() throws IOException
您的 class 具有签名为的读取方法:
public int read(byte[] b) throws IOException
所以除了 read(byte[] b)
之外,请实施 read()
。我做了一些修改,现在可以使用了...
import java.io.BufferedInputStream;
import java.io.DataInputStream;
import java.io.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 = obj1.getInputStream().read(buffer);
return i;
} catch (IOException ex) {
return -1;
}
}
public static void main(String args[]) {
MyClient cl1 = new MyClient();
int ret = 0;
byte[] data = {'a','b'};
ret = cl1.read(data);
System.out.println(ret);
}
}
import java.io.IOException;
import java.io.InputStream;
class MyClass {
/** Input stream */
private final MyInputStream in = new MyInputStream();
//get the InputStream
public InputStream getInputStream() {
return in;
}
class MyInputStream extends InputStream {
//here i am keeping implementation of read methods
public int read( byte b[] ) throws IOException {
System.out.println("Inside my read()");
return b.length;
//..................
}
@Override
public int read() throws IOException {
// TODO Auto-generated method stub
return 0;
}
}
}
我使用 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);
}
}