{
String msg = localStrings.getString( "admin.mbeans.ssmb.call_preparedownload_first" );
throw new MBeanException( new java.lang.IllegalStateException( msg ) );
}
// if it is a file of 0, return null bytes
ByteChunk byteChunk = null;
if (downloadInfo.downloadFile.length() == 0 ) {
byte[] bytes = new byte[ByteChunk.kChunkMinSize];
byteChunk = new ByteChunk(bytes,
downloadInfo.downloadFile.getAbsolutePath(),
true, true);
unlockAndReset();
return byteChunk;
}
if ((chunkIndex >= downloadInfo.numChunks) || (chunkIndex < 0))
{
String msg = localStrings.getString( "admin.mbeans.ssmb.invalid_chunk_index" );
unlockAndReset();
throw new MBeanException( new java.lang.IllegalStateException( msg ) );
}
RandomAccessFile raf = null;
try
{
raf = new RandomAccessFile(downloadInfo.downloadFile, "r");
byte[] bytes = new byte[ByteChunk.kChunkMaxSize];
raf.seek(downloadInfo.numBytesRead);
int actualBytesRead = raf.read(bytes, 0, ByteChunk.kChunkMaxSize);
/*
Debug.println("Read " + actualBytesRead + " from " +
downloadInfo.numBytesRead);
*/
if (actualBytesRead < bytes.length)
{
byte[] newBytes = new byte[actualBytesRead];
System.arraycopy(bytes, 0, newBytes, 0, newBytes.length);
bytes = newBytes;
}
downloadInfo.numBytesRead += actualBytesRead;
boolean isFirstChunk = (chunkIndex == 0);
boolean isLastChunk =
(chunkIndex == (downloadInfo.numChunks - 1));
sLogger.log(Level.FINEST, "chunkIndex = " + chunkIndex +
" isFirstChunk = " + isFirstChunk +
" isLastChunk = " + isLastChunk);
byteChunk = new ByteChunk(bytes,
downloadInfo.downloadFile.getAbsolutePath(),
isFirstChunk, isLastChunk);
}
catch (IOException ioe)
{
sLogger.log(Level.FINE, "mbean.download_failed", ioe);
unlockAndReset();
throw new MBeanException(ioe);
}
finally
{
if (raf != null)
{
try { raf.close(); }
catch (IOException ioe) {}
}
if ((byteChunk != null) && (byteChunk.isLast()))
{
unlockAndReset();
}
}
return byteChunk;