Java Nio 二十二

2018/01/31 Java

Java NIO 二十二

java.nio 包

java.nio 包包含在 java.nio.channels 和 java.nio.charset 分包中的类使用的 Buffer 类。

Buffer

Buffer 是所有其他缓冲类延展出来的基类。它包含所有缓冲器类型都适用的通用方法。

public abstract class Buffer
{
public final int capacity()
public final Buffer clear()
public final Buffer flip()
public final boolean hasRemaining() public abstract boolean isReadOnly(); public final int limit()
public final Buffer limit (int newLimit) public final Buffer mark()
public final int position()
public final Buffer position (int newPosition) public final int remaining()
public final Buffer reset()
public final Buffer rewind() }

BufferOverflowException

当一个简单相关 put()试图用缓冲器的位置相等于它的极限时,或当批量 put()引起位置超出限度时抛出 BufferOverflowException(未检查)。

public class BufferOverflowException extends RuntimeException
{
        public BufferOverflowException()
}

BufferUnderflowException

当一个简单相关 get()试图用缓冲器的位置相等于它的极限时,或当批量 get()引起位置超出限 度时抛出 BufferUnderflowException(未检查)。

public class BufferUnderflowException
              extends RuntimeException
{
        Public BufferUnderflowException()
}

ByteBuffer

ByteBuffer 是所有缓冲器类中最复杂也最万能的类。只有字节缓冲器可以在通道上参与到 I/O 操作中,发送与接收数据。

public abstract class ByteBuffer extends Buffer
              implements Comparable
{
        public BufferOverflowException()
        Public BufferUnderflowException()
        public static ByteBuffer allocate (int capacity)
        public static ByteBuffer allocateDirect (int capacity) public final byte []array()
        public final int arrayOffset()
        public abstract CharBuffer asCharBuffer();
        public abstract DoubleBuffer asDoubleBuffer();
        public abstract FloatBuffer asFloatBuffer();
        public abstract IntBuffer asIntBuffer();
        public abstract LongBuffer asLongBuffer();
        public abstract ByteBuffer asReadOnlyBuffer();
        public abstract ShortBuffer asShortBuffer();
        public abstract ByteBuffer compact();
        public int compareTo (Object ob)
        public abstract ByteBuffer duplicate();
        public boolean equals (Object ob)
        public abstract byte get();
        public ByteBuffer get (byte [] dst)
        public abstract byte get (int index);
        public ByteBuffer get (byte [] dst, int offset, int length) public abstract char getChar();
        public abstract char getChar (int index);
        public abstract double getDouble();
        public abstract double getDouble (int index);
        public abstract float getFloat();
        public abstract float getFloat (int index);
        public abstract int getInt();
        public abstract int getInt (int index);
        public abstract long getLong();
        public abstract long getLong (int index);
        public abstract short getShort();
        public abstract short getShort (int index);
        public final boolean hasArray()
        public int hashCode()
        public abstract boolean isDirect();
        public final ByteOrder order()
        public final ByteBuffer order (ByteOrder bo)
        public abstract ByteBuffer put (byte b);
        public final ByteBuffer put (byte [] src)
        public ByteBuffer put (ByteBuffer src)
        public abstract ByteBuffer put (int index, byte b);
        public ByteBuffer put (byte [] src, int offset, int length)
        public abstract ByteBuffer putChar (char value);
        public abstract ByteBuffer putChar (int index, char value);
        public abstract ByteBuffer putDouble (double value);
        public abstract ByteBuffer putDouble (int index, double value);         public abstract ByteBuffer putFloat (float value);
        public abstract ByteBuffer putFloat (int index, float value);
        public abstract ByteBuffer putInt (int value);
        public abstract ByteBuffer putInt (int index, int value);
        public abstract ByteBuffer putLong (long value);
        public abstract ByteBuffer putLong (int index, long value);
        public abstract ByteBuffer putShort (short value);
        public abstract ByteBuffer putShort (int index, short value);
        public abstract ByteBuffer slice();
        public String toString()
        public static ByteBuffer wrap (byte [] array)
        public static ByteBuffer wrap (byte [] array, int offset, int length)
}

ByteOrder

ByteOrder 是安全类型的列举,它不能被直接实例化。ByteOrder 的两个公开可访问实例 被视为静态类域。提供一个类方法用来确定基础操作系统的本地字节顺序,可以跟 Java 平台默认 的不同。

public final class ByteOrder
{
        public static final ByteOrder BIG_ENDIAN 
        public static final ByteOrder LITTLE_ENDIAN 
        public static ByteOrder nativeOrder() 
        public String toString()
}

