Ejemplo n.º 1
0
  // Only use when we know the seek in within the mapped segment. Throws an
  // IOException otherwise.
  public void seek(long pos) throws IOException {
    long inSegmentPos = pos - segmentOffset;
    if (inSegmentPos < 0 || inSegmentPos > buffer.capacity())
      throw new IOException(
          String.format(
              "Seek position %d is not within mmap segment (seg offs: %d, length: %d)",
              pos, segmentOffset, buffer.capacity()));

    position = (int) inSegmentPos;
  }
Ejemplo n.º 2
0
  /**
   * Read the contents of a text file using a memory-mapped byte buffer.
   *
   * <p>A MappedByteBuffer, is simply a special ByteBuffer. MappedByteBuffer maps a region of a file
   * directly in memory. Typically, that region comprises the entire file, although it could map a
   * portion. You must, therefore, specify what part of the file to map. Moreover, as with the other
   * Buffer objects, no constructor exists; you must ask the java.nio.channels.FileChannel for its
   * map() method to get a MappedByteBuffer.
   *
   * <p>Direct buffers allocate their data directly in the runtime environment memory, bypassing the
   * JVM|OS boundary, usually doubling file copy speed. However, they generally cost more to
   * allocate.
   */
  private static String fastStreamCopy(String filename) {
    String s = "";
    FileChannel fc = null;
    try {
      fc = new FileInputStream(filename).getChannel();

      // int length = (int)fc.size();

      MappedByteBuffer byteBuffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
      // CharBuffer charBuffer =
      // Charset.forName("ISO-8859-1").newDecoder().decode(byteBuffer);

      // ByteBuffer byteBuffer = ByteBuffer.allocate(length);
      // ByteBuffer byteBuffer = ByteBuffer.allocateDirect(length);
      // CharBuffer charBuffer = byteBuffer.asCharBuffer();

      // CharBuffer charBuffer =
      // ByteBuffer.allocateDirect(length).asCharBuffer();
      /*
       * int size = charBuffer.length(); if (size > 0) { StringBuffer sb =
       * new StringBuffer(size); for (int count=0; count<size; count++)
       * sb.append(charBuffer.get()); s = sb.toString(); }
       *
       * if (length > 0) { StringBuffer sb = new StringBuffer(length); for
       * (int count=0; count<length; count++) {
       * sb.append(byteBuffer.get()); } s = sb.toString(); }
       */
      int size = byteBuffer.capacity();
      if (size > 0) {
        // Retrieve all bytes in the buffer
        byteBuffer.clear();
        byte[] bytes = new byte[size];
        byteBuffer.get(bytes, 0, bytes.length);
        s = new String(bytes);
      }

      fc.close();
    } catch (FileNotFoundException fnfx) {
      System.err.println("File not found: " + fnfx);
    } catch (IOException iox) {
      System.err.println("I/O problems: " + iox);
    } finally {
      if (fc != null) {
        try {
          fc.close();
        } catch (IOException ignore) {
          // ignore
        }
      }
    }
    return s;
  }
Ejemplo n.º 3
0
  /**
   * Regression for HARMONY-6315 - FileChannel.map throws IOException when called with size 0
   *
   * @throws IOException
   */
  public void testEmptyBuffer() throws IOException {
    // Map empty file
    FileInputStream fis = new FileInputStream(emptyFile);
    FileChannel fc = fis.getChannel();
    MappedByteBuffer mmb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());

    // check non-null
    assertNotNull("MappedByteBuffer created from empty file should not be null", mmb);

    // check capacity is 0
    int len = mmb.capacity();
    assertEquals("MappedByteBuffer created from empty file should have 0 capacity", 0, len);

    assertFalse(
        "MappedByteBuffer from empty file shouldn't be backed by an array ", mmb.hasArray());

    try {
      byte b = mmb.get();
      fail(
          "Calling MappedByteBuffer.get() on empty buffer should throw a BufferUnderflowException");
    } catch (BufferUnderflowException e) {
      // expected behaviour
    }

    // test expected exceptions thrown
    try {
      mmb = fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size());
      fail("Expected NonWritableChannelException to be thrown");
    } catch (NonWritableChannelException e) {
      // expected behaviour
    }
    try {
      mmb = fc.map(FileChannel.MapMode.PRIVATE, 0, fc.size());
      fail("Expected NonWritableChannelException to be thrown");
    } catch (NonWritableChannelException e) {
      // expected behaviour
    }
    fc.close();
  }
Ejemplo n.º 4
0
  /**
   * A regression test for failing to correctly set capacity of underlying wrapped buffer from a
   * mapped byte buffer.
   */
  public void testasIntBuffer() throws IOException {
    // Map file
    FileInputStream fis = new FileInputStream(tmpFile);
    FileChannel fc = fis.getChannel();
    MappedByteBuffer mmb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
    int len = mmb.capacity();
    assertEquals("Got wrong number of bytes", 46, len); // $NON-NLS-1$

    // Read in our 26 bytes
    for (int i = 0; i < 26; i++) {
      byte b = mmb.get();
      assertEquals("Got wrong byte value", (byte) 'A' + i, b); // $NON-NLS-1$
    }

    // Now convert to an IntBuffer to read our ints
    IntBuffer ibuffer = mmb.asIntBuffer();
    for (int i = 0; i < 5; i++) {
      int val = ibuffer.get();
      assertEquals("Got wrong int value", i + 1, val); // $NON-NLS-1$
    }
    fc.close();
  }
Ejemplo n.º 5
0
 public boolean isEOF() throws IOException {
   return position == buffer.capacity();
 }
Ejemplo n.º 6
0
 protected long getPositionLimit() {
   return segmentOffset + buffer.capacity();
 }
Ejemplo n.º 7
0
 public long bytesRemaining() throws IOException {
   return buffer.capacity() - position;
 }