java bytebuffer flip_java string转byte

java bytebuffer flip_java string转byteimportjava.nio.ByteBuffer;importjava.nio.ByteOrder;publicclassbytebuffertest{publicstaticvoidmain(String[]args){//CreateaByteBufferusingabytearraybyte[]bytes=newbyte[10];Byt

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

import java.nio.ByteBuffer;  
import java.nio.ByteOrder;  
public class bytebuffertest {  
  
    public static void main(String[] args)  
    {  
        // Create a ByteBuffer using a byte array   
        byte[] bytes = new byte[10];  
        ByteBuffer buf = ByteBuffer.wrap(bytes);  
  
        // Create a non-direct ByteBuffer with a 10 byte capacity   
        // The underlying storage is a byte array.   
        buf = ByteBuffer.allocate(10);  
  
        // Create a direct (memory-mapped) ByteBuffer with a 10 byte capacity.   
        buf = ByteBuffer.allocateDirect(10);  
  
          
        // Create an empty ByteBuffer with a 10 byte capacity   
        ByteBuffer bbuf = ByteBuffer.allocate(10);  
  
        // Get the ByteBuffer's capacity   
        int capacity = bbuf.capacity(); // 10   
        System.out.println(capacity);  
  
        // Use the absolute get().   
        // This method does not affect the position.   
        byte b = bbuf.get(5); // position=0   
  
        // Set the position   
        bbuf.position(5);  
  
        // Use the relative get()   
        b = bbuf.get();  
  
        // Get the new position   
        int pos = bbuf.position(); // 6   
  
        // Get remaining byte count   
        int rem = bbuf.remaining(); // 4   
  
        // Set the limit   
        bbuf.limit(7); // remaining=1   
  
        // This convenience method sets the position to 0   
        bbuf.rewind(); // remaining=7   
          
        //Converting Between a ByteBuffer an a Byte Array    
  
        // Create a ByteBuffer from a byte array   
        byte[] bytes1 = new byte[10];  
        ByteBuffer buf1 = ByteBuffer.wrap(bytes);  
  
        // Retrieve bytes between the position and limit   
        // (see Putting Bytes into a ByteBuffer)   
        bytes1 = new byte[buf.remaining()];  
        buf1.get(bytes1, 0, bytes1.length);  
  
        // Retrieve all bytes in the buffer   
        buf1.clear();  
        bytes1 = new byte[buf1.capacity()];  
        buf1.get(bytes1, 0, bytes1.length);  
          
        // Use the absolute put().   
        // This method does not affect the position.   
        bbuf.put((byte)0xFF); // position=0   
  
        // Set the position   
        bbuf.position(5);  
  
        // Use the relative put()   
        bbuf.put((byte)0xFF);  
  
        // Get the new position   
        int pos1 = bbuf.position(); // 6   
          
          
        //Setting the Byte Ordering for a ByteBuffer    
        // Get default byte ordering   
        ByteOrder order = buf.order(); // ByteOrder.BIG_ENDIAN   
  
        // Put a multibyte value   
        buf.putShort(0, (short)123);  
        buf.get(0); // 0   
        buf.get(1); // 123   
  
        // Set to little endian   
        buf.order(ByteOrder.LITTLE_ENDIAN);  
  
        // Put a multibyte value   
        buf.putShort(0, (short)123);  
        buf.get(0); // 123   
        buf.get(1); // 0   
  
  
  
    }  
} 

这是一篇转载的博客我还没有来得及进行验证,放在这里只是为了以后自己方便查看,如有错误的地方还请以评论的方式告知,我会进行改正。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/186742.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • 【spring框架】spring的几个Annotation实现(下)

    【spring框架】spring的几个Annotation实现(下)

    2021年9月4日
    83
  • ASSERT_VALID宏[通俗易懂]

    ASSERT_VALID宏[通俗易懂]ASSERT_VALID()验证指针是否指向空值//AssurethatpMyObjectisavalidpointertoan//objectderivedfromCObject.ASSERT_VALID(pMyObject);SeeAlso   ASSERT,VERIFY

    2025年9月18日
    5
  • 适配器的作用[通俗易懂]

    适配器的作用[通俗易懂]首先我们从一般的概念上讨论一下计算机是怎样连接到局域网上的。计算机与外界局域网的连接是通过通信适配器(adapter)。适配器本来是在主机箱内插入的一块网络接口板(或者是在笔记本电脑中插入一块PCMCIA卡)。这种接口板又称为网络接口NIC(NetworkInterfaceCard)或简称为“网卡”。由于较新的计算机主板上已经嵌入了这种适配器,不使用单独的网卡了,因此…

    2022年6月12日
    35
  • 史上最简单的 SpringCloud 教程 | 第一篇: 服务的注册与发现Eureka(Finchley版本)

    史上最简单的 SpringCloud 教程 | 第一篇: 服务的注册与发现Eureka(Finchley版本)转载请标明出处:http://blog.csdn.net/forezp/article/details/69696915本文出自方志朋的博客一、springcloud简介鉴于《史上最简单的SpringCloud教程》很受读者欢迎,再次我特意升级了一下版本,目前支持的版本为SpringBoot版本2.0.3.RELEASE,SpringCloud版本为F…

    2022年6月9日
    36
  • Java线程同步:synchronized锁住的是代码还是对象

    Java线程同步:synchronized锁住的是代码还是对象在Java中,synchronized关键字是用来控制线程同步的,就是在多线程的环境下,控制synchronized代码段不被多个线程同时执行。Synchronized既可以对代码块使用,也可以加在整个方法上。 关键是,不要认为给方法或者代码段加上synchronized就万事大吉,看下面一段代码:classSync{ publicsynchronizedvoidtest

    2022年7月15日
    18
  • Spring 学习——基于Spring WebSocket 和STOMP实现简单的聊天功能

    本篇主要讲解如何使用Spring websocket 和STOMP搭建一个简单的聊天功能项目,里面使用到的技术,如websocket和STOMP等会简单介绍,不会太深,如果对相关介绍不是很了解的,请自行查阅相关知识。 本篇的项目主要是一个学习Spring websocket和STOMP的项目,基于Spring4.0之上。因为Spring4.0之上才支持Websocket。例子比较的简单,但是总体实

    2022年2月26日
    53

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号