PDA

View Full Version : ByteBuffer Issues


japangreg
08-10-2007, 09:45 PM
Hi, all.

Quick problem I'm having with a project - I'm trying to read a file into a ByteBuffer and have the bytes cast into chars for output. When this occurs, I'm getting a "0- exit" at the end of my string of chars. Another function that reverses the buffer chokes on this, although all code functioned correctly when I first wrote it using CharBuffer and char[] to reverse.

If someone could please take a look below and let me know what might be amiss. Thanks!

public void showFile() throws IOException{
// display contents of file
FileChannel inChannel = (new FileInputStream(fileName)).getChannel();

originalBytes.clear();
// ByteBuffer obj initiated earlier

originalBytes.allocate((int)inChannel.size());

while(inChannel.read(originalBytes) != -1){
// read bytes into buffer
}

originalBytes.flip();

inChannel.close();

// now, convert bytes to chars and output

while (originalBytes.hasRemaining()){
System.out.print( (char) (originalBytes.get() & 0xFF) );
}

}

public void reverseFile(){
// reverse the file
originalBytes.rewind();

// declare 2nd buffer
reverseBytes.allocate(originalBytes.limit());

// read first buffer into second in reverse order

for (int i = originalBytes.limit(); i <=0; i--){
reverseBytes.put(i, originalBytes.get());
}

try{
FileChannel outChannel = (new FileOutputStream(fileName)).getChannel();

outChannel.write(reverseBytes);

outChannel.close();

originalBytes.clear();
System.out.println("The file has been reversed. Please select option 2 to view the results.\n");
} catch (IOException ioe){
System.out.println("Write to file failed.");
}

}

javabits
08-16-2007, 08:52 PM
You're calling the allocate method incorrectly. It's a static method belonging to ByteBuffer. Inside of showFile you're doing a lot of unnecessary work, there's no reason to slow the transfer of the file with your own read loop, the whole purpose of nio is for speed so reading things a byte at a time defeats the purpose. Your reverseBuffer has some logic errors with regards to the indexes of the Buffers (i.e. if it has a size of 10 the indexes will be from 0 to 9). Your for loop condition causes it to never enter the loop.

Here's corrected code (I'm outputting the reversed file with a .rev extension):


import java.io.*;
import java.nio.*;
import java.nio.channels.*;

public class channels
{
private String fileName;
private ByteBuffer originalBytes;
private ByteBuffer reverseBytes;

public channels(String string) {
fileName = string;
}

public void showFile() throws IOException {
FileChannel filechannel = new FileInputStream(fileName).getChannel();
originalBytes = ByteBuffer.allocate((int) filechannel.size());
int i = filechannel.read(originalBytes);
originalBytes.rewind();
filechannel.close();
WritableByteChannel out = Channels.newChannel( System.out );
out.write( originalBytes );
}

public void reverseFile() {
originalBytes.rewind();
reverseBytes = ByteBuffer.allocate(originalBytes.limit());
for (int i = originalBytes.limit() - 1; i >= 0; i--)
{
reverseBytes.put(originalBytes.get(i));
}
reverseBytes.rewind();
try {
FileChannel filechannel = new FileOutputStream(fileName+".rev").getChannel();
filechannel.write(reverseBytes);
filechannel.close();
System.out.println("The file has been reversed. Please select option 2 to view the results.\n");
} catch (IOException ioexception) {
System.out.println("Write to file failed.");
}
}

public static void main(String[] strings) {
channels c = new channels(strings[0]);
try {
c.showFile();
c.reverseFile();
} catch (Exception e) {
e.printStackTrace();
}
}
}


semper fi...