forked from pgjdbc/pgjdbc
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove FilterStreams, do clean delegation instead.
- Loading branch information
1 parent
9df638c
commit db845e5
Showing
2 changed files
with
91 additions
and
27 deletions.
There are no files selected for viewing
80 changes: 61 additions & 19 deletions
80
pgjdbc/src/main/java/org/postgresql/core/CountingInputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,83 @@ | ||
package org.postgresql.core; | ||
|
||
import java.io.FilterInputStream; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
/** | ||
* @author Vasily Vasilkov ([email protected]) | ||
*/ | ||
final class CountingInputStream extends FilterInputStream { | ||
final class CountingInputStream extends InputStream { | ||
|
||
private final InputStream peer; | ||
private final AtomicLong counter; | ||
|
||
public CountingInputStream(InputStream in, AtomicLong counter) { | ||
super(in); | ||
this.peer = in; | ||
this.counter = counter; | ||
} | ||
|
||
@Override | ||
public int read() throws IOException { | ||
int readBytes = super.read(); | ||
@Override | ||
public int read() throws IOException { | ||
int readByte = peer.read(); | ||
|
||
if (readBytes != -1) { | ||
counter.addAndGet(readBytes); | ||
} | ||
if (readByte != -1) { | ||
counter.incrementAndGet(); | ||
} | ||
|
||
return readBytes; | ||
} | ||
return readByte; | ||
} | ||
|
||
@Override | ||
public int read(byte[] b, int off, int len) throws IOException { | ||
int readBytes = super.read(b, off, len); | ||
@Override | ||
public int read(byte[] b) throws IOException { | ||
int readBytes = peer.read(b); | ||
|
||
if (readBytes != -1) { | ||
counter.addAndGet(readBytes); | ||
} | ||
if (readBytes != -1) { | ||
counter.addAndGet(readBytes); | ||
} | ||
|
||
return readBytes; | ||
} | ||
|
||
@Override | ||
public int read(byte[] b, int off, int len) throws IOException { | ||
int readBytes = peer.read(b, off, len); | ||
|
||
if (readBytes != -1) { | ||
counter.addAndGet(readBytes); | ||
} | ||
|
||
return readBytes; | ||
} | ||
|
||
@Override | ||
public long skip(long n) throws IOException { | ||
return peer.skip(n); | ||
} | ||
|
||
@Override | ||
public int available() throws IOException { | ||
return peer.available(); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
peer.close(); | ||
} | ||
|
||
@Override | ||
public void mark(int readlimit) { | ||
peer.mark(readlimit); | ||
} | ||
|
||
@Override | ||
public void reset() throws IOException { | ||
peer.reset(); | ||
} | ||
|
||
@Override | ||
public boolean markSupported() { | ||
return peer.markSupported(); | ||
} | ||
|
||
return readBytes; | ||
} | ||
} |
38 changes: 30 additions & 8 deletions
38
pgjdbc/src/main/java/org/postgresql/core/CountingOutputStream.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,47 @@ | ||
package org.postgresql.core; | ||
|
||
import java.io.FilterOutputStream; | ||
import java.io.IOException; | ||
import java.io.OutputStream; | ||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
/** | ||
* @author Vasily Vasilkov ([email protected]) | ||
*/ | ||
final class CountingOutputStream extends FilterOutputStream { | ||
final class CountingOutputStream extends OutputStream { | ||
|
||
private final OutputStream peer; | ||
private final AtomicLong counter; | ||
|
||
public CountingOutputStream(OutputStream out, AtomicLong counter) { | ||
super(out); | ||
this.peer = out; | ||
this.counter = counter; | ||
} | ||
|
||
@Override | ||
public void write(int b) throws IOException { | ||
super.write(b); | ||
counter.addAndGet(1); | ||
} | ||
@Override | ||
public void write(int b) throws IOException { | ||
peer.write(b); | ||
counter.incrementAndGet(); | ||
} | ||
|
||
@Override | ||
public void write(byte[] b) throws IOException { | ||
peer.write(b); | ||
counter.addAndGet(b.length); | ||
} | ||
|
||
@Override | ||
public void write(byte[] b, int off, int len) throws IOException { | ||
peer.write(b, off, len); | ||
counter.addAndGet(len); | ||
} | ||
|
||
@Override | ||
public void flush() throws IOException { | ||
peer.flush(); | ||
} | ||
|
||
@Override | ||
public void close() throws IOException { | ||
peer.close(); | ||
} | ||
} |