Package java.nio

Examples of java.nio.MappedByteBuffer


  private byte[] mapFileIn(File infile) throws IOException{
    FileInputStream fis = new FileInputStream(infile);
    try{
      FileChannel fc = fis.getChannel(); // Get the file's size and then map it into memory
      int sz = (int)fc.size();
      MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz);
      byte[] data2 = new byte[bb.remaining()];
      bb.get(data2);
      return data2;
    }
    finally{//Ensures resources are closed regardless of whether the action suceeded
      fis.close();
    }
View Full Code Here


      if (position + length >= channel.size())
      {
         length = channel.size() - position;
      }

      MappedByteBuffer bb = channel.map(FileChannel.MapMode.READ_ONLY, position, length);

      WritableByteChannel ch;
      if (stream instanceof FileOutputStream)
      {
         ch = ((FileOutputStream)stream).getChannel();
View Full Code Here

               throw new IOException("Position " + position + " out of value size " + spoolChannel.size());

            if (position + length >= spoolChannel.size())
               length = spoolChannel.size() - position;

            MappedByteBuffer bb = spoolChannel.map(FileChannel.MapMode.READ_ONLY, position, length);

            WritableByteChannel ch = Channels.newChannel(stream);
            ch.write(bb);
            ch.close();
View Full Code Here

               {
                  chf = SpoolFile.createTempFile("jcrvdedit", null, tempDirectory);
                  chch = new RandomAccessFile(chf, "rw").getChannel();

                  // allocate the space for whole file
                  MappedByteBuffer bb = chch.map(FileChannel.MapMode.READ_WRITE, position + length, 0);
                  bb.force();
                  bb = null;

                  ReadableByteChannel bch = Channels.newChannel(new ByteArrayInputStream(this.data));

                  if ((newIndex = (int)position) > 0)
                  {
                     // begin from the existed bytes
                     chch.transferFrom(bch, 0, newIndex < data.length ? newIndex : data.length);
                     bch.close();
                  }

                  // write update data
                  ReadableByteChannel sch = Channels.newChannel(stream);
                  chch.transferFrom(sch, newIndex, length);
                  sch.close();
                  newIndex += length;

                  if (newIndex < data.length)
                     // write the rest of existed data
                     chch.transferFrom(bch, newIndex, data.length - newIndex);

                  bch.close();
               }
               catch (final IOException e)
               {
                  try
                  {
                     chch.close();
                     PrivilegedFileHelper.delete(chf);
                  }
                  catch (Exception e1)
                  {
                     if (LOG.isTraceEnabled())
                     {
                        LOG.trace("An exception occurred: " + e1.getMessage());
                     }
                  }
                  throw new IOException("update error " + e.getMessage())
                  {
                     @Override
                     public Throwable getCause()
                     {
                        return e;
                     }
                  };
               }
               this.spoolFile = chf;
               this.spoolChannel = chch;
               this.data = null;
            }
         }
         else
         {
            MappedByteBuffer bb = spoolChannel.map(FileChannel.MapMode.READ_WRITE, position, length);

            ReadableByteChannel ch = Channels.newChannel(stream);
            ch.read(bb);
            ch.close();

            bb.force();
         }
      }
View Full Code Here

                  bch.close();

                  if (chch.size() < size)
                  {
                     // extend length
                     MappedByteBuffer bb = chch.map(FileChannel.MapMode.READ_WRITE, size, 0);
                     bb.force();
                  }
               }
               catch (final IOException e)
               {
                  try
                  {
                     chch.close();
                     PrivilegedFileHelper.delete(chf);
                  }
                  catch (Exception e1)
                  {
                     if (LOG.isTraceEnabled())
                     {
                        LOG.trace("An exception occurred: " + e1.getMessage());
                     }
                  }
                  throw new IOException("setLength(" + size + ") error. " + e.getMessage())
                  {
                     @Override
                     public Throwable getCause()
                     {
                        return e;
                     }
                  };
               }
               this.spoolFile = chf;
               this.spoolChannel = chch;
               this.data = null;
            }
         }
         else if (size < maxBufferSize)
         {
            // switch to bytes
            ByteBuffer bb = ByteBuffer.allocate((int)size);
            spoolChannel.force(false);
            spoolChannel.position(0);
            spoolChannel.read(bb);

            byte[] tmpb = null;

            if (bb.hasArray())
            {
               tmpb = bb.array();
            }
            else
            {
               // impossible code in most cases, as we use heap backed buffer
               tmpb = new byte[bb.capacity()];
               bb.get(tmpb);
            }

            spoolChannel.close();

            // delete file
            if (!PrivilegedFileHelper.delete(spoolFile))
            {
               if (fileCleaner != null)
               {
                  LOG.info("Could not remove file. Add to fileCleaner " + spoolFile);
                  fileCleaner.addFile(spoolFile);
               }
               else
               {
                  LOG.warn("Could not remove temporary file on switch to bytes, fileCleaner not found. "
                     + PrivilegedFileHelper.getAbsolutePath(spoolFile));
               }
            }

            data = tmpb;
            spoolChannel = null;
            spoolFile = null;
         }
         else
         {
            if (spoolChannel.size() < size)
            {
               // extend file
               MappedByteBuffer bb = spoolChannel.map(FileChannel.MapMode.READ_WRITE, size, 0);
               bb.force();
            }
            else
            {
               // truncate file
               spoolChannel.truncate(size);
View Full Code Here

    short numberOfSections;
    /* A pointer to the "optional" header (which will always be present in .exe files */
    int ptrOptionalHeader;

    /* The file we're reading from.*/
    MappedByteBuffer file = new FileInputStream(filename).getChannel().map(FileChannel.MapMode.READ_ONLY, 0, new File(filename).length());
    /* Set the file ordering to little endian */
    file.order(ByteOrder.LITTLE_ENDIAN);

    /* The start of the PE is pointed at by the 0x3c'th byte */
    peStart = file.getInt(PE_START);

    /* The first 4 bytes are the signature */
    peSignature = file.getInt(peStart + 0);

    /* Verify that it's a valid pe file.  IF not, throw an exception */
    if(peSignature != 0x00004550)
      throw new IOException("Invalid PE file!");

    /* The number of sections is the short starting at the 6th byte */
    numberOfSections = file.getShort(peStart + 6);

    /* Get a pointer to the optional header */
    ptrOptionalHeader = peStart + 24;


View Full Code Here

    /**
     * @tests java.nio.channels.FileChannel#map(MapMode,long,long)
     */
    public void test_map_Private_NonZeroPosition() throws IOException {
        MappedByteBuffer mapped = readWriteFileChannel.map(MapMode.PRIVATE, 10,
                CONTENT_LENGTH - 10);
        assertEquals(CONTENT_LENGTH - 10, mapped.limit());
        assertEquals(CONTENT_LENGTH - 10, mapped.capacity());
        assertEquals(0, mapped.position());
    }
View Full Code Here

    /**
     * @tests java.nio.channels.FileChannel#map(MapMode,long,long)
     */
    public void test_map_ReadWrite() throws IOException {
        MappedByteBuffer mapped = null;
        writeDataToFile(fileOfReadWriteFileChannel);
        mapped = readWriteFileChannel.map(MapMode.READ_WRITE, 0, CONTENT
                .length());

        // put something will change its channel
        ByteBuffer returnByPut = mapped.put(TEST_BYTES);
        assertSame(returnByPut, mapped);
        String checkString = "test" + CONTENT.substring(4);
        ByteBuffer checkBuffer = ByteBuffer.allocate(CONTENT_LENGTH);
        mapped.force();
        readWriteFileChannel.position(0);
        readWriteFileChannel.read(checkBuffer);
        assertEquals(checkString, new String(checkBuffer.array(), "iso8859-1"));

        try {
            mapped.put(("test" + CONTENT).getBytes("iso8859-1"));
            fail("should throw BufferOverflowException.");
        } catch (BufferOverflowException ex) {
            // expected;
        }
    }
View Full Code Here

     * @tests java.nio.channels.FileChannel#map(MapMode,long,long)
     */
    public void test_map_ReadWrite_NonZeroPosition() throws IOException {
        // test position non-zero
        writeDataToFile(fileOfReadWriteFileChannel);
        MappedByteBuffer mapped = readWriteFileChannel.map(MapMode.READ_WRITE,
                10, CONTENT_LENGTH - 10);
        assertEquals(CONTENT_LENGTH - 10, mapped.limit());
        assertEquals(CONTENT.length() - 10, mapped.capacity());
        assertEquals(0, mapped.position());
        mapped.put(TEST_BYTES);
        ByteBuffer checkBuffer = ByteBuffer.allocate(CONTENT_LENGTH);
        readWriteFileChannel.read(checkBuffer);
        String expected = CONTENT.substring(0, 10) + "test"
                + CONTENT.substring(10 + "test".length());
        assertEquals(expected, new String(checkBuffer.array(), "iso8859-1"));
View Full Code Here

                readOnlyFileChannel = new FileInputStream(fileOfReadOnlyFileChannel)
                        .getChannel();
            }

            writeLargeDataToFile(fileOfReadOnlyFileChannel, sizes[i] + 2 * CONTENT_LEN);
            MappedByteBuffer mapped = readOnlyFileChannel.map(MapMode.READ_ONLY,
                    sizes[i], CONTENT_LEN);
            assertEquals("Incorrectly mapped file channel for " + sizes[i]
                    + " position (capacity)", CONTENT_LEN, mapped.capacity());
            assertEquals("Incorrectly mapped file channel for " + sizes[i]
                    + " position (limit)", CONTENT_LEN, mapped.limit());
            assertEquals("Incorrectly mapped file channel for " + sizes[i]
                    + " position (position)", 0, mapped.position());

            // map not change channel's position
            assertEquals(0, readOnlyFileChannel.position());

            // Close the file and the channel before the next iteration
View Full Code Here

TOP

Related Classes of java.nio.MappedByteBuffer

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.