Skip to content

Commit 71e0867

Browse files
committed
Code formatted
1 parent 0aeb078 commit 71e0867

3 files changed

Lines changed: 139 additions & 100 deletions

File tree

exercises/practice/paasio/.meta/src/reference/java/Paasio.java

Lines changed: 85 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,137 @@
11
import java.io.*;
22

3-
class FileOperations implements Closeable {
4-
5-
private FileInputStream fileInputStream;
6-
private FileOutputStream fileOutputStream;
3+
public class Paasio implements Closeable {
74

85
private long bytesRead;
9-
private long bytesWritten;
106
private long readOperationCount;
7+
private long bytesWritten;
118
private long writeOperationCount;
12-
private String mode;
139

14-
public FileOperations(File file, boolean readOperation){
10+
private final InputStream inputStream;
11+
private final OutputStream outputStream;
1512

13+
public Paasio(InputStream inputStream, OutputStream outputStream) throws FileNotFoundException {
14+
this.inputStream = inputStream;
15+
this.outputStream = outputStream;
1616
}
1717

1818
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++;
3023
}
24+
return byteData;
3125
}
3226

3327
public int read(byte[] b) throws IOException {
3428

35-
return -1;
29+
int totalBytesRead = inputStream.read(b);
30+
if (totalBytesRead != -1) {
31+
bytesRead += totalBytesRead;
32+
readOperationCount++;
33+
}
34+
return totalBytesRead;
35+
3636
}
3737

3838
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+
4049
}
4150

4251
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+
4461
}
4562

4663
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;
4872
}
4973

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+
}
5284

5385
}
5486

87+
public void write(byte[] b) throws IOException {
88+
try {
5589

56-
//Getter Setter
90+
this.outputStream.write(b);
91+
writeOperationCount++;
92+
bytesWritten += b.length;
5793

58-
public long getBytesRead() {
59-
return bytesRead;
60-
}
6194

62-
public void setBytesRead(long bytesRead) {
63-
this.bytesRead = bytesRead;
95+
} catch (IOException ioException) {
96+
ioException.printStackTrace();
97+
}
6498
}
6599

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+
}
68109
}
69110

70-
public void setBytesWritten(long bytesWritten) {
71-
this.bytesWritten = bytesWritten;
111+
public long getBytesRead() {
112+
return bytesRead;
72113
}
73114

74115
public long getReadOperationCount() {
75116
return readOperationCount;
76117
}
77118

78-
public void setReadOperationCount(long readOperationCount) {
79-
this.readOperationCount = readOperationCount;
119+
public long getBytesWritten() {
120+
return bytesWritten;
80121
}
81122

82123
public long getWriteOperationCount() {
83124
return writeOperationCount;
84125
}
85126

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+
}
92135
}
93136

94-
public void setMode(String mode) {
95-
this.mode = mode;
96-
}
97137
}
98-
99-

exercises/practice/paasio/src/main/java/Paasio.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import java.io.*;
2-
import java.net.Socket;
3-
import java.util.Objects;
42

53
public class Paasio implements Closeable {
64

@@ -113,12 +111,15 @@ public void write(byte[] b, int off, int len) throws IOException {
113111
public long getBytesRead() {
114112
return bytesRead;
115113
}
114+
116115
public long getReadOperationCount() {
117-
return readOperationCount;}
116+
return readOperationCount;
117+
}
118118

119119
public long getBytesWritten() {
120120
return bytesWritten;
121121
}
122+
122123
public long getWriteOperationCount() {
123124
return writeOperationCount;
124125
}

0 commit comments

Comments
 (0)