Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: gnu/java/security/der/DERValue.java


1   /* DERValue.java -- a value read or written to a DER encoding.
2      Copyright (C) 2003 Free Software Foundation, Inc.
3   
4   This file is part of GNU Classpath.
5   
6   GNU Classpath is free software; you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation; either version 2, or (at your option)
9   any later version.
10  
11  GNU Classpath is distributed in the hope that it will be useful, but
12  WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  General Public License for more details.
15  
16  You should have received a copy of the GNU General Public License
17  along with GNU Classpath; see the file COPYING.  If not, write to the
18  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19  02110-1301 USA.
20  
21  Linking this library statically or dynamically with other modules is
22  making a combined work based on this library.  Thus, the terms and
23  conditions of the GNU General Public License cover the whole
24  combination.
25  
26  As a special exception, the copyright holders of this library give you
27  permission to link this library with independent modules to produce an
28  executable, regardless of the license terms of these independent
29  modules, and to copy and distribute the resulting executable under
30  terms of your choice, provided that you also meet, for each linked
31  independent module, the terms and conditions of the license of that
32  module.  An independent module is a module which is not derived from
33  or based on this library.  If you modify this library, you may extend
34  this exception to your version of the library, but you are not
35  obligated to do so.  If you do not wish to do so, delete this
36  exception statement from your version. */
37  
38  
39  package gnu.java.security.der;
40  
41  import java.io.ByteArrayOutputStream;
42  import java.io.IOException;
43  
44  public class DERValue implements DER
45  {
46  
47    // Fields.
48    // ------------------------------------------------------------------------
49  
50    private final int tagClass;
51    private final boolean constructed;
52    private final int tag;
53    private int length;
54    private final Object value;
55    private byte[] encoded;
56  
57    // Constructor.
58    // ------------------------------------------------------------------------
59  
60    public DERValue(int tag, int length, Object value, byte[] encoded)
61    {
62      tagClass = tag & 0xC0;
63      this.tag = tag & 0x1F;
64      constructed = (tag & CONSTRUCTED) == CONSTRUCTED;
65      this.length = length;
66      this.value = value;
67      if (encoded != null)
68        this.encoded = (byte[]) encoded.clone();
69    }
70  
71    public DERValue(int tag, Object value)
72    {
73      this(tag, 0, value, null);
74    }
75  
76    // Instance methods.
77    // ------------------------------------------------------------------------
78  
79    public int getExternalTag()
80    {
81      return tagClass | tag | (constructed ? 0x20 : 0x00);
82    }
83  
84    public int getTag()
85    {
86      return tag;
87    }
88  
89    public int getTagClass()
90    {
91      return tagClass;
92    }
93  
94    public boolean isConstructed()
95    {
96      return constructed;
97    }
98  
99    public int getLength()
100   {
101     if (encoded == null)
102       {
103         try
104           {
105             ByteArrayOutputStream out = new ByteArrayOutputStream();
106             length = DERWriter.write(out, this);
107             encoded = out.toByteArray();
108           }
109         catch (IOException ioe)
110           {
111             encoded = new byte[0];
112           }
113       }
114     return length;
115   }
116 
117   public Object getValue()
118   {
119     return value;
120   }
121 
122   public Object getValueAs (final int derType) throws IOException
123   {
124     byte[] encoded = getEncoded ();
125     encoded[0] = (byte) derType;
126     return DERReader.read (encoded).getValue ();
127   }
128 
129   public byte[] getEncoded()
130   {
131     if (encoded == null)
132       {
133         try
134           {
135             ByteArrayOutputStream out = new ByteArrayOutputStream();
136             length = DERWriter.write(out, this);
137             encoded = out.toByteArray();
138           }
139         catch (IOException ioe)
140           {
141             encoded = new byte[0];
142           }
143       }
144     return (byte[]) encoded.clone();
145   }
146 
147   public int getEncodedLength()
148   {
149     if (encoded == null)
150       {
151         try
152           {
153             ByteArrayOutputStream out = new ByteArrayOutputStream();
154             length = DERWriter.write(out, this);
155             encoded = out.toByteArray();
156           }
157         catch (IOException ioe)
158           {
159             encoded = new byte[0];
160           }
161       }
162     return encoded.length;
163   }
164 
165   public String toString()
166   {
167     return "DERValue [ tag=" + tag + ", class=" + tagClass + ", constructed="
168       + constructed + ", value=" + value + " ]";
169   }
170 }