Skip to content

Commit

Permalink
Remove FilterStreams, do clean delegation instead.
Browse files Browse the repository at this point in the history
  • Loading branch information
vgv authored and turchenkoalex committed Dec 7, 2023
1 parent 9df638c commit db845e5
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 27 deletions.
80 changes: 61 additions & 19 deletions pgjdbc/src/main/java/org/postgresql/core/CountingInputStream.java
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 pgjdbc/src/main/java/org/postgresql/core/CountingOutputStream.java
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();
}
}

0 comments on commit db845e5

Please sign in to comment.