Re: Zipping multiple BLOBs
On Feb 19, 12:22 pm, Lew <no...@lewscanon.com> wrote:
cdef wrote:
and correct me if I'm wrong, is that each time the Java procedure is
called I should be able to determine the array count of the
destination BLOB and pick up at the end and append more zip entries. I
have tried to do exactly this with something like "int z_length =
dstBlob.length();" or "int z_length = getLength(dstBlob);". My proble=
m
is that I don't know 1) which one returns the array count vs the BLOB
size 2) how to use the z_length value once I've obtained it. If I try
"dstBlob[z_length].setBinaryStream(0);" I get a
java.lang.ArrayIndexOutOfBoundsException exception.
Before getting to your question, let me suggest strongly that you please =
do
not use TABs to indent Usenet listings. Using a maximum of four space
characters per indent level to keep your listings readable.
Now to your question. First, why do you use 'oracle.sql.BLOB' and not =
a
standard type?
public static void zipBlob(
oracle.sql.BLOB srcBlob, oracle.sql.BLOB dstBlob[], String name )
Second, don't give code examples as "something like ...". Give copied =
and
pasted *exact* code that you tried, as an SSCCE.
<http://sscce.com/>
Third, the "something like ..." code you describe won't compile. Array=
s in
Java do not have a 'length()' method, and you don't show the 'getLength( =
BLOB
)' method to which you refer, so I can't speak to that. Arrays in Java=
have a
'length' attribute, thus 'someArray.length'. This is very basic Java.
Fourth, the "something like ..." code you describe will throw an
'ArrayIndexOutOfBoundsException' once you fix the compilation problems, a=
s
you've seen. Assuming you set 'z_length' (a name that violates the Jav=
a
coding conventions, which call for camel case and no underscores in
non-constant variable names) from the array 'length' attribute, it cannot=
be
used as an array index for the same array. Arrays in Java do not chang=
e size,
and the maximum index of an array is one less than its length. Trying =
to
reference array element 'zLength' (to correct your name) uses an index th=
at is
out of bounds.
Fifth, if you want to add to an array, you need to create a longer array =
and
copy the 'zLength' elements of the old array into the first 'zLength' ele=
ments
of the new array. Make sure the new array is long enough to handle all=
the
new elements you want to add (maximum index "length minus one").
For a self-growing array-like structure use a 'java.util.List' instead of=
an
array. The 'java.util.ArrayList' is closest to a regular array and sim=
plest
of the 'List' implementations.
Have you read the Java tutorials on java.sun.com? If not, you must. =
They
contain answers to some of the questions you asked.
--
Lew
Sorry, I just copied the code from my IDE and didn't think to replace
the tabs. The code that I pasted is the exact code I have compiled. I
apologize for using unclear terms such as "something like". I also
wasn't aware of the camel case convention. Here is a cleaner version
of the code:
import java.io.*;
import java.util.zip.*;
import java.sql.*;
import oracle.sql.*;
import oracle.jdbc.*;
public class ZIPImpl
{
public static void zipBlob(oracle.sql.BLOB srcBlob,
oracle.sql.BLOB dstBlob[],java.lang.String name)
{
try
{
int zLngth = dstBlob.length;
OutputStream outBuffer =
dstBlob[0].setBinaryStream(0);
InputStream inBuffer = srcBlob.getBinaryStream();
ZipOutputStream zip = new ZipOutputStream(outBuffer);
zip.setMethod(ZipOutputStream.DEFLATED);
byte[] tmpBuffer = new byte[1024];
ZipEntry entry = new ZipEntry(name);
zip.putNextEntry(entry);
int n;
while ((n = inBuffer.read(tmpBuffer)) >= 0)
{
zip.write(tmpBuffer, 0, n);
}
zip.close();
}
catch (SQLException e)
{
System.err.println(e);
}
catch (IOException e)
{
System.err.println(e);
}
}
}
For the oracle.sql.BLOB type, which alternate type would you
recommend? I realize that a BLOB is just a simple byte array. Would it
be better to go with java.sql.blob or would an ArrayList work? I just
went with the oracle version because I figured it would play nicer
with the database. Another issue I have run into is that zLngth is
null even when I am passing in the very same BLOB that the Java
procedure returned on the first iteration. I have read through every
single Java tutorial regarding BLOBs, zipping files, and Oracle. I
can't believe I forgot that arrays cannot be extended in Java. I will
try building a new ArrayList and copying over the old zipped BLOB
contents to it plus the new contents.
Thanks for your help.