|
| 1 | +package org.javacore.nio; |
| 2 | + |
| 3 | +import java.nio.ByteBuffer; |
| 4 | +import java.nio.ByteOrder; |
| 5 | +import java.util.Arrays; |
| 6 | + |
| 7 | +/* |
| 8 | + * Copyright [2015] [Jeff Lee] |
| 9 | + * |
| 10 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 11 | + * you may not use this file except in compliance with the License. |
| 12 | + * You may obtain a copy of the License at |
| 13 | + * |
| 14 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 15 | + * |
| 16 | + * Unless required by applicable law or agreed to in writing, software |
| 17 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 18 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 19 | + * See the License for the specific language governing permissions and |
| 20 | + * limitations under the License. |
| 21 | + */ |
| 22 | + |
| 23 | +/** |
| 24 | + * @author Jeff Lee |
| 25 | + * @since 2015-10-13 20:40:00 |
| 26 | + * ByteBuffer中字节存储次序 |
| 27 | + */ |
| 28 | +public class Endians { |
| 29 | + public static void main(String[] args) { |
| 30 | + // 创建12个字节的字节缓冲区 |
| 31 | + ByteBuffer bb = ByteBuffer.wrap(new byte[12]); |
| 32 | + // 存入字符串 |
| 33 | + bb.asCharBuffer().put("abdcef"); |
| 34 | + System.out.println(Arrays.toString(bb.array())); |
| 35 | + |
| 36 | + // 反转缓冲区 |
| 37 | + bb.rewind(); |
| 38 | + // 设置字节存储次序 |
| 39 | + bb.order(ByteOrder.BIG_ENDIAN); |
| 40 | + bb.asCharBuffer().put("abcdef"); |
| 41 | + System.out.println(Arrays.toString(bb.array())); |
| 42 | + |
| 43 | + // 反转缓冲区 |
| 44 | + bb.rewind(); |
| 45 | + // 设置字节存储次序 |
| 46 | + bb.order(ByteOrder.LITTLE_ENDIAN); |
| 47 | + bb.asCharBuffer().put("abcdef"); |
| 48 | + System.out.println(Arrays.toString(bb.array())); |
| 49 | + } |
| 50 | +} |
0 commit comments