| 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.IOException; |
| 41 | import java.io.InputStream; |
| 42 | import java.io.OutputStream; |
| 43 | |
| 44 | /** |
| 45 | * This is an abstract base class for character string types. |
| 46 | * |
| 47 | * @version 1.0 |
| 48 | */ |
| 49 | public abstract class BERCharacterString extends BERElement { |
| 50 | /** |
| 51 | * Internal variables |
| 52 | */ |
| 53 | protected String m_value = null; |
| 54 | |
| 55 | /** |
| 56 | * Constructs a character string element containing a buffer. |
| 57 | */ |
| 58 | public BERCharacterString() { |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Constructs a character string element containing buffer. |
| 63 | * @param buffer a string value |
| 64 | */ |
| 65 | public BERCharacterString(String string) { |
| 66 | m_value = string; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Constructs a character string element from a byte array. |
| 71 | * @param buffer buffer containing UTF8 data |
| 72 | */ |
| 73 | public BERCharacterString(byte[] buffer) { |
| 74 | try{ |
| 75 | m_value = new String(buffer,"UTF8"); |
| 76 | } catch(Throwable x) |
| 77 | {} |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Constructs a character string element from an input stream |
| 82 | * (for constructed encoding) |
| 83 | * @param stream input stream |
| 84 | * @param bytes_read array of 1 int, incremented by number of bytes read |
| 85 | * @exception IOException failed to construct |
| 86 | */ |
| 87 | public BERCharacterString(BERTagDecoder decoder, InputStream stream, |
| 88 | int[] bytes_read) throws IOException { |
| 89 | int octet; |
| 90 | int contents_length = super.readLengthOctets(stream, bytes_read); |
| 91 | int[] component_length = new int[1]; |
| 92 | BERElement element = null; |
| 93 | |
| 94 | if (contents_length == -1) { |
| 95 | /* Constructed - indefinite length content octets. */ |
| 96 | do { |
| 97 | component_length[0] = 0; |
| 98 | element = getElement(decoder,stream,component_length); |
| 99 | if (element != null) { |
| 100 | /* element is a string of same type |
| 101 | * - add it to the existing buffer */ |
| 102 | BERCharacterString octet_element = (BERCharacterString)element; |
| 103 | String string_buffer = octet_element.getValue(); |
| 104 | if (m_value == null) { |
| 105 | m_value = string_buffer; |
| 106 | } else { |
| 107 | m_value = m_value + string_buffer; |
| 108 | } |
| 109 | } |
| 110 | } while (element != null); |
| 111 | } else { |
| 112 | /* Definite length content octets string. */ |
| 113 | bytes_read[0] += contents_length; |
| 114 | while (contents_length > 0) { |
| 115 | component_length[0] = 0; |
| 116 | element = getElement(decoder,stream,component_length); |
| 117 | if (element != null) { |
| 118 | /* element is a string of the same type |
| 119 | * - add it to the existing buffer */ |
| 120 | BERCharacterString octet_element = (BERCharacterString)element; |
| 121 | String string_buffer = octet_element.getValue(); |
| 122 | if (m_value == null) { |
| 123 | m_value = string_buffer; |
| 124 | } else { |
| 125 | m_value = m_value + string_buffer; |
| 126 | } |
| 127 | } |
| 128 | contents_length -= component_length[0]; |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | /** |
| 134 | * Constructs a character string element from an input stream |
| 135 | * (for primitive encoding) |
| 136 | * @param stream source |
| 137 | * @param bytes_read array of 1 int, incremented by number of bytes read |
| 138 | * @exception IOException failed to construct |
| 139 | */ |
| 140 | public BERCharacterString(InputStream stream, int[] bytes_read) |
| 141 | throws IOException { |
| 142 | int contents_length = super.readLengthOctets(stream, bytes_read); |
| 143 | |
| 144 | /* Definite length content octets string. */ |
| 145 | if (contents_length > 0) { |
| 146 | byte[] buffer = new byte[contents_length]; |
| 147 | for (int i = 0; i < contents_length; i++) { |
| 148 | buffer[i] = (byte) stream.read(); |
| 149 | } |
| 150 | bytes_read[0] += contents_length; |
| 151 | try { |
| 152 | m_value = new String(buffer,"UTF8"); |
| 153 | } catch(Throwable x) |
| 154 | {} |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | private byte[] byte_buf; |
| 159 | |
| 160 | /** |
| 161 | * Writes BER to stream. |
| 162 | * @param stream output stream |
| 163 | */ |
| 164 | public void write(OutputStream stream) throws IOException { |
| 165 | stream.write(getType()); /* tag */ |
| 166 | if (m_value == null) { |
| 167 | sendDefiniteLength(stream, 0); |
| 168 | } else { |
| 169 | try { |
| 170 | byte_buf = m_value.getBytes("UTF8"); |
| 171 | sendDefiniteLength(stream, byte_buf.length); /* length */ |
| 172 | } catch(Throwable x) |
| 173 | {} |
| 174 | stream.write(byte_buf,0,byte_buf.length); /* contents */ |
| 175 | } |
| 176 | } |
| 177 | |
| 178 | /** |
| 179 | * Gets the element value. |
| 180 | * @param element value |
| 181 | */ |
| 182 | public String getValue() { |
| 183 | return m_value; |
| 184 | } |
| 185 | |
| 186 | /** |
| 187 | * Gets the element type. |
| 188 | * @param element type |
| 189 | */ |
| 190 | public abstract int getType(); |
| 191 | |
| 192 | /** |
| 193 | * Gets the string representation. |
| 194 | * @return string representation. |
| 195 | */ |
| 196 | public abstract String toString(); |
| 197 | } |