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