-
-
Notifications
You must be signed in to change notification settings - Fork 716
Passio Practice Exercise implemented #2970
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 19 commits
a912d0c
d81eb05
bc2a2cd
c83978c
52e1794
3507f1c
4a94e3c
d1d9917
4c67d9b
9706a9f
8c3aaf5
0aeb078
71e0867
b934489
b8ba510
e07be74
03ee525
47ef869
fbf5c8d
3e71a5b
251ef7e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1869,6 +1869,14 @@ | |
"lists" | ||
], | ||
"difficulty": 10 | ||
}, | ||
{ | ||
"slug": "paasio", | ||
"name": "PaaS I/O", | ||
"uuid": "81177978-2ed3-47c2-a6a0-fef316d94c0b", | ||
"practices": [], | ||
"prerequisites": [], | ||
"difficulty": 1 | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This really shouldn't be a one (it's not that easy). Perhaps we could use the Go track as a guide - they have used 4 as the difficulty. |
||
} | ||
], | ||
"foregone": [ | ||
|
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 |
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" | ||
} |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,140 @@ | ||||||||||
import java.io.*; | ||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please don't use
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same goes for this! |
||||||||||
|
||||||||||
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 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(); | ||||||||||
} | ||||||||||
} | ||||||||||
|
||||||||||
} | ||||||||||
|
||||||||||
|
||||||||||
|
||||||||||
|
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"] | ||
} | ||
} |
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please move this exercise up with the other ones that have a difficulty of 4, and make sure it stays in alphabetical order within that section?