| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
| 2 | * |
| 3 | * ***** BEGIN LICENSE BLOCK ***** |
| 4 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
| 5 | * |
| 6 | * The contents of this file are subject to the Mozilla Public License Version |
| 7 | * 1.1 (the "License"); you may not use this file except in compliance with |
| 8 | * the License. You may obtain a copy of the License at |
| 9 | * http://www.mozilla.org/MPL/ |
| 10 | * |
| 11 | * Software distributed under the License is distributed on an "AS IS" basis, |
| 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 13 | * for the specific language governing rights and limitations under the |
| 14 | * License. |
| 15 | * |
| 16 | * The Original Code is mozilla.org code. |
| 17 | * |
| 18 | * The Initial Developer of the Original Code is |
| 19 | * Netscape Communications Corporation. |
| 20 | * Portions created by the Initial Developer are Copyright (C) 1999 |
| 21 | * the Initial Developer. All Rights Reserved. |
| 22 | * |
| 23 | * Contributor(s): |
| 24 | * |
| 25 | * Alternatively, the contents of this file may be used under the terms of |
| 26 | * either the GNU General Public License Version 2 or later (the "GPL"), or |
| 27 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
| 28 | * in which case the provisions of the GPL or the LGPL are applicable instead |
| 29 | * of those above. If you wish to allow use of your version of this file only |
| 30 | * under the terms of either the GPL or the LGPL, and not to allow others to |
| 31 | * use your version of this file under the terms of the MPL, indicate your |
| 32 | * decision by deleting the provisions above and replace them with the notice |
| 33 | * and other provisions required by the GPL or the LGPL. If you do not delete |
| 34 | * the provisions above, a recipient may use your version of this file under |
| 35 | * the terms of any one of the MPL, the GPL or the LGPL. |
| 36 | * |
| 37 | * ***** END LICENSE BLOCK ***** */ |
| 38 | package org.ietf.ldap.ber.stream; |
| 39 | |
| 40 | import java.io.ByteArrayOutputStream; |
| 41 | import java.io.IOException; |
| 42 | import java.io.InputStream; |
| 43 | import java.io.OutputStream; |
| 44 | import java.util.Vector; |
| 45 | |
| 46 | /** |
| 47 | * This abstract class serves as a base class for constructed |
| 48 | * types such as sequence or set. |
| 49 | * <P>See CCITT X.209. |
| 50 | * |
| 51 | * @version 1.0 |
| 52 | */ |
| 53 | public abstract class BERConstruct extends BERElement { |
| 54 | /** |
| 55 | * List of BER elements in the construct. |
| 56 | */ |
| 57 | private Vector<BERElement> m_elements = new Vector<>(); |
| 58 | |
| 59 | /** |
| 60 | * Constructs a construct element. |
| 61 | */ |
| 62 | public BERConstruct() { |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Constructs a construct element from an input stream. |
| 67 | * @param decoder decoder for application specific BER |
| 68 | * @param stream input stream from socket |
| 69 | * @param bytes_read array of 1 int; value incremented by number |
| 70 | * of bytes read from stream |
| 71 | * @exception IOException failed to construct |
| 72 | */ |
| 73 | public BERConstruct(BERTagDecoder decoder, InputStream stream, |
| 74 | int[] bytes_read) throws IOException { |
| 75 | int contents_length = super.readLengthOctets(stream,bytes_read); |
| 76 | int[] component_length = new int[1]; |
| 77 | if (contents_length == -1) { |
| 78 | /* Constructed - indefinite length */ |
| 79 | BERElement element = null; |
| 80 | { |
| 81 | component_length[0] = 0; |
| 82 | element = getElement(decoder, stream, component_length); |
| 83 | if (element != null) |
| 84 | addElement(element); |
| 85 | } while (element != null); |
| 86 | } else { |
| 87 | /* Constructed - definite length */ |
| 88 | bytes_read[0] += contents_length; |
| 89 | while (contents_length > 0) |
| 90 | { |
| 91 | component_length[0] = 0; |
| 92 | addElement(getElement(decoder, stream,component_length)); |
| 93 | contents_length -= component_length[0]; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | |
| 99 | /** |
| 100 | * Adds an element to the list. |
| 101 | * @return BER encoding of the element. |
| 102 | */ |
| 103 | public void addElement(BERElement element) { |
| 104 | m_elements.addElement(element); |
| 105 | } |
| 106 | |
| 107 | /** |
| 108 | * Retrieves number of elements. |
| 109 | * @return number of elements. |
| 110 | */ |
| 111 | public int size() { |
| 112 | return m_elements.size(); |
| 113 | } |
| 114 | |
| 115 | /** |
| 116 | * Gets ber element at specific position. |
| 117 | * @param index index of the element to get |
| 118 | * @return BER element. |
| 119 | */ |
| 120 | public BERElement elementAt(int index) { |
| 121 | return m_elements.elementAt(index); |
| 122 | } |
| 123 | |
| 124 | /** |
| 125 | * Sends the BER encoding directly to a stream. |
| 126 | * @param stream output stream |
| 127 | * @exception IOException failed to send |
| 128 | */ |
| 129 | public void write(OutputStream stream) throws IOException { |
| 130 | stream.write(getType()); |
| 131 | |
| 132 | ByteArrayOutputStream contents_stream = new ByteArrayOutputStream(); |
| 133 | for (int i = 0; i < m_elements.size(); i++) { |
| 134 | BERElement e = elementAt(i); |
| 135 | e.write(contents_stream); |
| 136 | } |
| 137 | byte[] contents_buffer = contents_stream.toByteArray(); |
| 138 | sendDefiniteLength(stream, contents_buffer.length); |
| 139 | stream.write(contents_buffer); |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Gets the element type. |
| 144 | * @param element type |
| 145 | */ |
| 146 | public abstract int getType(); |
| 147 | |
| 148 | /** |
| 149 | * Gets the string representation. |
| 150 | * @return string representation of tag. |
| 151 | */ |
| 152 | public abstract String toString(); |
| 153 | } |