CharBuffer

CharBuffer 管理类型 char 的数据元素并实现 CharSequence 接口,允许其参与到字符为 导向的操作中,例如正规表达式匹配。在 java.nio.charset 包中类也使用 CharBuffer。


public abstract class CharBuffer extends Buffer
              implements Comparable, CharSequence
{
        public static CharBuffer allocate (int capacity) 
        public final char []array()
        public final int arrayOffset()

        public abstract CharBuffer asReadOnlyBuffer(); 
        public final char charAt (int index)
        public abstract CharBuffer compact();
        public int compareTo (Object ob)
        public abstract CharBuffer duplicate();
        public boolean equals (Object ob)
        public abstract char get();
        public CharBuffer get(char [] dst)
        public abstract char get (int index);
        public CharBuffer get (char [] dst, int offset, int length) public final boolean hasArray()
        public int hashCode()
        public abstract boolean isDirect();
        public final int length()
        public abstract ByteOrder order();
        public abstract CharBuffer put (char c);
        public final CharBuffer put (char [] src)
        public final CharBuffer put (String src)
        public CharBuffer put (CharBuffer src)
        public abstract CharBuffer put (int index, char c);
        public CharBuffer put (char [] src, int offset, int length)
        public CharBuffer put (String src, int start, int end)
        public abstract CharBuffer slice();
        public abstract CharSequence subSequence (int start, int end);  
        public String toString()
        public static CharBuffer wrap (char [] array)
        public static CharBuffer wrap (CharSequence csq)
        public static CharBuffer wrap (char [] array, int offset, int length) 
        public static CharBuffer wrap (CharSequence csq, int start, int end)

DoubleBuffer

public abstract class DoubleBuffer extends Buffer
              implements Comparable
{
public static DoubleBuffer allocate (int capacity) public final double [] array()
public final int arrayOffset()
public abstract DoubleBuffer asReadOnlyBuffer(); public abstract DoubleBuffer compact();
public int compareTo (Object ob)
public abstract DoubleBuffer duplicate();
public boolean equals (Object ob)
public abstract double get();
public DoubleBuffer get (double [] dst)
public abstract double get (int index);
public DoubleBuffer get (double [] dst, int offset, int length) public final boolean hasArray()
public int hashCode()
public abstract boolean isDirect();
public abstract ByteOrder order();
public abstract DoubleBuffer put (double d);
public final DoubleBuffer put (double [] src)
public DoubleBuffer put (DoubleBuffer src)
public abstract DoubleBuffer put (int index, double d);
public DoubleBuffer put (double [] src, int offset, int length) public abstract DoubleBuffer slice();
public String toString()
public static DoubleBuffer wrap (double [] array)
public static DoubleBuffer wrap (double [] array, int offset,
int length)

}

FloatBuffer

public abstract class FloatBuffer extends Buffer
              implements Comparable
{
public static FloatBuffer allocate (int capacity) public final float [] array()
public final int arrayOffset()
public abstract FloatBuffer asReadOnlyBuffer(); public abstract FloatBuffer compact();
public int compareTo (Object ob)
public abstract FloatBuffer duplicate();
public boolean equals (Object ob)
public abstract float get();
public FloatBuffer get (float [] dst)
public abstract float get (int index);
public FloatBuffer get (float [] dst, int offset, int length) public final boolean hasArray()
public int hashCode()
public abstract boolean isDirect();
public abstract ByteOrder order();
public abstract FloatBuffer put (float f);
public final FloatBuffer put (float [] src)
public FloatBuffer put (FloatBuffer src)
public abstract FloatBuffer put (int index, float f);
public FloatBuffer put (float [] src, int offset, int length) public abstract FloatBuffer slice();
public String toString()
public static FloatBuffer wrap (float [] array)
public static FloatBuffer wrap (float [] array, int offset, int
length) 
}

IntBuffer

public abstract class IntBuffer extends Buffer
              implements Comparable
{
public static IntBuffer allocate (int capacity) public final int [] array()
public final int arrayOffset()
public abstract IntBuffer asReadOnlyBuffer(); public abstract IntBuffer compact();
public int compareTo (Object ob)
public abstract IntBuffer duplicate();
public boolean equals (Object ob)
public abstract int get();
public abstract int get (int index);
public IntBuffer get (int [] dst)
public IntBuffer get (int [] dst, int offset, int length)
public final boolean hasArray()
public int hashCode()
public abstract boolean isDirect();
public abstract ByteOrder order();
public abstract IntBuffer put (int i);
public final IntBuffer put (int [] src)
public IntBuffer put (IntBuffer src)
public abstract IntBuffer put (int index, int i);
public IntBuffer put (int [] src, int offset, int length) public abstract IntBuffer slice();

public String toString()
public static IntBuffer wrap (int [] array)
public static IntBuffer wrap (int [] array, int offset, int length)
}


InvalidMarkException

public class InvalidMarkException
              extends IllegalStateException
{ 
        Public InvalidMarkException()
}


LongBuffer


public abstract class LongBuffer
              extends Buffer
              implements Comparable
{
        public static LongBuffer allocate (int capacity) public final long [] array()
public final int arrayOffset()
public abstract LongBuffer asReadOnlyBuffer(); public abstract LongBuffer compact();
public int compareTo (Object ob)
public abstract LongBuffer duplicate();
public boolean equals (Object ob)
public abstract long get();
public abstract long get (int index);
public LongBuffer get (long [] dst)
public LongBuffer get (long [] dst, int offset, int length) public final boolean hasArray()
public int hashCode()
public abstract boolean isDirect();
public abstract ByteOrder order();
public LongBuffer put (LongBuffer src)
public abstract LongBuffer put (long l);
public final LongBuffer put (long [] src)
public abstract LongBuffer put (int index, long l);
public LongBuffer put (long [] src, int offset, int length) public abstract LongBuffer slice();
public String toString()
public static LongBuffer wrap (long [] array)
public static LongBuffer wrap (long [] array, int offset, int length)
}

MappedByteBuffer

MappedByteBuffer 是 ByteBuffer 的特殊类型,它的数据元素是硬盘文件的内容。 MappedByteBuffer 对象只能通过调用 FileChannel 对象的 map()方法创建。


public abstract class MappedByteBuffer
              extends ByteBuffer
{
        public final MappedByteBuffer force() 
        public final boolean isLoaded() 
        public final MappedByteBuffer load()
}

ReadOnlyBufferException

当有方法将更改缓冲器内容时,例如 put()或 compact(),抛出 ReadOnlyBufferException (未检查),在只读缓冲器上调用。


public class ReadOnlyBufferException
              extends UnsupportedOperationException
{
        public ReadOnlyBufferException()
}


ShortBuffer

ShortBuffer 管理类型 short 的数据元素。


public abstract class ShortBuffer extends Buffer
              implements Comparable
{
        public static ShortBuffer allocate (int capacity) public final short [] array()
public final int arrayOffset()
public abstract ShortBuffer asReadOnlyBuffer(); public abstract ShortBuffer compact();
public int compareTo (Object ob)
public abstract ShortBuffer duplicate();
public boolean equals (Object ob)
public abstract short get();
public abstract short get (int index);
public ShortBuffer get (short [] dst)
public ShortBuffer get (short [] dst, int offset, int length)

public final boolean hasArray()
public int hashCode()
public abstract boolean isDirect();
public abstract ByteOrder order();
public ShortBuffer put (ShortBuffer src)
public abstract ShortBuffer put (short s);
public final ShortBuffer put (short [] src)
public abstract ShortBuffer put (int index, short s);
public ShortBuffer put (short [] src, int offset, int length) public abstract ShortBuffer slice();
public String toString()
public static ShortBuffer wrap (short [] array)
public static ShortBuffer wrap (short [] array, int offset, int
length)
}

java.nio.channels 包

AlreadyConnectedException

当在已连接的 SocketChannel 对象上调用 connect()时抛出 AlreadyConnectedException(未检查)。

public class AlreadyConnectedException
              extends IllegalStateException
{
        public AlreadyConnectedException()
}

AsynchronousCloseException

当线程被锁定在通道操作上时,例如 read()和 write(),抛出 AsynchronousCloseException(IOException 的子类),并且通道被另一个线程关闭。


public class AsynchronousCloseException
              extends ClosedChannelException
{
        public AsynchronousCloseException()
}

ByteChannel

ByteChannel 是一个空聚合接口。它把 ReadableByteChannel 和 WritableByteChannel 结合到一个单接口中,但是不定义任何新的方法。

public interface ByteChannel
extends ReadableByteChannel, WritableByteChannel 
{

}

CancelledKeyException

当试图使用已失效的 SelectionKey 对象时抛出 CancelledKeyException(未检查)。


public class CancelledKeyException
              extends IllegalStateException
{ 
        public CancelledKeyException()
}

Show Disqus Comments

Search

    Table of Contents