I wrote a piece of code for buffering a file - I plan on using it eventually
as part of a larger application.
However the exception handler seems to be getting called, but the getMessage
function just returns null. I'm confused. The file copy works fine - I
did a DOS file compare (binary) on a copied file and it seems to be working
just fine.
import java.io.*;
class XFerObject{ //read only object
long fileSize;
int blockSize; //raw bytes per packet
String fileName;
int numBlocks;
boolean isFinalBlockPartial;
long getFileSize(){
return fileSize;
}
int getBlockSize(){
return blockSize;
}
int getNumBlocks(){
return numBlocks;
}
String getFileName(){
return fileName;
}
boolean getFinalBlockPartial(){
return isFinalBlockPartial;
}
XFerObject(XFerObject C){ //copy
fileSize = C.fileSize;
blockSize = C.blockSize;
fileName = C.fileName;
numBlocks = C.numBlocks;
isFinalBlockPartial = C.isFinalBlockPartial;
}
XFerObject(long size, int block, String fn){
fileSize = size;
blockSize = block;
fileName = fn;
isFinalBlockPartial = (fileSize%blockSize == 0? false : true);
numBlocks = (int)(fileSize / blockSize);
if(isFinalBlockPartial)
numBlocks++;
}
}
interface packetWriter{
void AcceptPacket(int PacketNumber, byte[] data) throws IOException;
}
class GNPInput{ //opens a file locally and reads
packetWriter dest;
XFerObject fileInfo;
RandomAccessFile file;
int lastBlock;
GNPInput(packetWriter d, XFerObject f) throws IOException{
File fileObject = new File(f.getFileName());
file = new RandomAccessFile(fileObject, "r");
lastBlock = 0;
dest = d;
fileInfo = f;
}
static long getFileSize(String f){
File file = new File(f);
return file.length();
}
void doXFer() throws IOException{
byte[] packet = new byte[fileInfo.getBlockSize()];
while(lastBlock<fileInfo.getFileSize()){
if(lastBlock == fileInfo.getNumBlocks() -1 &&
fileInfo.getFinalBlockPartial())
packet = new byte[(int)(fileInfo.getFileSize() %
fileInfo.getBlockSize())];
file.seek((long)fileInfo.getBlockSize() * lastBlock);
file.readFully(packet);
dest.AcceptPacket(lastBlock++, packet);
}
}
}
class GNPOutput implements packetWriter{ //creates file and writes to disk
XFerObject fileInfo;
RandomAccessFile file;
GNPOutput(XFerObject f) throws IOException{
File fileObject = new File(f.getFileName());
file = new RandomAccessFile(fileObject, "rw");
file.setLength(f.getFileSize());
fileInfo = f;
}
public void AcceptPacket(int PacketNumber, byte[] data) throws IOException{
//System.out.println(new String(data));
file.seek(PacketNumber * (long) fileInfo.getBlockSize());
file.write(data);
}
}
public class gnp{
public static void main(String[] args){
String theFile = "F:\\Documents and Settings\\Luc\\Desktop\\test";
XFerObject myObj = new XFerObject(GNPInput.getFileSize(theFile), 3000,
theFile);
XFerObject myObj2 = new XFerObject(GNPInput.getFileSize(theFile), 3000,
"F:\\Documents and Settings\\Luc\\Desktop\\test2out.zip");
try{
GNPOutput out = new GNPOutput(myObj2);
GNPInput in = new GNPInput(out, myObj);
in.doXFer();
}
catch(IOException e){
System.out.println("oh noes!" + e.getMessage());
}
}
}
Everything behaves as expected, the file copies - and then the output reads
oh noes!null
I cannot figure it out!
Help would be appreciated. Is an exception actually being thrown?

Signature
LTP
:)
Chris Smith - 23 Mar 2006 10:02 GMT
> try{
> GNPOutput out = new GNPOutput(myObj2);
[quoted text clipped - 13 lines]
> I cannot figure it out!
> Help would be appreciated. Is an exception actually being thrown?
Yes, an exception is being thrown, and it has a null message. Change
that catch block to e.printStackTrace() for more information. Or just
omit the try/catch and add a throws clause to main.

Signature
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.
Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
Luc The Perverse - 24 Mar 2006 01:04 GMT
>> try{
>> GNPOutput out = new GNPOutput(myObj2);
[quoted text clipped - 18 lines]
> that catch block to e.printStackTrace() for more information. Or just
> omit the try/catch and add a throws clause to main.
Thanks (to both of you)
The line
while(lastBlock<fileInfo.getFileSize()){
was supposed to be
while(lastBlock<fileInfo.getNumBlocks()){
That sucker would have looped for awhile
--
LTP
:)
Ian Shef - 23 Mar 2006 20:36 GMT
> I wrote a piece of code for buffering a file - I plan on using it
> eventually as part of a larger application.
<snip>
> }
> catch(IOException e){
> System.out.println("oh noes!" + e.getMessage());
> }
<snip>
> Help would be appreciated. Is an exception actually being thrown?
e.toString() would be helpful here. Probably an EOFException.

Signature
Ian Shef 805/F6 * These are my personal opinions
Raytheon Company * and not those of my employer.
PO Box 11337 *
Tucson, AZ 85734-1337 *