Code with Misuse: |
class QValueFactoryImpl.BinaryQValue {
/**
* Creates a new <code>BinaryQValue</code> instance from an
* <code>InputStream</code>. The contents of the stream is spooled
* to a temporary file or to a byte buffer if its size is smaller than
* {@link #MAX_BUFFER_SIZE}.
* <p/>
* The <code>temp</code> parameter governs whether dynamically allocated
* resources will be freed explicitly on {@link #discard()}. Note that any
* dynamically allocated resources (temp file/buffer) will be freed
* implicitly once this instance has been gc'ed.
*
* @param in stream to be represented as a <code>BinaryQValue</code> instance
* @param temp flag indicating whether this instance represents a
* <i>temporary</i> value whose resources can be explicitly freed
* on {@link #discard()}.
* @throws IOException if an error occurs while reading from the stream or
* writing to the temporary file
*/
private BinaryQValue(InputStream in, boolean temp) throws IOException {
byte[] spoolBuffer = new byte[0x2000];
int read;
int len = 0;
OutputStream out = null;
File spoolFile = null;
try {
while ((read = in.read(spoolBuffer)) > 0) {
if (out != null) {
// spool to temp file
out.write(spoolBuffer, 0, read);
len += read;
} else if (len + read > BinaryQValue.MAX_BUFFER_SIZE) {
// threshold for keeping data in memory exceeded;
// create temp file and spool buffer contents
TransientFileFactory fileFactory = TransientFileFactory.getInstance();
spoolFile = fileFactory.createTransientFile("bin", null, null);
out = new FileOutputStream(spoolFile);
out.write(buffer, 0, len);
out.write(spoolBuffer, 0, read);
buffer = null;
len += read;
} else {
// reallocate new buffer and spool old buffer contents
byte[] newBuffer = new byte[len + read];
System.arraycopy(buffer, 0, newBuffer, 0, len);
System.arraycopy(spoolBuffer, 0, newBuffer, len, read);
buffer = newBuffer;
len += read;
}
}
} finally {
if (out != null) {
out.close();
}
}
// init vars
file = spoolFile;
this.temp = temp;
// buffer is EMPTY_BYTE_ARRAY (default value)
}
}
|