cannot copy file with other buffer sizes, except 1
Hi,
i'd like to copy files (txt, images, etc) , but by using the following route:
-read bytes from a file in a ByteBuffer
-use a byte array to create a String representation in binary form (ie "0101101") of the ByteBuffer
-print the string
and then the reverse:
-create a byte array from the String
-put the bytes in another ByteBuffer
-write them to the destination file
The problem is, that in the general case, this only works for ByteBuffer.allocate(1)! it works for txt files with any size, but not for image files; i have not tried for any other kind of file.
Ideas?
import java.io.*;
import java.nio.*;
import java.nio.channels.*;
import java.math.*;
/* create the copy of a file:
* In the middle print a string with the contents of the buffer, in binary form
* and then use this string to create the copy of the picture.
*/
publicclass CopyFile
{
staticpublicvoid main( String args[] )throws Exception
{
if (args.length<2){
System.err.println("Usage: java CopyFile infile outfile" );
System.exit( 1 );
}
String infile = args[0];
String outfile = args[1];
FileInputStream fin =new FileInputStream( infile );
FileOutputStream fout =new FileOutputStream( outfile );
FileChannel fcin = fin.getChannel();
FileChannel fcout = fout.getChannel();
ByteBuffer bufferSource = ByteBuffer.allocate( 1 );
ByteBuffer bufferDest = ByteBuffer.allocate( 1 );
byte[] barraySource;
byte[] barrayDest;
while (true){
bufferSource.clear();
int r = fcin.read( bufferSource );
barraySource=bufferSource.array();
BigInteger biFrom =new BigInteger(barraySource);
String binary = biFrom.toString(2);
System.out.println(binary+" | ");
BigInteger biTo=new BigInteger(binary,2);
barrayDest=biTo.toByteArray();
bufferDest.clear();
bufferDest.put(barrayDest);
if (r==-1){
break;
}
bufferDest.flip();
fcout.write(bufferDest);
}
}
}
[3233 byte] By [
uiga] at [2007-11-26 13:56:17]

I've changed a couple of things, but the problem remains. It is the first time i work with binary data. Can somebody give me a hint?
public class Test2
{
public static void main(String[] args) throws Exception
{
File fin=new File("C:\\Documents and Settings\\p3020139\\My Documents\\pic.JPG");
File fout=new File("C:\\Documents and Settings\\p3020139\\My Documents\\piccp.JPG");
FileInputStream fis=new FileInputStream(fin);
FileOutputStream fos=new FileOutputStream(fout);
long fileSize= fin.length();
long bytesRead=0;
BufferedOutputStream baos=null;
while (bytesRead<fileSize+1)
{
byte[] barray=new byte[128];
int read=fis.read(barray);
BigInteger biFrom = new BigInteger(barray);
String binary = biFrom.toString(2);
System.out.println(binary);
BigInteger biTo=new BigInteger(binary,2);
byte[] bytesTo=biTo.toByteArray();
baos=new BufferedOutputStream(fos,128);
baos.write(bytesTo);
bytesRead=bytesRead+read;
}
fis.close();
baos.close();
}
>
uiga at 2007-7-8 1:35:45 >

Hi DrClap,
is this the differenece?
When i read/buffer more than one bytes, i write back this for reading 1 ,2, 3:
10000001000000011
The numbers are padded with 0s !
=================================
But when i read one byte at a time and write it back, i write back this for 1,2,3:
1
10
11
import java.math.*;
public class Test3 {
public static void main(String[] args)
{
byte[] bytes = new byte[]{(byte)1, (byte)2, (byte)3};
BigInteger bi = new BigInteger(bytes);
String s = bi.toString(2);
System.out.println(s);
System.out.println("=================================");
byte[] bytes1 = new byte[]{(byte)1};
byte[] bytes2 = new byte[]{(byte)2};
byte[] bytes3 = new byte[]{(byte)3};
BigInteger bi1 = new BigInteger(bytes1);
BigInteger bi2 = new BigInteger(bytes2);
BigInteger bi3 = new BigInteger(bytes3);
String s1 = bi1.toString(2);
String s2 = bi2.toString(2);
String s3 = bi3.toString(2);
System.out.println(s1);
System.out.println(s2);
System.out.println(s3);
}
}
uiga at 2007-7-8 1:35:45 >
