Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  //
  //  ========================================================================
  //  Copyright (c) 1995-2013 Mort Bay Consulting Pty. Ltd.
  //  ------------------------------------------------------------------------
  //  All rights reserved. This program and the accompanying materials
  //  are made available under the terms of the Eclipse Public License v1.0
  //  and Apache License v2.0 which accompanies this distribution.
  //
  //      The Eclipse Public License is available at
 //      http://www.eclipse.org/legal/epl-v10.html
 //
 //      The Apache License v2.0 is available at
 //      http://www.opensource.org/licenses/apache2.0.php
 //
 //  You may elect to redistribute this code under either of these licenses.
 //  ========================================================================
 //
 
 package org.eclipse.jetty.io.nio;
 
 import java.io.File;
 
 
 public class RandomAccessFileBuffer extends AbstractBuffer implements Buffer
 {
     final RandomAccessFile _file;
     final FileChannel _channel;
     final int _capacity;
 
     public RandomAccessFileBuffer(File file
         throws FileNotFoundException
     {
         super(,true);
         assert file.length()<=.;
          = new RandomAccessFile(file,"rw");
         =.getChannel();
         =.;
         setGetIndex(0);
         setPutIndex((int)file.length());
     }
     
     public RandomAccessFileBuffer(File file,int capacity
         throws FileNotFoundException
     {
         super(,true);
         assert capacity>=file.length();
         assert file.length()<=.;
         =capacity;
          = new RandomAccessFile(file,"rw");
         =.getChannel();
         setGetIndex(0);
         setPutIndex((int)file.length());
     }
     
     public RandomAccessFileBuffer(File file,int capacity,int access
         throws FileNotFoundException
     {
         super(access,true);
         assert capacity>=file.length();
         assert file.length()<=.;
         =capacity;
          = new RandomAccessFile(file,access==?"rw":"r");
         =.getChannel();
         setGetIndex(0);
         setPutIndex((int)file.length());
     }
 
     public byte[] array()
     {
         return null;
     }
 
     public int capacity()
     {
         return ;
     }
 
     @Override
     public void clear()
     {
         try
         {
             synchronized ()
             {
                 super.clear();
                 .setLength(0);
             }
         }
         catch(Exception e)
         {
             throw new RuntimeException(e);
         }
    }
    @Override
    public byte peek()
    {
        synchronized ()
        {
            try
            {
                if (!=.getFilePointer())
                    .seek();
                return .readByte();
            }
            catch(Exception e)
            {
                throw new RuntimeException(e);
            }
        }
    }
    public byte peek(int index)
    {
        synchronized ()
        {
            try
            {
                .seek(index);
                return .readByte();
            }
            catch(Exception e)
            {
                throw new RuntimeException(e);
            }
        }
    }
    public int peek(int indexbyte[] bint offsetint length)
    {
        synchronized ()
        {
            try
            {
                .seek(index);
                return .read(b,offset,length);
            }
            catch(Exception e)
            {
                throw new RuntimeException(e);
            }
        }
    }
    public void poke(int indexbyte b)
    {
        synchronized ()
        {
            try
            {
                .seek(index);
                .writeByte(b);
            }
            catch(Exception e)
            {
                throw new RuntimeException(e);
            }
        }
    }
    @Override
    public int poke(int indexbyte[] bint offsetint length)
    {
        synchronized ()
        {
            try
            {
                .seek(index);
                .write(b,offset,length);
                return length;
            }
            catch(Exception e)
            {
                throw new RuntimeException(e);
            }
        }
    }
    
    public int writeTo(WritableByteChannel channel,int indexint length)
        throws IOException
    {
        synchronized ()
        {
            return (int).transferTo(index,length,channel);
        }
    }
    
New to GrepCode? Check out our FAQ X