1 /*
   2  * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
   3  */
   4 /*
   5  * Licensed to the Apache Software Foundation (ASF) under one or more
   6  * contributor license agreements.  See the NOTICE file distributed with
   7  * this work for additional information regarding copyright ownership.
   8  * The ASF licenses this file to You under the Apache License, Version 2.0
   9  * (the "License"); you may not use this file except in compliance with
  10  * the License.  You may obtain a copy of the License at
  11  *
  12  *      http://www.apache.org/licenses/LICENSE-2.0
  13  *
  14  * Unless required by applicable law or agreed to in writing, software
  15  * distributed under the License is distributed on an "AS IS" BASIS,
  16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17  * See the License for the specific language governing permissions and
  18  * limitations under the License.
  19  */
  20 
  21 package com.sun.org.apache.xerces.internal.impl.dv.util;
  22 
  23 import com.sun.org.apache.xerces.internal.xs.XSException;
  24 import com.sun.org.apache.xerces.internal.xs.datatypes.ByteList;
  25 import java.util.AbstractList;
  26 
  27 /**
  28  * Implementation of <code>com.sun.org.apache.xerces.internal.xs.datatypes.ByteList</code>.
  29  *
  30  * @xerces.internal
  31  *
  32  * @author Ankit Pasricha, IBM
  33  *
  34  * @LastModified: Oct 2017
  35  */
  36 public class ByteListImpl extends AbstractList<Byte> implements ByteList {
  37 
  38     // actually data stored in a byte array
  39     protected final byte[] data;
  40 
  41     // canonical representation of the data
  42     protected String canonical;
  43 
  44     public ByteListImpl(byte[] data) {
  45         this.data = data;
  46     }
  47 
  48     /**
  49      * The number of <code>byte</code>s in the list. The range of
  50      * valid child object indices is 0 to <code>length-1</code> inclusive.
  51      */
  52     public int getLength() {
  53         return data.length;
  54     }
  55 
  56     /**
  57      * Checks if the <code>byte</code> <code>item</code> is a
  58      * member of this list.
  59      * @param item  <code>byte</code> whose presence in this list
  60      *   is to be tested.
  61      * @return  True if this list contains the <code>byte</code>
  62      *   <code>item</code>.
  63      */
  64     public boolean contains(byte item) {
  65         for (int i = 0; i < data.length; ++i) {
  66             if (data[i] == item) {
  67                 return true;
  68             }
  69         }
  70         return false;
  71     }
  72 
  73     /**
  74      * Returns the <code>index</code>th item in the collection. The index
  75      * starts at 0.
  76      * @param index  index into the collection.
  77      * @return  The <code>byte</code> at the <code>index</code>th
  78      *   position in the <code>ByteList</code>.
  79      * @exception XSException
  80      *   INDEX_SIZE_ERR: if <code>index</code> is greater than or equal to the
  81      *   number of objects in the list.
  82      */
  83     public byte item(int index)
  84         throws XSException {
  85 
  86         if(index < 0 || index > data.length - 1) {
  87             throw new XSException(XSException.INDEX_SIZE_ERR, null);
  88         }
  89         return data[index];
  90     }
  91 
  92     /*
  93      * List methods
  94      */
  95 
  96     public Byte get(int index) {
  97         if (index >= 0 && index < data.length) {
  98             return data[index];
  99         }
 100         throw new IndexOutOfBoundsException("Index: " + index);
 101     }
 102 
 103     public int size() {
 104         return getLength();
 105     }
 106 
 107     public byte[] toByteArray() {
 108         byte[] ret = new byte[data.length];
 109         System.arraycopy(data, 0, ret, 0, data.length);
 110         return ret;
 111     }
 112 }