Java by API/java.nio/Buffer — различия между версиями

Материал из Java эксперт
Перейти к: навигация, поиск
м (1 версия)
 
(нет различий)

Текущая версия на 14:31, 31 мая 2010

Buffer: hasRemaining()

 
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class Main {
  private static final int BSIZE = 1024;
  public static void main(String[] args) throws Exception {
    FileChannel fc = new FileInputStream("data.txt").getChannel();
    ByteBuffer buff = ByteBuffer.allocate(BSIZE);
    fc.read(buff);
    buff.flip();
    while (buff.hasRemaining())
      System.out.print((char) buff.get());
  }
}





Buffer: limit(int newLimit)

 
import static java.lang.Math.ceil;
import static java.lang.Math.min;
import static java.lang.Math.sqrt;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.LongBuffer;
import java.nio.channels.FileChannel;
public class MainClass {
  public static void main(String[] args) {
    int count = 100;
    long[] numbers = new long[count];
    for (int i = 0; i < numbers.length; i++) {
      numbers[i] = i;
    }
    File aFile = new File("data.bin");
    FileOutputStream outputFile = null;
    try {
      outputFile = new FileOutputStream(aFile);
    } catch (FileNotFoundException e) {
      e.printStackTrace(System.err);
      System.exit(1);
    }
    FileChannel file = outputFile.getChannel();
    final int BUFFERSIZE = 100;
    ByteBuffer buf = ByteBuffer.allocate(BUFFERSIZE);
    LongBuffer longBuf = buf.asLongBuffer();
    int numberWritten = 0;
    while (numberWritten < numbers.length) {
      longBuf.put(numbers, numberWritten, min(longBuf.capacity(), numbers.length - numberWritten));
      buf.limit(8 * longBuf.position());
      try {
        file.write(buf);
        numberWritten += longBuf.position();
      } catch (IOException e) {
        e.printStackTrace(System.err);
        System.exit(1);
      }
      longBuf.clear();
      buf.clear();
    }
    try {
      System.out.println("File written is " + file.size() + " bytes.");
      outputFile.close();
    } catch (IOException e) {
      e.printStackTrace(System.err);
      System.exit(1);
    }
  }
}





Buffer: remaining()

 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.nio.ByteBuffer;
import java.nio.LongBuffer;
import java.nio.BufferUnderflowException;
import java.nio.channels.FileChannel;
public class MainClass {
  public static void main(String[] args) {
    File aFile = new File("data.dat");
    FileInputStream inFile = null;
    try {
      inFile = new FileInputStream(aFile);
    } catch (FileNotFoundException e) {
      e.printStackTrace(System.err);
      System.exit(1);
    }
    FileChannel inChannel = inFile.getChannel();
    final int COUNT = 6;
    ByteBuffer buf = ByteBuffer.allocate(8 * COUNT);
    long[] data = new long[COUNT];
    try {
      int pos = 0;
      while (inChannel.read(buf) != -1) {
        try {
          ((ByteBuffer) (buf.flip())).asLongBuffer().get(data);
          pos = data.length;
        } catch (BufferUnderflowException e) {
          LongBuffer longBuf = buf.asLongBuffer();
          pos = longBuf.remaining();
          longBuf.get(data, 0, pos);
        }
        System.out.println();
        for (int i = 0; i < pos; i++) {
          System.out.printf("%10d", data[i]);
        }
        buf.clear();
      }
      System.out.println("\nEOF reached.");
      inFile.close();
    } catch (IOException e) {
      e.printStackTrace(System.err);
      System.exit(1);
    }
  }
}