1 /*
2 * Copyright (c) 2014, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation. Oracle designates this
8 * particular file as subject to the "Classpath" exception as provided
9 * by Oracle in the LICENSE file that accompanied this code.
10 *
11 * This code is distributed in the hope that it will be useful, but WITHOUT
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14 * version 2 for more details (a copy is included in the LICENSE file that
15 * accompanied this code).
16 *
17 * You should have received a copy of the GNU General Public License version
18 * 2 along with this work; if not, write to the Free Software Foundation,
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 *
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22 * or visit www.oracle.com if you need additional information or have any
23 * questions.
24 */
25 package jdk.nashorn.internal.runtime.arrays;
26
27 import static jdk.nashorn.internal.runtime.ECMAErrors.typeError;
28
29 import java.nio.ByteBuffer;
30 import jdk.nashorn.internal.objects.Global;
31 import jdk.nashorn.internal.runtime.PropertyDescriptor;
32 import jdk.nashorn.internal.runtime.ScriptRuntime;
33
34 /**
35 * Implementation of {@link ArrayData} that wraps a nio ByteBuffer
36 */
37 final class ByteBufferArrayData extends ArrayData {
38 private final ByteBuffer buf;
39
40 ByteBufferArrayData(final int length) {
41 super(length);
42 this.buf = ByteBuffer.allocateDirect(length);
43 }
44
45 /**
46 * Constructor
47 *
48 * @param buf ByteBuffer to create array data with.
49 */
50 ByteBufferArrayData(final ByteBuffer buf) {
51 super(buf.capacity());
52 this.buf = buf;
53 }
54
55 /**
56 * Returns property descriptor for element at a given index
57 *
58 * @param global the global object
59 * @param index the index
60 *
61 * @return property descriptor for element
62 */
63 @Override
64 public PropertyDescriptor getDescriptor(final Global global, final int index) {
65 // make the index properties not configurable
66 return global.newDataDescriptor(getObject(index), false, true, true);
67 }
68
69 @Override
70 public ArrayData copy() {
71 throw unsupported("copy");
72 }
73
74 @Override
75 public Object[] asObjectArray() {
76 throw unsupported("asObjectArray");
77 }
78
79 @Override
80 public void setLength(final long length) {
81 throw new UnsupportedOperationException("setLength");
82 }
83
84 @Override
85 public void shiftLeft(final int by) {
86 throw unsupported("shiftLeft");
87 }
88
89 @Override
90 public ArrayData shiftRight(final int by) {
91 throw unsupported("shiftRight");
92 }
93
94 @Override
95 public ArrayData ensure(final long safeIndex) {
96 if (safeIndex < buf.capacity()) {
97 return this;
98 }
99
100 throw unsupported("ensure");
101 }
102
103 @Override
104 public ArrayData shrink(final long newLength) {
105 throw unsupported("shrink");
106 }
107
108 @Override
109 public ArrayData set(final int index, final Object value, final boolean strict) {
110 if (value instanceof Number) {
111 buf.put(index, ((Number)value).byteValue());
112 return this;
113 }
114
115 throw typeError("not.a.number", ScriptRuntime.safeToString(value));
116 }
117
118 @Override
119 public ArrayData set(final int index, final int value, final boolean strict) {
120 buf.put(index, (byte)value);
121 return this;
122 }
123
124 @Override
125 public ArrayData set(final int index, final double value, final boolean strict) {
126 buf.put(index, (byte)value);
127 return this;
128 }
129
130 @Override
131 public int getInt(final int index) {
132 return 0x0ff & buf.get(index);
133 }
134
135 @Override
136 public double getDouble(final int index) {
137 return 0x0ff & buf.get(index);
138 }
139
140 @Override
141 public Object getObject(final int index) {
142 return 0x0ff & buf.get(index);
143 }
144
145 @Override
146 public boolean has(final int index) {
147 return index > -1 && index < buf.capacity();
148 }
149
150 @Override
151 public boolean canDelete(final int index, final boolean strict) {
152 return false;
153 }
154
155 @Override
156 public boolean canDelete(final long longIndex, final boolean strict) {
157 return false;
158 }
159
160 @Override
161 public ArrayData delete(final int index) {
162 throw unsupported("delete");
163 }
164
165 @Override
166 public ArrayData delete(final long fromIndex, final long toIndex) {
167 throw unsupported("delete");
168 }
169
170 @Override
171 public ArrayData push(final boolean strict, final Object... items) {
172 throw unsupported("push");
173 }
174
175 @Override
176 public Object pop() {
177 throw unsupported("pop");
178 }
179
180 @Override
181 public ArrayData slice(final long from, final long to) {
182 throw unsupported("slice");
183 }
184
185 @Override
186 public ArrayData convert(final Class<?> type) {
187 throw unsupported("convert");
188 }
189
190 private static UnsupportedOperationException unsupported(final String method) {
191 return new UnsupportedOperationException(method);
192 }
193 }
--- EOF ---