I/O

(一)构造缓冲器

各种缓冲器均没有构造函数,生成是通过缓冲器的静态方法来实现的。缓冲器一旦生成,容量不可改变。共有两种构造方法,以ByteBuffer为例:

法1:ByteBuffer byteBuffer = ByteBuffer.allocate(256);

allocate函数生成一个数组,并将该数组打包进缓冲器。

法2:Byte byteArray[] = new byte[1024];

ByteBuffer byteBuffer = byteBuffer.wrap(bytearray);

如果已经存在一个byte数组byteArray,可以用wrap()函数生成缓冲器。须注意,byteBuffer的读写操作可以直接修改存在于byteArray中的数据。


Buffer的状态参数管理方法

position():取出现在的位置

position(int index):将index设为当前位置

limit()取出现在的限度

limit(int newLimit):将newLimit设为当前限度

clear():position设为0,limit设为capacity,取消所有mark

rewind():position设为0,取消所有mark

flip():limit设为当前position,position设为0


例1:

public class SimpleByteBufferDemo {
public SimpleByteBufferDemo() {
byte bytes[] = {(byte)0,(byte)255,(byte)0,(byte)0};

ByteBuffer byteBuffer = ByteBuffer.allocate(4);
for (int i = 0; i < bytes.length; i++) {
byteBuffer.put(bytes[i]);
}
//重设ByteBuffer的状态参数
byteBuffer.flip();
//取出整数,生成Color对象
Color color = new Color(byteBuffer.getInt(),true);
System.out.println(color);
System.out.println(Color.red);
}

public static void main(String[] args) {
new SimpleByteBufferDemo();
}
}


例2:

public class SimpleByteBufferDemo2 {
public SimpleByteBufferDemo2() {
ByteBuffer byteBuffer = ByteBuffer.allocate(8);
byteBuffer.put((byte)1);
byteBuffer.put((byte)2);
byteBuffer.put((byte)3);
System.out.println(byteBuffer.get());//此时position为3,内容为空
}

public static void main(String[] args) {
new SimpleByteBufferDemo2();
}
}


例3:

public class SimpleByteBufferDemo3 {
public void demoBulkGet(){
IntBuffer intBuffer = IntBuffer.allocate(10);
for (int i = 0; i < intBuffer.capacity(); i++) {
intBuffer.put(3*(i+1));
}
intBuffer.flip();

int[] cloned = new int[10];
intBuffer.get(cloned);//将缓冲器中所有数据复制到整数数组cloned中
StringBuffer buf = new StringBuffer();
for (int i = 0; i < cloned.length; i++) {
buf.append(cloned[i]).append(",");
}

System.out.println(buf.substring(0, buf.lastIndexOf(",")));
}

public static void main(String[] args) {
new SimpleByteBufferDemo3().demoBulkGet();
}
}


(二)Charset

Java从开始一直采用2byte处理字符,并且与Unicode兼容。但大多数操作系统并不兼容Unicode。Charset提供一种在byte数值和Unicode间相互映射的方法。


(三)编码器与解码器

/**
 * 编码的工作是将CharBuffer的内容转换成ByteBuffer.
 * 解码的工作是将ByteBuffer的内容转换写进CharBuffer.
 * 
 */
public class HelloNIO {
Charset charset = null;
public HelloNIO() {
//构造Charset对象
charset = Charset.forName("ISO-8859-1");
}

public void sayHelloNIO() throws CharacterCodingException{
//构造解码器
CharsetDecoder decoder = charset.newDecoder();
ByteBuffer byteBuffer = getBytesOfTest("Hello New I/O");
//解码
CharBuffer charBuffer = decoder.decode(byteBuffer);
StringBuffer result = new StringBuffer();
for (int i = 0; i < charBuffer.capacity(); i++) {
result.append(charBuffer.get(i));
}
System.out.println(result.toString());
}


public ByteBuffer getBytesOfTest(String string) throws CharacterCodingException {
//构造编码器
CharBuffer charBuffer = CharBuffer.wrap(string);
CharsetEncoder encoder = charset.newEncoder();
return encoder.encode(charBuffer);//返回编码
}

public static void main(String[] args) throws CharacterCodingException {
new HelloNIO().sayHelloNIO();
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值