Length for a byte array

I am trying to download a file from a MySQL database using JDBC, using the example below.

(The was posted to this forum by Koray Yersel on May 8, 2006)

I could use some help figuring out this line of the code: "byte[] buf = new byte[bBufLen];"

What should be the length of the byte Array, the variable "bBufLen" ?

Many thanks in advance

-- The original post:

here is my solution for writing and loading any kind of files in mysql db.

(tested with doc, jpeg, txt and pdf)

writing:

...PreparedStatement ps = conn.prepareStatement("insert into anlagen values('"+sendername.toString()+"','"+filename.toString() +"',?)");

File fileIn =new File("C:\\diso_temp\\" + filename);

int fileLength = (int) fileIn.length();

InputStream streamedFile =new FileInputStream(fileIn);

ps.setBinaryStream(1, streamedFile, fileLength);

ps.executeUpdate();

ps.close();

streamedFile.close();

...

reading / loading:

...

ResultSet rs = conn.createStatement().executeQuery("SELECT file,filename FROM anlagen");

while (rs.next()){

Blob blob = rs.getBlob("file");

String filename = rs.getString("filename");

OutputStream fwriter =new FileOutputStream("C:\\"+filename);

readFromBlob(blob, fwriter);

fwriter.close();

}

...

publicstaticlong readFromBlob(Blob blob, OutputStream out)

throws SQLException, IOException{

InputStream in = blob.getBinaryStream();

int length = -1;

long read = 0;

byte[] buf =newbyte[bBufLen];

while ((length = in.read(buf)) != -1){

out.write(buf, 0, length);

read += length;

}

in.close();

return read;

}

[2692 byte] By [java_Huha] at [2007-10-3 3:12:38]
# 1
Usually folks use some multiple of 1024, like 8 * 1024, but as long as its not too small and not too large, it really shouldn't matter. If performance is found to be lacking, you could tune this.
IanSchneidera at 2007-7-14 21:03:34 > top of Java-index,Java Essentials,Java Programming...
# 2

1) given how the array is used as an arg to an InputStream, it doesn't necessarily have to be anything in particular. The array is just a buffer to be filled up. You could choose any convenient value, say 256.There's probably an optimal value but don't worry about that now.

2) Not that I've ever used this before, but looking at the API docs (you should try it sometime), I see there's a Blob.getBytes method, which seems to do the same thing that this buffer is used for except it doesn't require that you create an InputStream.

paulcwa at 2007-7-14 21:03:34 > top of Java-index,Java Essentials,Java Programming...