List of usage examples for java.nio ByteBuffer allocate
public static ByteBuffer allocate(int capacity)
From source file:MainClass.java
public static void main(String[] args) throws Exception { createFile();/* www.ja v a2 s.c o m*/ File aFile = new File("C:/primes.bin"); FileInputStream inFile = new FileInputStream(aFile); FileChannel inChannel = inFile.getChannel(); final int PRIMESREQUIRED = 10; ByteBuffer buf = ByteBuffer.allocate(8 * PRIMESREQUIRED); long[] primes = new long[PRIMESREQUIRED]; int index = 0; // Position for a prime in the file // Count of primes in the file final int PRIMECOUNT = (int) inChannel.size() / 8; // Read the number of random primes required for (int i = 0; i < PRIMESREQUIRED; i++) { index = 8 * (int) (PRIMECOUNT * Math.random()); inChannel.read(buf, index); // Read the value buf.flip(); primes[i] = buf.getLong(); // Save it in the array buf.clear(); } for (long prime : primes) { System.out.printf("%12d", prime); } inFile.close(); // Close the file and the channel }
From source file:Main.java
public static void main(String[] args) throws Exception { Path path = Paths.get("test.txt"); try (AsynchronousFileChannel afc = AsynchronousFileChannel.open(path, StandardOpenOption.READ)) { int fileSize = (int) afc.size(); ByteBuffer dataBuffer = ByteBuffer.allocate(fileSize); Future<Integer> result = afc.read(dataBuffer, 0); int readBytes = result.get(); System.out.format("%s bytes read from %s%n", readBytes, path); System.out.format("Read data is:%n"); byte[] byteData = dataBuffer.array(); Charset cs = Charset.forName("UTF-8"); String data = new String(byteData, cs); System.out.println(data); } catch (IOException ex) { ex.printStackTrace();/*from w w w . jav a2s.c o m*/ } }
From source file:Main.java
public static void main(String[] args) throws IOException { FileInputStream fin = new FileInputStream(args[0]); GZIPInputStream gzin = new GZIPInputStream(fin); ReadableByteChannel in = Channels.newChannel(gzin); WritableByteChannel out = Channels.newChannel(System.out); ByteBuffer buffer = ByteBuffer.allocate(65536); while (in.read(buffer) != -1) { buffer.flip();//from w w w. j a va 2s . co m out.write(buffer); buffer.clear(); } }
From source file:BufferConverter.java
public static void main(String[] arguments) { try {/*w w w . ja v a 2 s. com*/ String data = "friends.dat"; FileInputStream inData = new FileInputStream(data); FileChannel inChannel = inData.getChannel(); long inSize = inChannel.size(); ByteBuffer source = ByteBuffer.allocate((int) inSize); inChannel.read(source, 0); source.position(0); for (int i = 0; source.remaining() > 0; i++) System.out.print(source.get() + " "); source.position(0); Charset ascii = Charset.forName("US-ASCII"); CharsetDecoder toAscii = ascii.newDecoder(); CharBuffer destination = toAscii.decode(source); destination.position(0); System.out.println("\n\nNew character data:"); for (int i = 0; destination.remaining() > 0; i++) System.out.print(destination.get()); } catch (Exception ioe) { System.out.println(ioe.getMessage()); } }
From source file:MainClass.java
public static void main(String[] args) { File aFile = new File("afile.txt"); FileOutputStream outputFile = null; try {/*from w w w.j a v a 2 s . c om*/ outputFile = new FileOutputStream(aFile, true); System.out.println("File stream created successfully."); } catch (FileNotFoundException e) { e.printStackTrace(System.err); } ByteBuffer buf = ByteBuffer.allocate(1024); System.out.println("\nByte buffer:"); System.out.printf("position = %2d Limit = %4d capacity = %4d%n", buf.position(), buf.limit(), buf.capacity()); // Create a view buffer CharBuffer charBuf = buf.asCharBuffer(); System.out.println("Char view buffer:"); System.out.printf("position = %2d Limit = %4d capacity = %4d%n", charBuf.position(), charBuf.limit(), charBuf.capacity()); try { outputFile.close(); // Close the O/P stream & the channel } catch (IOException e) { e.printStackTrace(System.err); } }
From source file:Main.java
public static void main(String[] args) { File inputFile = new File("test1.txt"); if (!inputFile.exists()) { System.out.println("The input file " + inputFile.getAbsolutePath() + " does not exist."); System.out.println("Aborted the file reading process."); return;/* w w w . ja va2s .com*/ } try (FileChannel fileChannel = new FileInputStream(inputFile).getChannel()) { ByteBuffer buffer = ByteBuffer.allocate(1024); while (fileChannel.read(buffer) > 0) { buffer.flip(); while (buffer.hasRemaining()) { byte b = buffer.get(); System.out.print((char) b); } buffer.clear(); } } catch (IOException e) { e.printStackTrace(); } }
From source file:MainClass.java
public static void main(String[] args) { File aFile = new File("C:/test.bin"); FileInputStream inFile = null; try {// ww w. j a v a2 s .co m inFile = new FileInputStream(aFile); } catch (FileNotFoundException e) { e.printStackTrace(System.err); } FileChannel inChannel = inFile.getChannel(); final int PRIMECOUNT = 6; ByteBuffer buf = ByteBuffer.allocate(8 * PRIMECOUNT); long[] primes = new long[PRIMECOUNT]; try { while (inChannel.read(buf) != -1) { ((ByteBuffer) (buf.flip())).asLongBuffer().get(primes); for (long prime : primes) { System.out.printf("%10d", prime); } buf.clear(); } inFile.close(); } catch (IOException e) { e.printStackTrace(System.err); } }
From source file:MainClass.java
public static void main(String[] args) { File aFile = new File("primes.txt"); FileInputStream inFile = null; try {//from ww w .j a v a2 s .c om inFile = new FileInputStream(aFile); } catch (FileNotFoundException e) { e.printStackTrace(System.err); } FileChannel inChannel = inFile.getChannel(); try { ByteBuffer lengthBuf = ByteBuffer.allocate(8); while (true) { if (inChannel.read(lengthBuf) == -1) { break; } lengthBuf.flip(); int strLength = (int) lengthBuf.getDouble(); ByteBuffer buf = ByteBuffer.allocate(2 * strLength + 8); if (inChannel.read(buf) == -1) { break; } buf.flip(); byte[] strChars = new byte[2 * strLength]; buf.get(strChars); System.out.println(strLength); System.out.println(ByteBuffer.wrap(strChars).asCharBuffer()); System.out.println(buf.getLong()); lengthBuf.clear(); } inFile.close(); } catch (IOException e) { e.printStackTrace(System.err); } }
From source file:Test.java
public static void main(String[] args) throws Exception { Path path = Paths.get("C:/home/docs/users.txt"); UserDefinedFileAttributeView view = Files.getFileAttributeView(path, UserDefinedFileAttributeView.class); view.write("publishable", Charset.defaultCharset().encode("true")); System.out.println("Publishable set"); String name = "publishable"; ByteBuffer buffer = ByteBuffer.allocate(view.size(name)); view.read(name, buffer);//from www . j a v a2 s. c om buffer.flip(); String value = Charset.defaultCharset().decode(buffer).toString(); System.out.println(value); }
From source file:MainClass.java
public static void main(String[] args) { File aFile = new File("file.dat"); FileInputStream inFile = null; try {// ww w. j ava 2 s. c o m inFile = new FileInputStream(aFile); } catch (FileNotFoundException e) { e.printStackTrace(System.err); System.exit(0); } FileChannel inChannel = inFile.getChannel(); final int COUNT = 6; ByteBuffer buf = ByteBuffer.allocate(8 * COUNT); long[] data = new long[COUNT]; try { while (inChannel.read(buf) != -1) { ((ByteBuffer) (buf.flip())).asLongBuffer().get(data); System.out.println(); for (long prime : data) System.out.printf("%10d", prime); buf.clear(); } System.out.println("\nEOF reached."); inFile.close(); } catch (IOException e) { e.printStackTrace(System.err); System.exit(1); } }