Skip to content

Commit b8ba510

Browse files
committed
Merge branch 'paasio-exercise'
2 parents 2238efa + b934489 commit b8ba510

12 files changed

Lines changed: 1076 additions & 0 deletions

File tree

config.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1869,6 +1869,14 @@
18691869
"lists"
18701870
],
18711871
"difficulty": 10
1872+
},
1873+
{
1874+
"slug": "paasio",
1875+
"name": "PaaS I/O",
1876+
"uuid": "81177978-2ed3-47c2-a6a0-fef316d94c0b",
1877+
"practices": [],
1878+
"prerequisites": [],
1879+
"difficulty": 1
18721880
}
18731881
],
18741882
"foregone": [
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Instructions
2+
3+
Report network IO statistics.
4+
5+
You are writing a [PaaS][paas], and you need a way to bill customers based on network and filesystem usage.
6+
7+
Create a wrapper for network connections and files that can report IO statistics.
8+
The wrapper must report:
9+
10+
- The total number of bytes read/written.
11+
- The total number of read/write operations.
12+
13+
[paas]: https://en.wikipedia.org/wiki/Platform_as_a_service
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"authors": [],
3+
"files": {
4+
"solution": [
5+
"src/main/java/Paasio.java"
6+
],
7+
"test": [
8+
"src/test/java/PaasioTest.java"
9+
],
10+
"example": [
11+
".meta/src/reference/java/Paasio.java"
12+
]
13+
},
14+
"blurb": "Report network IO statistics.",
15+
"source": "Brian Matsuo",
16+
"source_url": "https://github.com/bmatsuo"
17+
}
Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
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+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
plugins {
2+
id "java"
3+
}
4+
5+
repositories {
6+
mavenCentral()
7+
}
8+
9+
dependencies {
10+
testImplementation platform("org.junit:junit-bom:5.10.0")
11+
testImplementation "org.junit.jupiter:junit-jupiter"
12+
testImplementation "org.assertj:assertj-core:3.25.1"
13+
14+
testRuntimeOnly "org.junit.platform:junit-platform-launcher"
15+
}
16+
17+
test {
18+
useJUnitPlatform()
19+
20+
testLogging {
21+
exceptionFormat = "full"
22+
showStandardStreams = true
23+
events = ["passed", "failed", "skipped"]
24+
}
25+
}
42.4 KB
Binary file not shown.
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
distributionBase=GRADLE_USER_HOME
2+
distributionPath=wrapper/dists
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
4+
networkTimeout=10000
5+
validateDistributionUrl=true
6+
zipStoreBase=GRADLE_USER_HOME
7+
zipStorePath=wrapper/dists

0 commit comments

Comments
 (0)