View Javadoc
1   /*
2    * Copyright (c) 2002-2018 Gargoyle Software Inc.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    * http://www.apache.org/licenses/LICENSE-2.0
8    *
9    * Unless required by applicable law or agreed to in writing, software
10   * distributed under the License is distributed on an "AS IS" BASIS,
11   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12   * See the License for the specific language governing permissions and
13   * limitations under the License.
14   */
15  package com.gargoylesoftware.htmlunit.javascript.host.arrays;
16  
17  import java.nio.ByteBuffer;
18  import java.nio.ByteOrder;
19  
20  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxClass;
21  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstant;
22  import com.gargoylesoftware.htmlunit.javascript.configuration.JsxConstructor;
23  
24  import net.sourceforge.htmlunit.corejs.javascript.Scriptable;
25  
26  /**
27   * Represents an array of unsigned 32-bit integers.
28   *
29   * @author Ahmed Ashour
30   * @author Frank Danek
31   * @author Ronald Brill
32   * @author Michael Rimov
33   */
34  @JsxClass
35  public class Uint32Array extends ArrayBufferViewBase {
36  
37      /** The size, in bytes, of each array element. */
38      @JsxConstant
39      public static final int BYTES_PER_ELEMENT = 4;
40  
41      /**
42       * {@inheritDoc}
43       */
44      @Override
45      @JsxConstructor
46      public void constructor(final Object object, final Object byteOffset, final Object length) {
47          super.constructor(object, byteOffset, length);
48      }
49  
50      /**
51       * {@inheritDoc}
52       */
53      @Override
54      protected byte[] toByteArray(final Number number) {
55          final ByteBuffer buff = ByteBuffer.allocate(BYTES_PER_ELEMENT);
56          buff.order(ByteOrder.LITTLE_ENDIAN);
57          if (number == null || Double.isInfinite(number.doubleValue())) {
58              buff.putInt(0);
59              return buff.array();
60          }
61          buff.putInt(number.intValue());
62          return buff.array();
63      }
64  
65      /**
66       * {@inheritDoc}
67       */
68      @Override
69      protected Object fromArray(final byte[] array, final int offset) {
70          if (offset < 0 || offset >= array.length) {
71              return Scriptable.NOT_FOUND;
72          }
73          final ByteBuffer buff = ByteBuffer.wrap(array);
74          buff.order(ByteOrder.LITTLE_ENDIAN);
75          return buff.getInt(offset) & 0xFFFFFFFFL;
76      }
77  
78      /**
79       * {@inheritDoc}
80       */
81      @Override
82      protected int getBytesPerElement() {
83          return BYTES_PER_ELEMENT;
84      }
85  
86  }