001/*
002 * Copyright 2009-2024 Ping Identity Corporation
003 * All Rights Reserved.
004 */
005/*
006 * Copyright 2009-2024 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-2024 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.NotNull;
042import com.unboundid.util.ThreadSafety;
043import com.unboundid.util.ThreadSafetyLevel;
044
045import static com.unboundid.asn1.ASN1Messages.*;
046
047
048
049/**
050 * This class provides a data structure which is used in the course of reading
051 * an ASN.1 set from an ASN.1 stream reader.  It provides access to the BER type
052 * and the total number of bytes in the encoded representations of all of the
053 * embedded values, and provides a method to determine when the end of the set
054 * has been reached.
055 */
056@Mutable()
057@ThreadSafety(level=ThreadSafetyLevel.NOT_THREADSAFE)
058public final class ASN1StreamReaderSet
059{
060  // The ASN.1 stream reader associated with this object.
061  @NotNull private final ASN1StreamReader reader;
062
063  // The BER type for this ASN.1 set.
064  private final byte type;
065
066  // The number of bytes contained in the encoded representations of all of the
067  // embedded values.
068  private final int length;
069
070  // The value for the total number of bytes read from the associated reader at
071  // which the end of the set should be encountered.
072  private final long endBytesRead;
073
074
075
076  /**
077   * Creates a new instance of this class with the provided information.
078   *
079   * @param  reader  The ASN.1 stream reader with which this object will be
080   *                 associated.
081   * @param  type    The BER type for this ASN.1 set.
082   * @param  length  The number of bytes contained in the encoded
083   *                 representations of all the embedded values.
084   */
085  ASN1StreamReaderSet(@NotNull final ASN1StreamReader reader, final byte type,
086                      final int length)
087  {
088    this.reader = reader;
089    this.type   = type;
090    this.length = length;
091
092    endBytesRead = reader.getTotalBytesRead() + length;
093  }
094
095
096
097  /**
098   * Retrieves the BER type for this ASN.1 set.
099   *
100   * @return  The BER type for this ASN.1 set.
101   */
102  public byte getType()
103  {
104    return type;
105  }
106
107
108
109  /**
110   * Retrieves the number of bytes contained in the encoded representations of
111   * all the embedded values.
112   *
113   * @return  The number of bytes contained in the encoded representations of
114   *          all the embedded values.
115   */
116  public int getLength()
117  {
118    return length;
119  }
120
121
122
123  /**
124   * Indicates whether there are more elements in this set to be read from the
125   * associated ASN.1 stream reader.
126   *
127   * @return  {@code true} if there are more elements in this set to be read
128   *          from the associated ASN.1 stream reader or false if the end of the
129   *          set has been reached.
130   *
131   * @throws  ASN1Exception  If the associated ASN.1 stream reader has already
132   *                         read beyond the end of the set.
133   */
134  public boolean hasMoreElements()
135         throws ASN1Exception
136  {
137    final long currentBytesRead = reader.getTotalBytesRead();
138    if (currentBytesRead == endBytesRead)
139    {
140      return false;
141    }
142    else if (currentBytesRead < endBytesRead)
143    {
144      return true;
145    }
146
147    throw new ASN1Exception(ERR_STREAM_READER_SET_READ_PAST_END.get(
148         length, endBytesRead, currentBytesRead));
149  }
150}