[fpc-pascal] how to convert JNByteBuffer variable type to type usable by fw.write

Jonas Maebe jonas at freepascal.org
Tue Aug 27 08:52:53 CEST 2019


On 2019-08-26 15:13, Mgr. Janusz Chmiel wrote:
> Please, does somebody of us know how to write file by using binary
> mode and writing data from The variable which have JNByteBuffer
> variable type?

Here is a Java example on how to do it. You need to use 
java.io.FileOutputStream (JIFileOutputStream in FPC):

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Main {
   public static void main(String[] args) throws Exception {
     String fromFileName = "from.txt";
     String toFileName = "to.txt";
     FileChannel in = new FileInputStream(fromFileName).getChannel();
     FileChannel out = new FileOutputStream(toFileName).getChannel();

     ByteBuffer buff = ByteBuffer.allocateDirect(32 * 1024);

     while (in.read(buff) > 0) {
       buff.flip();
       out.write(buff);
       buff.clear();
     }

     in.close();
     out.close();
   }
}

(reproduced from 
http://www.java2s.com/Code/Java/File-Input-Output/WritewithByteBuffer.htm 
)


Jonas


More information about the fpc-pascal mailing list