Author: kahatlen
Date: Sun Nov 2 01:19:37 2008
New Revision: 709856
URL: http://svn.apache.org/viewvc?rev=709856&view=rev
Log:
DERBY-3770: Create a utility class for skipping data in an InputStream
Replaced skip loops with calls to the new utility methods in
- EmbedBlob.length()
- PositionedStoreStream.reposition()
- UpdatableBlobStream.updateIfRequired()
- SQLBinary.getLength()
Modified:
db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLBinary.java
db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedBlob.java
db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/PositionedStoreStream.java
db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/UpdatableBlobStream.java
Modified: db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLBinary.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLBinary.java?rev=709856&r1=709855&r2=709856&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLBinary.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/iapi/types/SQLBinary.java Sun Nov 2
01:19:37 2008
@@ -25,32 +25,22 @@
import org.apache.derby.iapi.reference.MessageId;
import org.apache.derby.iapi.services.io.ArrayInputStream;
-import org.apache.derby.iapi.services.io.FormatableBitSet;
-import org.apache.derby.iapi.services.io.NewByteArrayInputStream;
-import org.apache.derby.iapi.types.DataTypeDescriptor;
-import org.apache.derby.iapi.types.DataValueDescriptor;
-import org.apache.derby.iapi.types.TypeId;
import org.apache.derby.iapi.types.BitDataValue;
import org.apache.derby.iapi.types.DataValueDescriptor;
import org.apache.derby.iapi.types.ConcatableDataValue;
-import org.apache.derby.iapi.types.VariableSizeDataValue;
import org.apache.derby.iapi.error.StandardException;
-import org.apache.derby.iapi.services.io.FormatIdUtil;
import org.apache.derby.iapi.services.io.StoredFormatIds;
-import org.apache.derby.iapi.services.io.StreamStorable;
import org.apache.derby.iapi.services.io.FormatIdInputStream;
import org.apache.derby.iapi.services.sanity.SanityManager;
import org.apache.derby.iapi.services.i18n.MessageService;
import org.apache.derby.iapi.types.BooleanDataValue;
-import org.apache.derby.iapi.types.StringDataValue;
import org.apache.derby.iapi.types.NumberDataValue;
import org.apache.derby.iapi.services.cache.ClassSize;
-import org.apache.derby.iapi.util.StringUtil;
import org.apache.derby.iapi.types.SQLInteger;
@@ -59,9 +49,9 @@
import java.io.IOException;
import java.io.InputStream;
-import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
+import org.apache.derby.iapi.services.io.InputStreamUtil;
/**
* SQLBinary is the abstract class for the binary datatypes.
@@ -251,17 +241,11 @@
// If we have the stream length encoded.
// just read that.
streamValueLength = readBinaryLength((ObjectInput) stream);
- if (streamValueLength != 0)
- return streamValueLength;
- // Otherwise we will have to read the whole stream.
- for (;;) {
- long skipsize = stream.skip(Integer.MAX_VALUE);
- streamValueLength += skipsize;
- if (stream.read() == -1)
- break;
- else
- streamValueLength++;
- }
+ if (streamValueLength == 0) {
+ // Otherwise we will have to read the whole stream.
+ streamValueLength =
+ (int) InputStreamUtil.skipUntilEOF(stream);
+ }
return streamValueLength;
}
catch (IOException ioe) {
Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedBlob.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedBlob.java?rev=709856&r1=709855&r2=709856&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedBlob.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/EmbedBlob.java Sun Nov 2 01:19:37
2008
@@ -25,7 +25,6 @@
import org.apache.derby.iapi.reference.SQLState;
import org.apache.derby.iapi.error.StandardException;
import org.apache.derby.iapi.jdbc.EngineLOB;
-import org.apache.derby.iapi.reference.Limits;
import org.apache.derby.iapi.services.sanity.SanityManager;
import org.apache.derby.iapi.types.DataValueDescriptor;
import org.apache.derby.iapi.types.Resetable;
@@ -339,27 +338,12 @@
myStream.resetStream();
BinaryToRawStream tmpStream =
new BinaryToRawStream(myStream, this);
- streamLength = 0;
if (SanityManager.DEBUG) {
SanityManager.ASSERT(tmpStream.getLength() == -1);
}
- for (;;)
- {
- long skipped = tmpStream.skip(Limits.DB2_LOB_MAXWIDTH);
- if (SanityManager.DEBUG) {
- SanityManager.ASSERT(skipped >= 0);
- }
- streamLength += skipped;
- // If skip reports zero bytes skipped, verify EOF.
- if (skipped == 0) {
- if (tmpStream.read() == -1) {
- break; // Exit the loop, no more data.
- } else {
- streamLength++;
- }
- }
- }
+ streamLength = InputStreamUtil.skipUntilEOF(tmpStream);
+
tmpStream.close();
// Save for future uses.
return streamLength;
Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/PositionedStoreStream.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/PositionedStoreStream.java?rev=709856&r1=709855&r2=709856&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/PositionedStoreStream.java
(original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/PositionedStoreStream.java
Sun Nov 2 01:19:37 2008
@@ -27,6 +27,7 @@
import java.io.InputStream;
import org.apache.derby.iapi.error.StandardException;
+import org.apache.derby.iapi.services.io.InputStreamUtil;
import org.apache.derby.iapi.types.Resetable;
/**
@@ -199,7 +200,7 @@
}
if (this.pos < requestedPos) {
try {
- skipFully(requestedPos - this.pos);
+ InputStreamUtil.skipFully(stream, requestedPos - pos);
} catch (EOFException eofe) {
// A position after the end of the stream was requested.
// To recover, and for consistency, reset to position zero.
@@ -220,26 +221,4 @@
return this.pos;
}
- /**
- * Skip exactly the requested number of bytes.
- *
- * @throws EOFException if EOF is reached before all bytes are skipped
- * @throws IOException if reading from the stream fails
- */
- private void skipFully(long toSkip)
- throws IOException {
- long remaining = toSkip;
- while (remaining > 0) {
- long skippedNow = this.stream.skip(remaining);
- if (skippedNow == 0) {
- if (this.stream.read() == -1) {
- throw new EOFException("Reached end-of-stream prematurely" +
- ", with " + remaining + " byte(s) to go");
- } else {
- skippedNow = 1;
- }
- }
- remaining -= skippedNow;
- }
- }
} // End class PositionedStoreStream
Modified: db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/UpdatableBlobStream.java
URL: http://svn.apache.org/viewvc/db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/UpdatableBlobStream.java?rev=709856&r1=709855&r2=709856&view=diff
==============================================================================
--- db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/UpdatableBlobStream.java (original)
+++ db/derby/code/trunk/java/engine/org/apache/derby/impl/jdbc/UpdatableBlobStream.java Sun
Nov 2 01:19:37 2008
@@ -26,8 +26,7 @@
import java.io.IOException;
import java.io.InputStream;
import java.sql.SQLException;
-import org.apache.derby.iapi.reference.SQLState;
-import org.apache.derby.iapi.services.i18n.MessageService;
+import org.apache.derby.iapi.services.io.InputStreamUtil;
/**
* Updatable blob stream is a wrapper stream over dvd stream
@@ -112,22 +111,7 @@
} catch (SQLException ex) {
throw Util.newIOException(ex);
}
- long leftToSkip = pos;
- while (leftToSkip > 0) {
- long skipped = stream.skip (leftToSkip);
- if (skipped == 0) {
- //skipping zero byte check stream has reached eof
- if (stream.read() < 0) {
- throw new IOException (
- MessageService.getCompleteMessage (
- SQLState.STREAM_EOF, new Object [0]));
- }
- else {
- skipped = 1;
- }
- }
- leftToSkip -= skipped;
- }
+ InputStreamUtil.skipFully(stream, pos);
}
}
|