Exception handler being called

L

Luc The Perverse

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?
 
C

Chris Smith

Luc The Perverse said:
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?

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.

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
I

Ian Shef

I wrote a piece of code for buffering a file - I plan on using it
eventually as part of a larger application.
}
catch(IOException e){
System.out.println("oh noes!" + e.getMessage());
}
Help would be appreciated. Is an exception actually being thrown?

e.toString() would be helpful here. Probably an EOFException.
 
L

Luc The Perverse

Chris Smith said:
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.

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
 
R

Roedy Green

catch(IOException e){
System.out.println("oh noes!" + e.getMessage());

Perhaps would be useful to dump the class of the IOException. Maybe it
is something ordinary like an EOFException.
--
Roedy Green Canadian Mind Products
http://mindprod.com

"I mean, source code in files; how quaint, how seventies!"
~ Kent Beck (born: 1961 age: 48), evangelist for extreme programming.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top