]>
Commit | Line | Data |
---|---|---|
1d09f67e TL |
1 | /* |
2 | * Licensed to the Apache Software Foundation (ASF) under one or more | |
3 | * contributor license agreements. See the NOTICE file distributed with | |
4 | * this work for additional information regarding copyright ownership. | |
5 | * The ASF licenses this file to You under the Apache License, Version 2.0 | |
6 | * (the "License"); you may not use this file except in compliance with | |
7 | * the License. You may obtain a copy of the License at | |
8 | * | |
9 | * http://www.apache.org/licenses/LICENSE-2.0 | |
10 | * | |
11 | * Unless required by applicable law or agreed to in writing, software | |
12 | * distributed under the License is distributed on an "AS IS" BASIS, | |
13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
14 | * See the License for the specific language governing permissions and | |
15 | * limitations under the License. | |
16 | */ | |
17 | ||
18 | package io.netty.buffer; | |
19 | ||
20 | ||
21 | import static org.junit.Assert.assertEquals; | |
22 | ||
23 | import java.io.ByteArrayInputStream; | |
24 | import java.io.ByteArrayOutputStream; | |
25 | import java.io.IOException; | |
26 | import java.nio.ByteOrder; | |
27 | import java.nio.charset.StandardCharsets; | |
28 | ||
29 | import org.junit.Test; | |
30 | ||
31 | public class TestUnsafeDirectLittleEndian { | |
32 | private static final boolean LITTLE_ENDIAN = ByteOrder.nativeOrder() == ByteOrder.LITTLE_ENDIAN; | |
33 | ||
34 | @Test | |
35 | public void testPrimitiveGetSet() { | |
36 | ByteBuf byteBuf = Unpooled.directBuffer(64); | |
37 | UnsafeDirectLittleEndian unsafeDirect = new UnsafeDirectLittleEndian(new LargeBuffer(byteBuf)); | |
38 | ||
39 | unsafeDirect.setByte(0, Byte.MAX_VALUE); | |
40 | unsafeDirect.setByte(1, -1); // 0xFF | |
41 | unsafeDirect.setShort(2, Short.MAX_VALUE); | |
42 | unsafeDirect.setShort(4, -2); // 0xFFFE | |
43 | unsafeDirect.setInt(8, Integer.MAX_VALUE); | |
44 | unsafeDirect.setInt(12, -66052); // 0xFFFE FDFC | |
45 | unsafeDirect.setLong(16, Long.MAX_VALUE); | |
46 | unsafeDirect.setLong(24, -4295098372L); // 0xFFFF FFFE FFFD FFFC | |
47 | unsafeDirect.setFloat(32, 1.23F); | |
48 | unsafeDirect.setFloat(36, -1.23F); | |
49 | unsafeDirect.setDouble(40, 1.234567D); | |
50 | unsafeDirect.setDouble(48, -1.234567D); | |
51 | ||
52 | assertEquals(Byte.MAX_VALUE, unsafeDirect.getByte(0)); | |
53 | assertEquals(-1, unsafeDirect.getByte(1)); | |
54 | assertEquals(Short.MAX_VALUE, unsafeDirect.getShort(2)); | |
55 | assertEquals(-2, unsafeDirect.getShort(4)); | |
56 | assertEquals((char) 65534, unsafeDirect.getChar(4)); | |
57 | assertEquals(Integer.MAX_VALUE, unsafeDirect.getInt(8)); | |
58 | assertEquals(-66052, unsafeDirect.getInt(12)); | |
59 | assertEquals(4294901244L, unsafeDirect.getUnsignedInt(12)); | |
60 | assertEquals(Long.MAX_VALUE, unsafeDirect.getLong(16)); | |
61 | assertEquals(-4295098372L, unsafeDirect.getLong(24)); | |
62 | assertEquals(1.23F, unsafeDirect.getFloat(32), 0.0); | |
63 | assertEquals(-1.23F, unsafeDirect.getFloat(36), 0.0); | |
64 | assertEquals(1.234567D, unsafeDirect.getDouble(40), 0.0); | |
65 | assertEquals(-1.234567D, unsafeDirect.getDouble(48), 0.0); | |
66 | ||
67 | byte[] inBytes = "1234567".getBytes(StandardCharsets.UTF_8); | |
68 | try (ByteArrayInputStream bais = new ByteArrayInputStream(inBytes); | |
69 | ByteArrayOutputStream baos = new ByteArrayOutputStream()) { | |
70 | assertEquals(5, unsafeDirect.setBytes(56, bais, 5)); | |
71 | unsafeDirect.getBytes(56, baos, 5); | |
72 | assertEquals("12345", new String(baos.toByteArray(), StandardCharsets.UTF_8)); | |
73 | } catch (IOException e) { | |
74 | e.printStackTrace(); | |
75 | } | |
76 | } | |
77 | } |