Reputation: 9
I am trying to create segments of data from a text file to transfer over a network to another device however my reader is not working.
It will read the chunk, send it and update the counter value remainigBytes
then read the next segment from where the next segment should start. For example HelloWorld would be 2 5 Byte segments of "Hello" and "World" but when i read the file it only reads the first 5 letters("Hello") 2 times and does not read "World" at all.
How do I get the fileReader to read from a specific start point rather than always from the beginning. I am not limited by using FileReader if there is a more appropriate reader I could implement and please let me know.
public int readData() throws IOException {
//creates the file reader for the input file.
FileReader myReader = null;
try {
myReader = new FileReader(inputFileName);
} catch (IOException e){
System.out.println("File reader could not be initialized");
System.out.println(e.getMessage());
System.exit(0);
}
System.out.println("Creating segment...");
System.out.println("----------------------------------------------------");
//reads the input file and changes the segments values
if (this.remainingBytes < this.maxPayload) {
//if the payload size is greater than the number of characters left
char[] segCharsBuf = new char[(int) this.remainingBytes];
myReader.read(segCharsBuf,0,(int)this.remainingBytes);
this.dataSeg.setSize((int)remainingBytes);
this.dataSeg.setPayLoad(Arrays.toString(segCharsBuf));
this.dataSeg.setSq(((int)this.fileSize - (int)this.remainingBytes) / this.maxPayload);
System.out.println("Segment number " + this.dataSeg.getSq() +" created (" + this.dataSeg.getSize() + " Bytes)");
System.out.println("----------------------------------------------------");
return -1;
} else {
//if the file is longer or just as ling as the payload size
char[] segCharsBuf = new char[this.maxPayload];
myReader.read(segCharsBuf,0, this.maxPayload);
this.dataSeg.setSize(this.maxPayload);
this.dataSeg.setPayLoad(Arrays.toString(segCharsBuf));
this.dataSeg.setSq(((int)this.fileSize - (int)this.remainingBytes) / this.maxPayload);
System.out.println("Segment number " + this.dataSeg.getSq() +" created (" + this.dataSeg.getSize() + " Bytes)");
System.out.println("----------------------------------------------------");
return 0;
}
}
Upvotes: -2
Views: 57
Reputation: 109613
FileReader
is an old utility class to read text in the current platforms encoding.
Taking the encoding of your current PC is a bad idea. And with a multibyte encoding like UTF-8 (important for Unicode) you have the problem that you cannot simply split a byte sequence of more than one byte in half, like what sometimes would happen with fixed chunk sizes.
So this means: it is much easier to use binary byte level reading. It is also more efficient als reading and writing text means that binary data, bytes are converted to java String / char with some encoding / Charset, and one the other side converted back.
For a binary data transfer, a memory mapped file, using a ByteBuffer, would be nice and relatively easy. And also compression would be feasible.
For reading/writing text, one could argue that developing is more reliant, as one can simply read resulting and transferred texts.
You must know the encoding, as the back-and-forth conversion could corrupt the data, substituting bytes.
The simplest way (besides the slightly harder but better ByteBuffer
) would be a RandomAccessFile
with which to read a block of bytes (byte[]
).
Then you create a ByteInputStream
to read bytes. and on that a InputStreamReader
to read text - in some Charset.
Upvotes: 0
Reputation: 9
A solution I found after reading some more documentation is to use the skip
method from fileReader this skip a certain number of characters so to fix my code i added myReader.skip(startPoint);
and before the if statment added long startPoint = this.fileSize - this.remainingBytes;
to calculate the starting position of the next segment.
If it runs out of characters though the code will return an error so you should make sure you have catches to prevent the error.
Upvotes: 1
Reputation: 312086
On each call to readData
you create a new FileReader
object, which will start reading the file from its beginning. Instead, you should keep the same reader in a data member, and continue reading from it each time readData
is called.
Upvotes: 2