001    /*
002     * CDDL HEADER START
003     *
004     * The contents of this file are subject to the terms of the
005     * Common Development and Distribution License, Version 1.0 only
006     * (the "License").  You may not use this file except in compliance
007     * with the License.
008     *
009     * You can obtain a copy of the license at
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2006-2008 Sun Microsystems, Inc.
026     */
027    package org.opends.server.protocols.asn1;
028    
029    
030    
031    import java.io.IOException;
032    import java.io.OutputStream;
033    import java.net.Socket;
034    
035    import static org.opends.server.loggers.debug.DebugLogger.*;
036    import org.opends.server.loggers.debug.DebugTracer;
037    import org.opends.server.types.DebugLogLevel;
038    
039    
040    /**
041     * This class defines a utility that can be used to write ASN.1 elements over a
042     * provided socket or output stream.
043     */
044    @org.opends.server.types.PublicAPI(
045         stability=org.opends.server.types.StabilityLevel.UNCOMMITTED,
046         mayInstantiate=true,
047         mayExtend=false,
048         mayInvoke=true)
049    public final class ASN1Writer
050    {
051      /**
052       * The tracer object for the debug logger.
053       */
054      private static final DebugTracer TRACER = getTracer();
055    
056    
057    
058    
059      // The output stream to which the encoded elements should be written.
060      private OutputStream outputStream;
061    
062      // The socket with which the output stream is associated.
063      private Socket socket;
064    
065    
066    
067      /**
068       * Creates a new ASN.1 writer that will write elements over the provided
069       * socket.
070       *
071       * @param  socket  The socket to use to write ASN.1 elements.
072       *
073       * @throws  IOException  If a problem occurs while trying to get the output
074       *                       stream for the socket.
075       */
076      public ASN1Writer(Socket socket)
077             throws IOException
078      {
079        this.socket  = socket;
080        outputStream = socket.getOutputStream();
081      }
082    
083    
084    
085      /**
086       * Creates a new ASN.1 writer that will write elements over the provided
087       * output stream.
088       *
089       * @param  outputStream  The output stream to use to write ASN.1 elements.
090       */
091      public ASN1Writer(OutputStream outputStream)
092      {
093        this.outputStream = outputStream;
094        socket            = null;
095      }
096    
097    
098    
099      /**
100       * Writes the provided ASN.1 element over the output stream associated with
101       * this ASN.1 writer.
102       *
103       * @param  element  The element to be written.
104       *
105       * @return  The number of bytes actually written over the output stream.
106       *
107       * @throws  IOException  If a problem occurs while trying to write the
108       *                       information over the output stream.
109       */
110      public int writeElement(ASN1Element element)
111             throws IOException
112      {
113        byte[] elementBytes = element.encode();
114        outputStream.write(elementBytes);
115        outputStream.flush();
116    
117        return elementBytes.length;
118      }
119    
120    
121    
122      /**
123       * Closes this ASN.1 writer and the underlying output stream/socket.
124       */
125      public void close()
126      {
127        try
128        {
129          outputStream.close();
130        }
131        catch (Exception e)
132        {
133          if (debugEnabled())
134          {
135            TRACER.debugCaught(DebugLogLevel.ERROR, e);
136          }
137        }
138    
139    
140        if (socket != null)
141        {
142          try
143          {
144            socket.close();
145          }
146          catch (Exception e)
147          {
148            if (debugEnabled())
149            {
150              TRACER.debugCaught(DebugLogLevel.ERROR, e);
151            }
152          }
153        }
154      }
155    }
156