View Javadoc
1   /*
2    * Copyright (c) 2002-2017 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[] toArray(final Number number) {
55          final ByteBuffer buff = ByteBuffer.allocate(BYTES_PER_ELEMENT);
56          buff.order(ByteOrder.LITTLE_ENDIAN);
57          buff.putInt(number != null ? number.intValue() : 0);
58          return buff.array();
59      }
60  
61      /**
62       * {@inheritDoc}
63       */
64      @Override
65      protected Object fromArray(final byte[] array, final int offset) {
66          if (offset < 0 || offset >= array.length) {
67              return Scriptable.NOT_FOUND;
68          }
69          final ByteBuffer buff = ByteBuffer.wrap(array);
70          buff.order(ByteOrder.LITTLE_ENDIAN);
71          return buff.getInt(offset) & 0xFFFFFFFFL;
72      }
73  
74      /**
75       * {@inheritDoc}
76       */
77      @Override
78      protected int getBytesPerElement() {
79          return BYTES_PER_ELEMENT;
80      }
81  
82  }