|
1 | 1 | import java.io.*; |
2 | 2 |
|
3 | | -class FileOperations implements Closeable { |
4 | | - |
5 | | - private FileInputStream fileInputStream; |
6 | | - private FileOutputStream fileOutputStream; |
| 3 | +public class Paasio implements Closeable { |
7 | 4 |
|
8 | 5 | private long bytesRead; |
9 | | - private long bytesWritten; |
10 | 6 | private long readOperationCount; |
| 7 | + private long bytesWritten; |
11 | 8 | private long writeOperationCount; |
12 | | - private String mode; |
13 | 9 |
|
14 | | - public FileOperations(File file, boolean readOperation){ |
| 10 | + private final InputStream inputStream; |
| 11 | + private final OutputStream outputStream; |
15 | 12 |
|
| 13 | + public Paasio(InputStream inputStream, OutputStream outputStream) throws FileNotFoundException { |
| 14 | + this.inputStream = inputStream; |
| 15 | + this.outputStream = outputStream; |
16 | 16 | } |
17 | 17 |
|
18 | 18 | public int read() throws IOException { |
19 | | - |
20 | | - if (this.mode.equals("r")) { |
21 | | - |
22 | | - int byteData = fileInputStream.read(); |
23 | | - if (byteData != -1) { |
24 | | - bytesRead += byteData; |
25 | | - readOperationCount += 1; |
26 | | - } |
27 | | - return byteData; |
28 | | - } else { |
29 | | - throw new UnsupportedOperationException("Not in read mode."); |
| 19 | + int byteData = inputStream.read(); |
| 20 | + if (byteData != -1) { |
| 21 | + bytesRead += 1; |
| 22 | + readOperationCount++; |
30 | 23 | } |
| 24 | + return byteData; |
31 | 25 | } |
32 | 26 |
|
33 | 27 | public int read(byte[] b) throws IOException { |
34 | 28 |
|
35 | | - return -1; |
| 29 | + int totalBytesRead = inputStream.read(b); |
| 30 | + if (totalBytesRead != -1) { |
| 31 | + bytesRead += totalBytesRead; |
| 32 | + readOperationCount++; |
| 33 | + } |
| 34 | + return totalBytesRead; |
| 35 | + |
36 | 36 | } |
37 | 37 |
|
38 | 38 | public int read(byte[] b, int off, int len) throws IOException { |
39 | | - return -1; |
| 39 | + |
| 40 | + int bytesReadIntoBuffer = inputStream.read(b, off, len); |
| 41 | + |
| 42 | + if (bytesReadIntoBuffer != -1) { |
| 43 | + bytesRead += bytesReadIntoBuffer; |
| 44 | + readOperationCount++; |
| 45 | + } |
| 46 | + return bytesReadIntoBuffer; |
| 47 | + |
| 48 | + |
40 | 49 | } |
41 | 50 |
|
42 | 51 | public byte[] readAllBytes() throws IOException { |
43 | | - return new byte[0]; |
| 52 | + |
| 53 | + byte[] allData = this.inputStream.readAllBytes(); |
| 54 | + |
| 55 | + if (allData.length > 0) { |
| 56 | + readOperationCount++; |
| 57 | + bytesRead += allData.length; |
| 58 | + } |
| 59 | + return allData; |
| 60 | + |
44 | 61 | } |
45 | 62 |
|
46 | 63 | public byte[] readNBytes(int len) throws IOException { |
47 | | - return new byte[0]; |
| 64 | +// return new byte[0]; |
| 65 | + |
| 66 | + byte[] allData = this.inputStream.readNBytes(len); |
| 67 | + if (allData.length > 0) { |
| 68 | + readOperationCount++; |
| 69 | + bytesRead += allData.length; |
| 70 | + } |
| 71 | + return allData; |
48 | 72 | } |
49 | 73 |
|
50 | | - @Override |
51 | | - public void close() throws IOException { |
| 74 | + public void write(int b) throws IOException { |
| 75 | + |
| 76 | + try { |
| 77 | + this.outputStream.write(b); |
| 78 | + writeOperationCount++; |
| 79 | + bytesWritten++; |
| 80 | + |
| 81 | + } catch (IOException ioException) { |
| 82 | + ioException.printStackTrace(); |
| 83 | + } |
52 | 84 |
|
53 | 85 | } |
54 | 86 |
|
| 87 | + public void write(byte[] b) throws IOException { |
| 88 | + try { |
55 | 89 |
|
56 | | - //Getter Setter |
| 90 | + this.outputStream.write(b); |
| 91 | + writeOperationCount++; |
| 92 | + bytesWritten += b.length; |
57 | 93 |
|
58 | | - public long getBytesRead() { |
59 | | - return bytesRead; |
60 | | - } |
61 | 94 |
|
62 | | - public void setBytesRead(long bytesRead) { |
63 | | - this.bytesRead = bytesRead; |
| 95 | + } catch (IOException ioException) { |
| 96 | + ioException.printStackTrace(); |
| 97 | + } |
64 | 98 | } |
65 | 99 |
|
66 | | - public long getBytesWritten() { |
67 | | - return bytesWritten; |
| 100 | + public void write(byte[] b, int off, int len) throws IOException { |
| 101 | + try { |
| 102 | + this.outputStream.write(b, off, len); |
| 103 | + writeOperationCount++; |
| 104 | + bytesWritten += len; |
| 105 | + |
| 106 | + } catch (IOException ioException) { |
| 107 | + ioException.printStackTrace(); |
| 108 | + } |
68 | 109 | } |
69 | 110 |
|
70 | | - public void setBytesWritten(long bytesWritten) { |
71 | | - this.bytesWritten = bytesWritten; |
| 111 | + public long getBytesRead() { |
| 112 | + return bytesRead; |
72 | 113 | } |
73 | 114 |
|
74 | 115 | public long getReadOperationCount() { |
75 | 116 | return readOperationCount; |
76 | 117 | } |
77 | 118 |
|
78 | | - public void setReadOperationCount(long readOperationCount) { |
79 | | - this.readOperationCount = readOperationCount; |
| 119 | + public long getBytesWritten() { |
| 120 | + return bytesWritten; |
80 | 121 | } |
81 | 122 |
|
82 | 123 | public long getWriteOperationCount() { |
83 | 124 | return writeOperationCount; |
84 | 125 | } |
85 | 126 |
|
86 | | - public void setWriteOperationCount(long writeOperationCount) { |
87 | | - this.writeOperationCount = writeOperationCount; |
88 | | - } |
89 | | - |
90 | | - public String getMode() { |
91 | | - return mode; |
| 127 | + @Override |
| 128 | + public void close() throws IOException { |
| 129 | + if (this.inputStream != null) { |
| 130 | + this.inputStream.close(); |
| 131 | + } |
| 132 | + if (this.outputStream != null) { |
| 133 | + this.outputStream.close(); |
| 134 | + } |
92 | 135 | } |
93 | 136 |
|
94 | | - public void setMode(String mode) { |
95 | | - this.mode = mode; |
96 | | - } |
97 | 137 | } |
98 | | - |
99 | | - |
|
0 commit comments