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.runtime.GlobalObject; 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 public PropertyDescriptor getDescriptor(final GlobalObject global, final int index) { 64 // make the index properties not configurable 65 return global.newDataDescriptor(getObject(index), false, true, true); 66 } 67 68 @Override 69 public ArrayData copy() { 70 throw unsupported("copy"); 71 } 72 73 @Override 74 public Object[] asObjectArray() { 75 throw unsupported("asObjectArray"); 76 } 77 78 @Override 79 public void setLength(final long length) { 80 throw new UnsupportedOperationException("setLength"); 81 } 82 83 @Override 84 public void shiftLeft(int by) { 85 throw unsupported("shiftLeft"); 86 } 87 88 @Override 89 public ArrayData shiftRight(int by) { 90 throw unsupported("shiftRight"); 91 } 92 93 @Override 94 public ArrayData ensure(long safeIndex) { 95 if (safeIndex < buf.capacity()) { 96 return this; 97 } 98 99 throw unsupported("ensure"); 100 } 101 102 @Override 103 public ArrayData shrink(long newLength) { 104 throw unsupported("shrink"); 105 } 106 107 @Override 108 public ArrayData set(int index, Object value, boolean strict) { 109 if (value instanceof Number) { 110 buf.put(index, ((Number)value).byteValue()); 111 return this; 112 } 113 114 throw typeError("not.a.number", ScriptRuntime.safeToString(value)); 115 } 116 117 @Override 118 public ArrayData set(int index, int value, boolean strict) { 119 buf.put(index, (byte)value); 120 return this; 121 } 122 123 @Override 124 public ArrayData set(int index, long value, boolean strict) { 125 buf.put(index, (byte)value); 126 return this; 127 } 128 129 @Override 130 public ArrayData set(int index, double value, boolean strict) { 131 buf.put(index, (byte)value); 132 return this; 133 } 134 135 @Override 136 public int getInt(int index) { 137 return 0x0ff & buf.get(index); 138 } 139 140 @Override 141 public long getLong(int index) { 142 return 0x0ff & buf.get(index); 143 } 144 145 @Override 146 public double getDouble(int index) { 147 return 0x0ff & buf.get(index); 148 } 149 150 @Override 151 public Object getObject(int index) { 152 return (int)(0x0ff & buf.get(index)); 153 } 154 155 @Override 156 public boolean has(int index) { 157 return index > -1 && index < buf.capacity(); 158 } 159 160 @Override 161 public boolean canDelete(final int index, final boolean strict) { 162 return false; 163 } 164 165 @Override 166 public boolean canDelete(final long fromIndex, final long toIndex, final boolean strict) { 167 return false; 168 } 169 170 @Override 171 public ArrayData delete(int index) { 172 throw unsupported("delete"); 173 } 174 175 @Override 176 public ArrayData delete(long fromIndex, long toIndex) { 177 throw unsupported("delete"); 178 } 179 180 @Override 181 public ArrayData push(final boolean strict, final Object... items) { 182 throw unsupported("push"); 183 } 184 185 @Override 186 public Object pop() { 187 throw unsupported("pop"); 188 } 189 190 @Override 191 public ArrayData slice(long from, long to) { 192 throw unsupported("slice"); 193 } 194 195 @Override 196 public ArrayData convert(final Class<?> type) { 197 throw unsupported("convert"); 198 } 199 200 private UnsupportedOperationException unsupported(final String method) { 201 return new UnsupportedOperationException(method); 202 } 203 } --- EOF ---