Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -2070,6 +2070,14 @@
"lists"
],
"difficulty": 10
},
{
"slug": "paasio",
"name": "PaaS I/O",
"uuid": "81177978-2ed3-47c2-a6a0-fef316d94c0b",
"practices": [],
"prerequisites": [],
"difficulty": 4
}
],
"foregone": [
Expand Down
13 changes: 13 additions & 0 deletions exercises/practice/paasio/.docs/instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Instructions

Report network IO statistics.

You are writing a [PaaS][paas], and you need a way to bill customers based on network and filesystem usage.

Create a wrapper for network connections and files that can report IO statistics.
The wrapper must report:

- The total number of bytes read/written.
- The total number of read/write operations.

[paas]: https://en.wikipedia.org/wiki/Platform_as_a_service
17 changes: 17 additions & 0 deletions exercises/practice/paasio/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": [],
"files": {
"solution": [
"src/main/java/Paasio.java"
],
"test": [
"src/test/java/PaasioTest.java"
],
"example": [
".meta/src/reference/java/Paasio.java"
]
},
"blurb": "Report network IO statistics.",
"source": "Brian Matsuo",
"source_url": "https://github.com/bmatsuo"
}
149 changes: 149 additions & 0 deletions exercises/practice/paasio/.meta/src/reference/java/Paasio.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import java.io.*;

public class Paasio implements Closeable {

private long bytesRead;
private long readOperationCount;
private long bytesWritten;
private long writeOperationCount;

private final InputStream inputStream;
private final OutputStream outputStream;


public InputStream getInputStream() {
return inputStream;
}

public OutputStream getOutputStream() {
return outputStream;
}

public Paasio(InputStream inputStream, OutputStream outputStream) {
this.inputStream = inputStream;
this.outputStream = outputStream;
}

public int read() throws IOException {
int byteData = inputStream.read();
if (byteData != -1) {
bytesRead += 1;
readOperationCount++;
}
return byteData;
}

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

int totalBytesRead = inputStream.read(b);
if (totalBytesRead != -1) {
bytesRead += totalBytesRead;
readOperationCount++;
}
return totalBytesRead;

}

public int read(byte[] b, int off, int len) throws IOException {

int bytesReadIntoBuffer = inputStream.read(b, off, len);

if (bytesReadIntoBuffer != -1) {
bytesRead += bytesReadIntoBuffer;
readOperationCount++;
}
return bytesReadIntoBuffer;


}

public byte[] readAllBytes() throws IOException {

byte[] allData = this.inputStream.readAllBytes();

if (allData.length > 0) {
readOperationCount++;
bytesRead += allData.length;
}
return allData;

}

public byte[] readNBytes(int len) throws IOException {

byte[] allData = this.inputStream.readNBytes(len);
if (allData.length > 0) {
readOperationCount++;
bytesRead += allData.length;
}
return allData;
}

public void write(int b) throws IOException {

try {
this.outputStream.write(b);
writeOperationCount++;
bytesWritten++;

} catch (IOException ioException) {
ioException.printStackTrace();
}

}

public void write(byte[] b) throws IOException {
try {

this.outputStream.write(b);
writeOperationCount++;
bytesWritten += b.length;


} catch (IOException ioException) {
ioException.printStackTrace();
}
}

public void write(byte[] b, int off, int len) throws IOException {
try {
this.outputStream.write(b, off, len);
writeOperationCount++;
bytesWritten += len;

} catch (IOException ioException) {
ioException.printStackTrace();
}
}

public long getBytesRead() {
return bytesRead;
}

public long getReadOperationCount() {
return readOperationCount;
}

public long getBytesWritten() {
return bytesWritten;
}

public long getWriteOperationCount() {
return writeOperationCount;
}

@Override
public void close() throws IOException {
if (this.inputStream != null) {
this.inputStream.close();
}
if (this.outputStream != null) {
this.outputStream.close();
}
}

}




25 changes: 25 additions & 0 deletions exercises/practice/paasio/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
plugins {
id "java"
}

repositories {
mavenCentral()
}

dependencies {
testImplementation platform("org.junit:junit-bom:5.10.0")
testImplementation "org.junit.jupiter:junit-jupiter"
testImplementation "org.assertj:assertj-core:3.25.1"

testRuntimeOnly "org.junit.platform:junit-platform-launcher"
}

test {
useJUnitPlatform()

testLogging {
exceptionFormat = "full"
showStandardStreams = true
events = ["passed", "failed", "skipped"]
}
}
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.7-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
Loading
Loading