我做了一個擴展InputStream的新類,並且有@Override read()。 我想使用方法read(int b),但是當我使用它時,它會轉到方法 read(),我不能使用參數,我通過了。inputStream擴展InputStream的新類
這是我的代碼:
public class Run {
public static void main(String[] args) {
DFSMaze3dGenerator mg = new DFSMaze3dGenerator();
try {
Maze3d maze3d = mg.generate(1, 5, 5);
maze3d.print3DMaze();
OutputStream out = new MyCompressorOutputStream(
new FileOutputStream("1.maz"));
out.write(maze3d.toByteArray());
byte[] arr = maze3d.toByteArray();
System.out.println("");
for (int i = 0; i < arr.length; i++) {
System.out.print(arr[i] + ",");
}
out.close();
InputStream in = new MyDecompressorInputStream(new FileInputStream(
"1.maz"));
byte b[] = new byte[maze3d.toByteArray().length];
in.read(b);
in.close();
Maze3d loaded = new Maze3d(b);
System.out.println(loaded.equals(maze3d));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我怎樣才能使用該參數時,我使用的方法:讀取(b)中; ???
public class MyDecompressorInputStream extends InputStream {
InputStream in;
int count;
boolean even = false;
public MyDecompressorInputStream(InputStream in) {
super();
this.in = in;
}
@Override
public int read() throws IOException {
return 100;
}
}
也許你應該重寫'讀(字節[])'? http://docs.oracle.com/javase/7/docs/api/java/io/InputStream.html#read(byte []) –
我做了它仍然去的方法read() –