OnJava8-Examples/newio/BufferToText.java

94 lines
2.5 KiB
Java
Raw Normal View History

2015-11-03 12:00:44 -08:00
// newio/BufferToText.java
2020-10-07 13:35:40 -06:00
// (c)2020 MindView LLC: see Copyright.txt
2015-11-15 15:51:35 -08:00
// We make no guarantees that this code is fit for any purpose.
2016-09-23 13:23:35 -06:00
// Visit http://OnJava8.com for more book information.
2015-06-15 17:47:35 -07:00
// Converting text to and from ByteBuffers
import java.nio.*;
import java.nio.channels.*;
import java.nio.charset.*;
import java.io.*;
public class BufferToText {
private static final int BSIZE = 1024;
2017-01-21 16:29:16 -08:00
public static void main(String[] args) {
try(
FileChannel fc = new FileOutputStream(
"data2.txt").getChannel()
) {
2016-01-25 18:05:55 -08:00
fc.write(ByteBuffer.wrap("Some text".getBytes()));
2017-01-21 16:29:16 -08:00
} catch(IOException e) {
throw new RuntimeException(e);
2016-01-25 18:05:55 -08:00
}
2015-06-15 17:47:35 -07:00
ByteBuffer buff = ByteBuffer.allocate(BSIZE);
try(
FileChannel fc = new FileInputStream(
"data2.txt").getChannel()
) {
2016-01-25 18:05:55 -08:00
fc.read(buff);
2017-01-21 16:29:16 -08:00
} catch(IOException e) {
throw new RuntimeException(e);
2016-01-25 18:05:55 -08:00
}
2015-06-15 17:47:35 -07:00
buff.flip();
// Doesn't work:
System.out.println(buff.asCharBuffer());
// Decode using this system's default Charset:
buff.rewind();
2017-01-21 16:29:16 -08:00
String encoding =
System.getProperty("file.encoding");
System.out.println("Decoded using " +
encoding + ": "
2015-06-15 17:47:35 -07:00
+ Charset.forName(encoding).decode(buff));
2017-01-21 16:29:16 -08:00
// Encode with something that prints:
try(
FileChannel fc = new FileOutputStream(
"data2.txt").getChannel()
) {
2016-01-25 18:05:55 -08:00
fc.write(ByteBuffer.wrap(
"Some text".getBytes("UTF-16BE")));
2017-01-21 16:29:16 -08:00
} catch(IOException e) {
throw new RuntimeException(e);
2016-01-25 18:05:55 -08:00
}
2015-06-15 17:47:35 -07:00
// Now try reading again:
buff.clear();
try(
FileChannel fc = new FileInputStream(
"data2.txt").getChannel()
) {
2016-01-25 18:05:55 -08:00
fc.read(buff);
2017-01-21 16:29:16 -08:00
} catch(IOException e) {
throw new RuntimeException(e);
2016-01-25 18:05:55 -08:00
}
2015-06-15 17:47:35 -07:00
buff.flip();
System.out.println(buff.asCharBuffer());
// Use a CharBuffer to write through:
2017-01-21 16:29:16 -08:00
buff = ByteBuffer.allocate(24);
2015-06-15 17:47:35 -07:00
buff.asCharBuffer().put("Some text");
try(
FileChannel fc = new FileOutputStream(
"data2.txt").getChannel()
) {
2016-01-25 18:05:55 -08:00
fc.write(buff);
2017-01-21 16:29:16 -08:00
} catch(IOException e) {
throw new RuntimeException(e);
2016-01-25 18:05:55 -08:00
}
2015-06-15 17:47:35 -07:00
// Read and display:
buff.clear();
try(
FileChannel fc = new FileInputStream(
"data2.txt").getChannel()
) {
2016-01-25 18:05:55 -08:00
fc.read(buff);
2017-01-21 16:29:16 -08:00
} catch(IOException e) {
throw new RuntimeException(e);
2016-01-25 18:05:55 -08:00
}
2015-06-15 17:47:35 -07:00
buff.flip();
System.out.println(buff.asCharBuffer());
}
2015-09-07 11:44:36 -06:00
}
/* Output:
2015-06-15 17:47:35 -07:00
????
2016-07-22 14:45:35 -06:00
Decoded using windows-1252: Some text
2015-06-15 17:47:35 -07:00
Some text
Some textNULNULNUL
2015-09-07 11:44:36 -06:00
*/