001/*
002 * Copyright 2009-2020 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2009-2020 Ping Identity Corporation
007 *
008 * Licensed under the Apache License, Version 2.0 (the "License");
009 * you may not use this file except in compliance with the License.
010 * You may obtain a copy of the License at
011 *
012 *    http://www.apache.org/licenses/LICENSE-2.0
013 *
014 * Unless required by applicable law or agreed to in writing, software
015 * distributed under the License is distributed on an "AS IS" BASIS,
016 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017 * See the License for the specific language governing permissions and
018 * limitations under the License.
019 */
020/*
021 * Copyright (C) 2009-2020 Ping Identity Corporation
022 *
023 * This program is free software; you can redistribute it and/or modify
024 * it under the terms of the GNU General Public License (GPLv2 only)
025 * or the terms of the GNU Lesser General Public License (LGPLv2.1 only)
026 * as published by the Free Software Foundation.
027 *
028 * This program is distributed in the hope that it will be useful,
029 * but WITHOUT ANY WARRANTY; without even the implied warranty of
030 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
031 * GNU General Public License for more details.
032 *
033 * You should have received a copy of the GNU General Public License
034 * along with this program; if not, see <http://www.gnu.org/licenses>.
035 */
036package com.unboundid.asn1;
037
038
039
040import com.unboundid.util.Mutable;
041import com.unboundid.util.ThreadSafety;
042import com.unboundid.util.ThreadSafetyLevel;
043
044import static com.unboundid.asn1.ASN1Messages.*;
045
046
047
048/**
049 * This class provides a data structure which is used in the course of reading
050 * an ASN.1 set from an ASN.1 stream reader.  It provides access to the BER type
051 * and the total number of bytes in the encoded representations of all of the
052 * embedded values, and provides a method to determine when the end of the set
053 * has been reached.
054 */
055@Mutable()
056@ThreadSafety(level=ThreadSafetyLevel.NOT_THREADSAFE)
057public final class ASN1StreamReaderSet
058{
059  // The ASN.1 stream reader associated with this object.
060  private final ASN1StreamReader reader;
061
062  // The BER type for this ASN.1 set.
063  private final byte type;
064
065  // The number of bytes contained in the encoded representations of all of the
066  // embedded values.
067  private final int length;
068
069  // The value for the total number of bytes read from the associated reader at
070  // which the end of the set should be encountered.
071  private final long endBytesRead;
072
073
074
075  /**
076   * Creates a new instance of this class with the provided information.
077   *
078   * @param  reader  The ASN.1 stream reader with which this object will be
079   *                 associated.
080   * @param  type    The BER type for this ASN.1 set.
081   * @param  length  The number of bytes contained in the encoded
082   *                 representations of all the embedded values.
083   */
084  ASN1StreamReaderSet(final ASN1StreamReader reader, final byte type,
085                      final int length)
086  {
087    this.reader = reader;
088    this.type   = type;
089    this.length = length;
090
091    endBytesRead = reader.getTotalBytesRead() + length;
092  }
093
094
095
096  /**
097   * Retrieves the BER type for this ASN.1 set.
098   *
099   * @return  The BER type for this ASN.1 set.
100   */
101  public byte getType()
102  {
103    return type;
104  }
105
106
107
108  /**
109   * Retrieves the number of bytes contained in the encoded representations of
110   * all the embedded values.
111   *
112   * @return  The number of bytes contained in the encoded representations of
113   *          all the embedded values.
114   */
115  public int getLength()
116  {
117    return length;
118  }
119
120
121
122  /**
123   * Indicates whether there are more elements in this set to be read from the
124   * associated ASN.1 stream reader.
125   *
126   * @return  {@code true} if there are more elements in this set to be read
127   *          from the associated ASN.1 stream reader or false if the end of the
128   *          set has been reached.
129   *
130   * @throws  ASN1Exception  If the associated ASN.1 stream reader has already
131   *                         read beyond the end of the set.
132   */
133  public boolean hasMoreElements()
134         throws ASN1Exception
135  {
136    final long currentBytesRead = reader.getTotalBytesRead();
137    if (currentBytesRead == endBytesRead)
138    {
139      return false;
140    }
141    else if (currentBytesRead < endBytesRead)
142    {
143      return true;
144    }
145
146    throw new ASN1Exception(ERR_STREAM_READER_SET_READ_PAST_END.get(
147         length, endBytesRead, currentBytesRead));
148  }
149}