|
| 1 | +import java.io.*; |
| 2 | + |
| 3 | +public class Paasio implements Closeable { |
| 4 | + |
| 5 | + private long bytesRead; |
| 6 | + private long readOperationCount; |
| 7 | + private long bytesWritten; |
| 8 | + private long writeOperationCount; |
| 9 | + |
| 10 | + private final InputStream inputStream; |
| 11 | + private final OutputStream outputStream; |
| 12 | + |
| 13 | + public Paasio(InputStream inputStream, OutputStream outputStream) throws FileNotFoundException { |
| 14 | + this.inputStream = inputStream; |
| 15 | + this.outputStream = outputStream; |
| 16 | + } |
| 17 | + |
| 18 | + public int read() throws IOException { |
| 19 | + int byteData = inputStream.read(); |
| 20 | + if (byteData != -1) { |
| 21 | + bytesRead += 1; |
| 22 | + readOperationCount++; |
| 23 | + } |
| 24 | + return byteData; |
| 25 | + } |
| 26 | + |
| 27 | + public int read(byte[] b) throws IOException { |
| 28 | + |
| 29 | + int totalBytesRead = inputStream.read(b); |
| 30 | + if (totalBytesRead != -1) { |
| 31 | + bytesRead += totalBytesRead; |
| 32 | + readOperationCount++; |
| 33 | + } |
| 34 | + return totalBytesRead; |
| 35 | + |
| 36 | + } |
| 37 | + |
| 38 | + public int read(byte[] b, int off, int len) throws IOException { |
| 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 | + |
| 49 | + } |
| 50 | + |
| 51 | + public byte[] readAllBytes() throws IOException { |
| 52 | + |
| 53 | + byte[] allData = this.inputStream.readAllBytes(); |
| 54 | + |
| 55 | + if (allData.length > 0) { |
| 56 | + readOperationCount++; |
| 57 | + bytesRead += allData.length; |
| 58 | + } |
| 59 | + return allData; |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + public byte[] readNBytes(int len) throws IOException { |
| 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; |
| 72 | + } |
| 73 | + |
| 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 | + } |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | + public void write(byte[] b) throws IOException { |
| 88 | + try { |
| 89 | + |
| 90 | + this.outputStream.write(b); |
| 91 | + writeOperationCount++; |
| 92 | + bytesWritten += b.length; |
| 93 | + |
| 94 | + |
| 95 | + } catch (IOException ioException) { |
| 96 | + ioException.printStackTrace(); |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 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 | + } |
| 109 | + } |
| 110 | + |
| 111 | + public long getBytesRead() { |
| 112 | + return bytesRead; |
| 113 | + } |
| 114 | + |
| 115 | + public long getReadOperationCount() { |
| 116 | + return readOperationCount; |
| 117 | + } |
| 118 | + |
| 119 | + public long getBytesWritten() { |
| 120 | + return bytesWritten; |
| 121 | + } |
| 122 | + |
| 123 | + public long getWriteOperationCount() { |
| 124 | + return writeOperationCount; |
| 125 | + } |
| 126 | + |
| 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 | + } |
| 135 | + } |
| 136 | + |
| 137 | +} |
0 commit comments