Mercurial > hg > truffle
annotate truffle/com.oracle.truffle.api/src/com/oracle/truffle/api/nodes/serial/VariableLengthIntBuffer.java @ 22157:dc83cc1f94f2
Using fully qualified imports
author | Jaroslav Tulach <jaroslav.tulach@oracle.com> |
---|---|
date | Wed, 16 Sep 2015 11:33:22 +0200 |
parents | 9c8c0937da41 |
children |
rev | line source |
---|---|
12388
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
1 /* |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
2 * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
4 * |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
5 * This code is free software; you can redistribute it and/or modify it |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
6 * under the terms of the GNU General Public License version 2 only, as |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
7 * published by the Free Software Foundation. Oracle designates this |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
8 * particular file as subject to the "Classpath" exception as provided |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
9 * by Oracle in the LICENSE file that accompanied this code. |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
10 * |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
15 * accompanied this code). |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
16 * |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
17 * You should have received a copy of the GNU General Public License version |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
20 * |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
22 * or visit www.oracle.com if you need additional information or have any |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
23 * questions. |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
24 */ |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
25 package com.oracle.truffle.api.nodes.serial; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
26 |
22157
dc83cc1f94f2
Using fully qualified imports
Jaroslav Tulach <jaroslav.tulach@oracle.com>
parents:
21951
diff
changeset
|
27 import java.nio.ByteBuffer; |
12388
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
28 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
29 /** |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
30 * Experimental API. May change without notice. Simple variable length unsigned int buffer backed by |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
31 * a byte buffer. |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
32 */ |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
33 public class VariableLengthIntBuffer { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
34 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
35 public static final int NULL = -1; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
36 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
37 private ByteBuffer buffer; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
38 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
39 public VariableLengthIntBuffer(ByteBuffer buffer) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
40 this.buffer = buffer; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
41 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
42 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
43 public VariableLengthIntBuffer(byte[] array) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
44 buffer = ByteBuffer.wrap(array); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
45 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
46 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
47 /** |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
48 * Returns the backing byte buffer. |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
49 */ |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
50 public ByteBuffer getBuffer() { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
51 return buffer; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
52 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
53 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
54 public byte[] getBytes() { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
55 int pos = buffer.position(); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
56 byte[] bytes = new byte[buffer.position()]; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
57 buffer.rewind(); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
58 buffer.get(bytes); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
59 buffer.position(pos); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
60 return bytes; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
61 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
62 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
63 public int get() { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
64 byte peekByte = buffer.get(buffer.position()); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
65 if ((peekByte & 0x80) == 0) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
66 // single byte encoding with prefix 0 (range 127) |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
67 return buffer.get(); // no bit to be masked |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
68 } else { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
69 if (peekByte == (byte) 0xFF) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
70 buffer.get(); // skip one byte |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
71 return NULL; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
72 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
73 int result = buffer.getInt() & 0x7FFF_FFFF; // first bit masked out |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
74 assert (result & 0x4000_0000) == 0; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
75 return result; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
76 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
77 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
78 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
79 public void put(int i) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
80 ensureCapacity(); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
81 if (i == NULL) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
82 buffer.put((byte) 0xFF); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
83 } else if ((i & 0xFFFF_FF80) == 0) { // 7 bits data |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
84 buffer.put((byte) i); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
85 } else if ((i & 0xC000_0000) == 0) { // 32 bits data |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
86 buffer.putInt(i | 0x8000_0000); // append leading 1 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
87 } else { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
88 throw new IllegalArgumentException("Integer out of encodeable " + i); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
89 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
90 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
91 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
92 private void ensureCapacity() { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
93 if (buffer.position() + 4 > buffer.capacity()) { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
94 ByteBuffer newBuffer = ByteBuffer.allocate(buffer.capacity() * 2); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
95 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
96 int pos = buffer.position(); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
97 buffer.rewind(); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
98 newBuffer.put(buffer); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
99 newBuffer.position(pos); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
100 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
101 buffer = newBuffer; |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
102 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
103 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
104 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
105 public boolean hasRemaining() { |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
106 return buffer.hasRemaining(); |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
107 } |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
108 |
96c1d057a5ed
Truffle: Added experimental serialization API.
Christian Humer <christian.humer@gmail.com>
parents:
diff
changeset
|
109 } |