`

从ByteBuffer中获取基本类型

    博客分类:
  • NIO
阅读更多
我们可以从ByteBuffer中产生出不同类型值的方法
 
public class Test{
       public static final int BSIZE=1024;
       public static void main()
	{
            ByteBuffer bb=ByteBuffer.allocate(BSIZE);
            int i=0;
            while(i++<bb.limit())
	       if(bb.get()!=0)
                   System.out.print("nozero");
               System.out.println("i ="+i);
               bb.rewind();
               bb.asShortBuffer().put((short)471142);
               System.out.print(bb.getShort());
               bb.rewind();
               bb.asIntBuffer().put(9947142);
               System.out.print(bb.getInt());
               bb.rewind();
               bb.asLongBuffer().put(99471142);
               System.out.print(bb.getLong());
               bb.rewind();
               bb.asFloatBuffer().put(99471142);
               System.out.print(bb.getFloat());
               bb.rewind();
               bb.asDoubleBuffer().put(99471142);
               System.out.print(bb.getDouble());
               bb.rewind();
         				      
                               
	}

}
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics