Skip to content

Commit

Permalink
Revert "perf: add read(b,o,l) to BlobInputStream (pgjdbc#2376)" (pgjd…
Browse files Browse the repository at this point in the history
…bc#2422)

This reverts commit 8825a38.
  • Loading branch information
davecramer authored Jan 26, 2022
1 parent 90e53cb commit 3af3b32
Show file tree
Hide file tree
Showing 4 changed files with 318 additions and 500 deletions.
101 changes: 23 additions & 78 deletions pgjdbc/src/main/java/org/postgresql/largeobject/BlobInputStream.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class BlobInputStream extends InputStream {
/**
* The absolute position.
*/
private long absolutePosition;
private long apos;

/**
* Buffer used to improve performance.
Expand All @@ -33,17 +33,17 @@ public class BlobInputStream extends InputStream {
/**
* Position within buffer.
*/
private int bufferPosition;
private int bpos;

/**
* The buffer size.
*/
private final int bufferSize;
private int bsize;

/**
* The mark position.
*/
private long markPosition = 0;
private long mpos = 0;

/**
* The limit.
Expand Down Expand Up @@ -74,9 +74,9 @@ public BlobInputStream(LargeObject lo, int bsize) {
public BlobInputStream(LargeObject lo, int bsize, long limit) {
this.lo = lo;
buffer = null;
bufferPosition = 0;
absolutePosition = 0;
this.bufferSize = bsize;
bpos = 0;
apos = 0;
this.bsize = bsize;
this.limit = limit;
}

Expand All @@ -86,88 +86,33 @@ public BlobInputStream(LargeObject lo, int bsize, long limit) {
public int read() throws java.io.IOException {
LargeObject lo = getLo();
try {
if (limit > 0 && absolutePosition >= limit) {
if (limit > 0 && apos >= limit) {
return -1;
}
// read more in if necessary
if (buffer == null || bufferPosition >= buffer.length) {
buffer = lo.read(bufferSize);
bufferPosition = 0;
if (buffer == null || bpos >= buffer.length) {
buffer = lo.read(bsize);
bpos = 0;
}

// Handle EOF
if ( buffer == null || bufferPosition >= buffer.length) {
if (buffer == null || bpos >= buffer.length) {
return -1;
}

int ret = (buffer[bufferPosition] & 0xFF);
int ret = (buffer[bpos] & 0x7F);
if ((buffer[bpos] & 0x80) == 0x80) {
ret |= 0x80;
}

bufferPosition++;
absolutePosition++;
bpos++;
apos++;

return ret;
} catch (SQLException se) {
throw new IOException(se.toString());
}
}

@Override
public int read(byte[] b, int off, int len) throws IOException {
int bytesCopied = 0;
LargeObject lo = getLo();

/* check to make sure we aren't at the limit
* funny to test for 0, but I guess someone could create a blob
* with a limit of zero
*/
if ( limit >= 0 && absolutePosition >= limit ) {
return -1;
}

/* check to make sure we are not going to read past the limit */
if ( limit >= 0 && len > limit - absolutePosition ) {
len = (int)(limit - absolutePosition);
}

try {
// have we read anything into the buffer
if ( buffer != null ) {
// now figure out how much data is in the buffer
int bytesInBuffer = buffer.length - bufferPosition;
// figure out how many bytes the user wants
int bytesToCopy = len > bytesInBuffer ? bytesInBuffer : len;
// copy them in
System.arraycopy(buffer, bufferPosition, b, off, bytesToCopy);
// move the buffer position
bufferPosition += bytesToCopy;
// position in the blob
absolutePosition += bytesToCopy;
// increment offset
off += bytesToCopy;
// decrement the length
len -= bytesToCopy;
bytesCopied = bytesToCopy;
}

if (len > 0 ) {
bytesCopied += lo.read(b, off, len);
buffer = null;
bufferPosition = 0;
absolutePosition += bytesCopied;
/*
if there is a limit on the size of the blob then we could have read to the limit
so bytesCopied will be non-zero but we will have read nothing
*/
if ( bytesCopied == 0 && (buffer == null) ) {
return -1;
}
}
} catch (SQLException ex ) {
throw new IOException(ex.getCause());
}
return bytesCopied;
}

/**
* <p>Closes this input stream and releases any system resources associated with the stream.</p>
*
Expand Down Expand Up @@ -208,7 +153,7 @@ public void close() throws IOException {
* @see java.io.InputStream#reset()
*/
public synchronized void mark(int readlimit) {
markPosition = absolutePosition;
mpos = apos;
}

/**
Expand All @@ -221,13 +166,13 @@ public synchronized void mark(int readlimit) {
public synchronized void reset() throws IOException {
LargeObject lo = getLo();
try {
if (markPosition <= Integer.MAX_VALUE) {
lo.seek((int)markPosition);
if (mpos <= Integer.MAX_VALUE) {
lo.seek((int)mpos);
} else {
lo.seek64(markPosition, LargeObject.SEEK_SET);
lo.seek64(mpos, LargeObject.SEEK_SET);
}
buffer = null;
absolutePosition = markPosition;
apos = mpos;
} catch (SQLException se) {
throw new IOException(se.toString());
}
Expand Down
14 changes: 0 additions & 14 deletions pgjdbc/src/main/java/org/postgresql/largeobject/LargeObject.java
Original file line number Diff line number Diff line change
Expand Up @@ -398,20 +398,6 @@ public InputStream getInputStream(long limit) throws SQLException {
return new BlobInputStream(this, 4096, limit);
}

/**
* Returns an {@link InputStream} from this object, that will limit the amount of data that is
* visible.
* Added mostly for testing
*
* @param maxSize internal buffer size
* @param limit maximum number of bytes the resulting stream will serve
* @return {@link InputStream} from this object
* @throws SQLException if a database-access error occurs.
*/
public InputStream getInputStream(int maxSize, long limit) throws SQLException {
return new BlobInputStream(this, maxSize, limit);
}

/**
* <p>Returns an {@link OutputStream} to this object.</p>
*
Expand Down
Loading

0 comments on commit 3af3b32

Please sign in to comment.