Save This Page
Home » openjdk-7 » java » nio » [javadoc | source]
    1   /* MappedByteBufferImpl.java -- 
    2      Copyright (C) 2002, 2003, 2004 Free Software Foundation, Inc.
    3   
    4   This file is part of GNU Classpath.
    5   
    6   GNU Classpath is free software; you can redistribute it and/or modify
    7   it under the terms of the GNU General Public License as published by
    8   the Free Software Foundation; either version 2, or (at your option)
    9   any later version.
   10   
   11   GNU Classpath is distributed in the hope that it will be useful, but
   12   WITHOUT ANY WARRANTY; without even the implied warranty of
   13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   14   General Public License for more details.
   15   
   16   You should have received a copy of the GNU General Public License
   17   along with GNU Classpath; see the file COPYING.  If not, write to the
   18   Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   19   02110-1301 USA.
   20   
   21   Linking this library statically or dynamically with other modules is
   22   making a combined work based on this library.  Thus, the terms and
   23   conditions of the GNU General Public License cover the whole
   24   combination.
   25   
   26   As a special exception, the copyright holders of this library give you
   27   permission to link this library with independent modules to produce an
   28   executable, regardless of the license terms of these independent
   29   modules, and to copy and distribute the resulting executable under
   30   terms of your choice, provided that you also meet, for each linked
   31   independent module, the terms and conditions of the license of that
   32   module.  An independent module is a module which is not derived from
   33   or based on this library.  If you modify this library, you may extend
   34   this exception to your version of the library, but you are not
   35   obligated to do so.  If you do not wish to do so, delete this
   36   exception statement from your version. */
   37   
   38   
   39   package java.nio;
   40   
   41   import gnu.classpath.Pointer;
   42   
   43   import java.io.IOException;
   44   
   45   final class MappedByteBufferImpl extends MappedByteBuffer
   46   {
   47     boolean readOnly;
   48   
   49     /** Posix uses this for the pointer returned by mmap;
   50      * Win32 uses it for the pointer returned by MapViewOfFile. */
   51     public Pointer implPtr;
   52     /** Posix uses this for the actual length passed to mmap;
   53      * Win32 uses it for the pointer returned by CreateFileMapping. */
   54     public long implLen;
   55     
   56     public MappedByteBufferImpl(Pointer address, int size, boolean readOnly)
   57       throws IOException
   58     {
   59       super(size, size, 0, -1);
   60       this.address = address;
   61       this.readOnly = readOnly;
   62     }
   63   
   64     public boolean isReadOnly()
   65     {
   66       return readOnly;
   67     }
   68     
   69     public byte get()
   70     {
   71       checkForUnderflow();
   72   
   73       int pos = position();
   74       byte result = VMDirectByteBuffer.get(address, pos);
   75       position(pos + 1);
   76       return result;
   77     }
   78   
   79     public ByteBuffer put(byte value)
   80     {
   81       checkIfReadOnly();
   82       checkForOverflow();
   83   
   84       int pos = position();
   85       VMDirectByteBuffer.put(address, pos, value);
   86       position(pos + 1);
   87       return this;
   88     }
   89   
   90     public byte get(int index)
   91     {
   92       checkIndex(index);
   93   
   94       return VMDirectByteBuffer.get(address, index);
   95     }
   96   
   97     public ByteBuffer get(byte[] dst, int offset, int length)
   98     {
   99       checkArraySize(dst.length, offset, length);
  100       checkForUnderflow(length);
  101   
  102       int index = position();
  103       VMDirectByteBuffer.get(address, index, dst, offset, length);
  104       position(index+length);
  105   
  106       return this;
  107     }
  108   
  109     public ByteBuffer put(int index, byte value)
  110     {
  111       checkIfReadOnly();
  112       checkIndex(index);
  113   
  114       VMDirectByteBuffer.put(address, index, value);
  115       return this;
  116     }
  117   
  118     public ByteBuffer compact()
  119     {
  120       checkIfReadOnly();
  121       mark = -1;
  122       int pos = position();
  123       if (pos > 0)
  124         {
  125   	int count = remaining();
  126   	// Call shiftDown method optimized for direct buffers.
  127   	VMDirectByteBuffer.shiftDown(address, 0, pos, count);
  128   	position(count);
  129   	limit(capacity());
  130         }
  131       else
  132         {
  133   	position(limit());
  134   	limit(capacity());
  135         }
  136       return this;
  137     }
  138   
  139     public boolean isDirect()
  140     {
  141       return true;
  142     }
  143   
  144     public ByteBuffer slice()
  145     {
  146       int rem = remaining();
  147       if (isReadOnly())
  148           return new DirectByteBufferImpl.ReadOnly
  149         (this, VMDirectByteBuffer.adjustAddress(address, position()),
  150          rem, rem, 0);
  151       else
  152           return new DirectByteBufferImpl.ReadWrite
  153         (this, VMDirectByteBuffer.adjustAddress(address, position()),
  154          rem, rem, 0);
  155     }
  156   
  157     private ByteBuffer duplicate(boolean readOnly)
  158     {
  159       int pos = position();
  160       reset();
  161       int mark = position();
  162       position(pos);
  163       DirectByteBufferImpl result;
  164       if (readOnly)
  165           result = new DirectByteBufferImpl.ReadOnly(this, address, capacity(),
  166                                                      limit(), pos);
  167       else
  168           result = new DirectByteBufferImpl.ReadWrite(this, address, capacity(),
  169                                                       limit(), pos);
  170   
  171       if (mark != pos)
  172         {
  173   	result.position(mark);
  174   	result.mark();
  175   	result.position(pos);
  176         }
  177       return result;
  178     }
  179   
  180     public ByteBuffer duplicate()
  181     {
  182       return duplicate(isReadOnly());
  183     }
  184   
  185     public ByteBuffer asReadOnlyBuffer()
  186     {
  187       return duplicate(true);
  188     }
  189   
  190     public CharBuffer asCharBuffer()
  191     {
  192       return new CharViewBufferImpl(this, remaining() >> 1);
  193     }
  194   
  195     public ShortBuffer asShortBuffer()
  196     {
  197       return new ShortViewBufferImpl(this, remaining() >> 1);
  198     }
  199   
  200     public IntBuffer asIntBuffer()
  201     {
  202       return new IntViewBufferImpl(this, remaining() >> 2);
  203     }
  204   
  205     public LongBuffer asLongBuffer()
  206     {
  207       return new LongViewBufferImpl(this, remaining() >> 3);
  208     }
  209   
  210     public FloatBuffer asFloatBuffer()
  211     {
  212       return new FloatViewBufferImpl(this, remaining() >> 2);
  213     }
  214   
  215     public DoubleBuffer asDoubleBuffer()
  216     {
  217       return new DoubleViewBufferImpl(this, remaining() >> 3);
  218     }
  219   
  220     public char getChar()
  221     {
  222       return ByteBufferHelper.getChar(this, order());
  223     }
  224     
  225     public ByteBuffer putChar(char value)
  226     {
  227       ByteBufferHelper.putChar(this, value, order());
  228       return this;
  229     }
  230     
  231     public char getChar(int index)
  232     {
  233       return ByteBufferHelper.getChar(this, index, order());
  234     }
  235     
  236     public ByteBuffer putChar(int index, char value)
  237     {
  238       ByteBufferHelper.putChar(this, index, value, order());
  239       return this;
  240     }
  241   
  242     public short getShort()
  243     {
  244       return ByteBufferHelper.getShort(this, order());
  245     }
  246     
  247     public ByteBuffer putShort(short value)
  248     {
  249       ByteBufferHelper.putShort(this, value, order());
  250       return this;
  251     }
  252     
  253     public short getShort(int index)
  254     {
  255       return ByteBufferHelper.getShort(this, index, order());
  256     }
  257     
  258     public ByteBuffer putShort(int index, short value)
  259     {
  260       ByteBufferHelper.putShort(this, index, value, order());
  261       return this;
  262     }
  263   
  264     public int getInt()
  265     {
  266       return ByteBufferHelper.getInt(this, order());
  267     }
  268     
  269     public ByteBuffer putInt(int value)
  270     {
  271       ByteBufferHelper.putInt(this, value, order());
  272       return this;
  273     }
  274     
  275     public int getInt(int index)
  276     {
  277       return ByteBufferHelper.getInt(this, index, order());
  278     }
  279     
  280     public ByteBuffer putInt(int index, int value)
  281     {
  282       ByteBufferHelper.putInt(this, index, value, order());
  283       return this;
  284     }
  285   
  286     public long getLong()
  287     {
  288       return ByteBufferHelper.getLong(this, order());
  289     }
  290     
  291     public ByteBuffer putLong(long value)
  292     {
  293       ByteBufferHelper.putLong(this, value, order());
  294       return this;
  295     }
  296     
  297     public long getLong(int index)
  298     {
  299       return ByteBufferHelper.getLong(this, index, order());
  300     }
  301     
  302     public ByteBuffer putLong(int index, long value)
  303     {
  304       ByteBufferHelper.putLong(this, index, value, order());
  305       return this;
  306     }
  307   
  308     public float getFloat()
  309     {
  310       return ByteBufferHelper.getFloat(this, order());
  311     }
  312     
  313     public ByteBuffer putFloat(float value)
  314     {
  315       ByteBufferHelper.putFloat(this, value, order());
  316       return this;
  317     }
  318     
  319     public float getFloat(int index)
  320     {
  321       return ByteBufferHelper.getFloat(this, index, order());
  322     }
  323   
  324     public ByteBuffer putFloat(int index, float value)
  325     {
  326       ByteBufferHelper.putFloat(this, index, value, order());
  327       return this;
  328     }
  329   
  330     public double getDouble()
  331     {
  332       return ByteBufferHelper.getDouble(this, order());
  333     }
  334   
  335     public ByteBuffer putDouble(double value)
  336     {
  337       ByteBufferHelper.putDouble(this, value, order());
  338       return this;
  339     }
  340     
  341     public double getDouble(int index)
  342     {
  343       return ByteBufferHelper.getDouble(this, index, order());
  344     }
  345     
  346     public ByteBuffer putDouble(int index, double value)
  347     {
  348       ByteBufferHelper.putDouble(this, index, value, order());
  349       return this;
  350     }
  351   
  352     // NOTE: In libgcj these methods are implemented in natFileChannelXxx.cc,
  353     // because they're small, and to put them next to FileChannelImpl::mapImpl.
  354     native void unmapImpl();
  355     native boolean isLoadedImpl();
  356       // FIXME: Try to load all pages into memory.
  357     native void loadImpl();
  358   
  359     native void forceImpl();
  360   }

Save This Page
Home » openjdk-7 » java » nio » [javadoc | source]