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(int by) { 86 throw unsupported("shiftLeft"); 87 } 88 89 @Override 90 public ArrayData shiftRight(int by) { 91 throw unsupported("shiftRight"); 92 } 93 94 @Override 95 public ArrayData ensure(long safeIndex) { 96 if (safeIndex < buf.capacity()) { 97 return this; 98 } 99 100 throw unsupported("ensure"); 101 } 102 103 @Override 104 public ArrayData shrink(long newLength) { 105 throw unsupported("shrink"); 106 } 107 108 @Override 109 public ArrayData set(int index, Object value, 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(int index, int value, boolean strict) { 120 buf.put(index, (byte)value); 121 return this; 122 } 123 124 @Override 125 public ArrayData set(int index, long value, boolean strict) { 126 buf.put(index, (byte)value); 127 return this; 128 } 129 130 @Override 131 public ArrayData set(int index, double value, boolean strict) { 132 buf.put(index, (byte)value); 133 return this; 134 } 135 136 @Override 137 public int getInt(int index) { 138 return 0x0ff & buf.get(index); 139 } 140 141 @Override 142 public long getLong(int index) { 143 return 0x0ff & buf.get(index); 144 } 145 146 @Override 147 public double getDouble(int index) { 148 return 0x0ff & buf.get(index); 149 } 150 151 @Override 152 public Object getObject(int index) { 153 return (int)(0x0ff & buf.get(index)); 154 } 155 156 @Override 157 public boolean has(int index) { 158 return index > -1 && index < buf.capacity(); 159 } 160 161 @Override 162 public boolean canDelete(final int index, final boolean strict) { 163 return false; 164 } 165 166 @Override 167 public boolean canDelete(final long fromIndex, final long toIndex, final boolean strict) { 168 return false; 169 } 170 171 @Override 172 public ArrayData delete(int index) { 173 throw unsupported("delete"); 174 } 175 176 @Override 177 public ArrayData delete(long fromIndex, long toIndex) { 178 throw unsupported("delete"); 179 } 180 181 @Override 182 public ArrayData push(final boolean strict, final Object... items) { 183 throw unsupported("push"); 184 } 185 186 @Override 187 public Object pop() { 188 throw unsupported("pop"); 189 } 190 191 @Override 192 public ArrayData slice(long from, long to) { 193 throw unsupported("slice"); 194 } 195 196 @Override 197 public ArrayData convert(final Class<?> type) { 198 throw unsupported("convert"); 199 } 200 201 private UnsupportedOperationException unsupported(final String method) { 202 return new UnsupportedOperationException(method); 203 } 204 }