Java by API/java.nio/LongBuffer

Материал из Java эксперт
Версия от 14:31, 31 мая 2010; Admin (обсуждение | вклад) (1 версия)
(разн.) ← Предыдущая | Текущая версия (разн.) | Следующая → (разн.)
Перейти к: навигация, поиск

LongBuffer: clear()

 
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);
    }
  }
}





LongBuffer: get()

 
import java.nio.ByteBuffer;
import java.nio.LongBuffer;
public class Main {
  public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, "a" });
    bb.rewind();
    LongBuffer lb = ((ByteBuffer) bb.rewind()).asLongBuffer();
    System.out.println("Long Buffer");
    while (lb.hasRemaining())
      System.out.println(lb.position() + " -> " + lb.get());
  }
}





LongBuffer: get(long[] dst)

 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.FileNotFoundException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
public class MainClass {
  public static void main(String[] args) {
    File aFile = new File("file.dat");
    FileInputStream inFile = null;
    try {
      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);
    }
  }
}





LongBuffer: position()

 
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);
    }
  }
}





LongBuffer: put(long l)

 
import java.nio.ByteBuffer;
public class Main {
  private static final int BSIZE = 1024;
  public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.allocate(BSIZE);
    bb.asLongBuffer().put(99472342341142L);
    System.out.println(bb.getLong());
  }
}





LongBuffer: put(long[] src, int offset, int length)

 
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);
    }
  }
}





LongBuffer: 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);
    }
  }
}





ShortBuffer: hasRemaining()

 
import java.nio.ByteBuffer;
import java.nio.ShortBuffer;
public class Main {
  public static void main(String[] args) {
    ByteBuffer bb = ByteBuffer.wrap(new byte[] { 0, 0, 0, 0, 0, 0, 0, "a" });
    bb.rewind();
    ShortBuffer sb = ((ByteBuffer) bb.rewind()).asShortBuffer();
    System.out.println("Short Buffer");
    while (sb.hasRemaining())
      System.out.println(sb.position() + " -> " + sb.get());
  }
}