---
title: "从ByteBuffer中获取基本类型"
author: "latty"
date: "2012-03-04T14:03:42Z"
tags: ["bytebuffer"]
categories: ["编程语言"]
slug: "bytebuffer"
draft: false
lastmod: "2012-03-04T14:03:41Z"
id: 1439475
url: "https://latty.iteye.com/blog/1439475"
---

我们可以从ByteBuffer中产生出不同类型值的方法
  
```java
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();
         				      
                               
	}

}
```