Package java.nio

Examples of java.nio.MappedByteBuffer



        FileChannel inChannel = inRandom.getChannel();

        //将输入文件的通道通过只读的权限 映射到内存中。
        MappedByteBuffer byteMapper = inChannel.map(FileChannel.MapMode.READ_ONLY, 0, (int) inFile.length());

        CharsetDecoder inDecoder = inCharset.newDecoder();
        CharsetEncoder outEncoder = outCharset.newEncoder();

        CharBuffer cb = inDecoder.decode(byteMapper);
View Full Code Here


                            || (start > 0 && start <= logFileTime && end > 0 && end >= logFileTime)) {

                        // It's in the range, so process the file
                        RandomAccessFile raf = new RandomAccessFile(logFile, "r");
                        FileChannel fc = raf.getChannel();
                        MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
                        CharBuffer cb = Charset.forName("US-ASCII").decode(bb); //todo: does Jetty use a different charset on a foreign PC?
                        Matcher lines = FULL_LINE_PATTERN.matcher(cb);
                        Matcher target = ACCESS_LOG_PATTERN.matcher("");
                        SimpleDateFormat format = (start == 0 && end == 0) ? null : new SimpleDateFormat(ACCESS_LOG_DATE_FORMAT);
                        int max = maxResults == null ? MAX_SEARCH_RESULTS : Math.min(maxResults.intValue(), MAX_SEARCH_RESULTS);
View Full Code Here

    static public String read(String path) throws MinException {
        try {
            FileInputStream stream = new FileInputStream(new java.io.File(path));
            try {
                FileChannel fc = stream.getChannel();
                MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
        /* Instead of using default, pass in a decoder. */
                return java.nio.charset.Charset.defaultCharset().decode(bb).toString();
            } finally {
                stream.close();
            }
View Full Code Here

        boolean capped = false;
        int lineCount = 0;
        try {
            RandomAccessFile raf = new RandomAccessFile(file, "r");
            FileChannel fc = raf.getChannel();
            MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
            CharBuffer cb = Charset.forName("US-ASCII").decode(bb); //todo: does Jetty use a different charset on a foreign PC?
            Matcher lines = FULL_LINE_PATTERN.matcher(cb);
            Matcher target = ACCESS_LOG_PATTERN.matcher("");
            long start = startDate == null ? 0 : startDate.getTime();
            long end = endDate == null ? 0 : endDate.getTime();
View Full Code Here

        boolean capped = false;
        int lineCount = 0;
        try {
            RandomAccessFile raf = new RandomAccessFile(file, "r");
            FileChannel fc = raf.getChannel();
            MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
            CharBuffer cb = Charset.forName("US-ASCII").decode(bb); //todo: does Log4J use a different charset on a foreign PC?
            Matcher target = null;
            Matcher any = null;
            Matcher lines = FULL_LINE_PATTERN.matcher(cb);
            Matcher text = textSearch == null ? null : textSearch.matcher("");
View Full Code Here

        boolean capped = false;
        int lineCount = 0;
        try {
            RandomAccessFile raf = new RandomAccessFile(file, "r");
            FileChannel fc = raf.getChannel();
            MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
            CharBuffer cb = Charset.forName("US-ASCII").decode(bb); //todo: does Log4J use a different charset on a foreign PC?
            Matcher target = null;
            Matcher any = null;
            Matcher lines = FULL_LINE_PATTERN.matcher(cb);
            Matcher text = textSearch == null ? null : textSearch.matcher("");
View Full Code Here

   * OS pages into this process's page. It will be automatically unmapped when
   * the ByteBuffer that is returned here goes out of scope. This method is
   * currently invoked only by the FSDataInputStream ScatterGather api.
   */
  public ByteBuffer readAll() throws IOException {
    MappedByteBuffer bb = dataIn.getChannel().map(FileChannel.MapMode.READ_ONLY,
                                 startOffset, length);
    return bb; 
  }
View Full Code Here

               {
                  chf = File.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
                  // TODO don't use Channels.newChannel in Java5
                  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();
                     chf.delete();
                  }
                  catch (Exception e1)
                  {
                  }
                  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();
                     chf.delete();
                  }
                  catch (Exception e1)
                  {
                  }
                  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 (!spoolFile.delete())
            {
               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. "
                     + spoolFile.getAbsolutePath());
               }
            }

            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

               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); // TODO don't use Channels.newChannel on java5
            ch.write(bb);
            ch.close();
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.