001    // Copyright (C) 2002 IAIK
002    // https://jce.iaik.tugraz.at
003    //
004    // Copyright (C) 2003 - 2025 Stiftung Secure Information and
005    //                           Communication Technologies SIC
006    // https://sic.tech
007    //
008    // All rights reserved.
009    //
010    // Redistribution and use in source and binary forms, with or without
011    // modification, are permitted provided that the following conditions
012    // are met:
013    // 1. Redistributions of source code must retain the above copyright
014    //    notice, this list of conditions and the following disclaimer.
015    // 2. Redistributions in binary form must reproduce the above copyright
016    //    notice, this list of conditions and the following disclaimer in the
017    //    documentation and/or other materials provided with the distribution.
018    //
019    // THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
020    // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
021    // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
022    // ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
023    // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
024    // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
025    // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
026    // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
027    // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
028    // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
029    // SUCH DAMAGE.
030    
031    // Copyright (C) 2002 IAIK
032    // https://sic.tech/
033    //
034    // Copyright (C) 2003 - 2025 Stiftung Secure Information and 
035    //                           Communication Technologies SIC
036    // https://sic.tech/
037    //
038    // All rights reserved.
039    //
040    // This source is provided for inspection purposes and recompilation only,
041    // unless specified differently in a contract with IAIK. This source has to
042    // be kept in strict confidence and must not be disclosed to any third party
043    // under any circumstances. Redistribution in source and binary forms, with
044    // or without modification, are <not> permitted in any case!
045    //
046    // THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
047    // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
048    // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
049    // ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
050    // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
051    // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
052    // OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
053    // HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
054    // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
055    // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
056    // SUCH DAMAGE.
057    //
058    // $Header: /IAIK-CMS/current/src/demo/cms/data/DataOutputStreamDemo.java 6     12.02.25 17:58 Dbratko $
059    // $Revision: 6 $
060    //
061    
062    package demo.cms.data;
063    
064    import iaik.asn1.ObjectID;
065    import iaik.cms.CMSException;
066    import iaik.cms.ContentInfoOutputStream;
067    import iaik.cms.DataOutputStream;
068    import iaik.cms.DataStream;
069    import iaik.utils.Util;
070    
071    import java.io.ByteArrayInputStream;
072    import java.io.ByteArrayOutputStream;
073    import java.io.IOException;
074    import java.io.InputStream;
075    
076    import demo.DemoUtil;
077    
078    /**
079     * Demonstrates the usage of class {@link iaik.cms.DataOutputStream}.
080     */
081    public class DataOutputStreamDemo {
082    
083      /**
084       * Default constructor.
085       */
086      public DataOutputStreamDemo() throws IOException {
087        System.out.println();
088        System.out.println("**********************************************************************************");
089        System.out.println("*                          DataOutputStream demo                                 *");
090        System.out.println("*           (shows the usage of the DataOutputStream implementation)             *");
091        System.out.println("**********************************************************************************");
092        System.out.println();
093      }
094    
095    
096      /**
097       * Uses the IAIK-CMS DataOutputStream class to encode the given data.
098       * 
099       * @param message the message to be encoded, as byte representation
100       * 
101       * @return the BER encoding of the <code>Data</code> object just created,
102       *         wrapped in a ContentInfo
103       * 
104       * @throws CMSException if the <code>Data</code> object cannot
105       *                          be created
106       * @throws IOException if an I/O error occurs
107       */
108      public byte[] createData(byte[] message) throws CMSException, IOException  {
109    
110        System.out.println("Create a new Data message:");
111    
112        // the stream from which to read the content
113        ByteArrayInputStream is = new ByteArrayInputStream(message);
114    
115        // the stream to which to write the Data object
116        ByteArrayOutputStream resultStream = new ByteArrayOutputStream();
117        
118        // wrap Data into a ContentInfo 
119        ContentInfoOutputStream contentInfoStream = 
120          new ContentInfoOutputStream(ObjectID.cms_data, resultStream);
121        // create a new DataOutputStream object 
122        DataOutputStream data = new DataOutputStream(contentInfoStream);
123    
124    
125        int blockSize = 8; // in real world we would use a block size like 2048
126        //  write the data 
127        byte[] buffer = new byte[blockSize];
128        int bytesRead;
129        while ((bytesRead = is.read(buffer)) != -1) {
130          data.write(buffer, 0, bytesRead);
131        }
132        
133        // closing the stream 
134        data.close();
135        return resultStream.toByteArray();
136      }
137    
138      /**
139       * Parses a CMS <code>Data</code> object.
140       *
141       * @param data the <code>Data</code> object as BER encoded byte array
142       *
143       * @return the inherent content as byte array
144       *
145       * @throws CMSException if an parsing exception occurs
146       * @throws IOException if an I/O error occurs
147       */
148      public byte[] getData(byte[] data) throws CMSException, IOException {
149    
150        // we are testing the stream interface
151        ByteArrayInputStream is = new ByteArrayInputStream(data);
152        // create the Data object
153        DataStream dataStream = new DataStream(is);
154    
155        // get an InputStream for reading the signed content
156        InputStream content = dataStream.getInputStream();
157        ByteArrayOutputStream os = new ByteArrayOutputStream();
158        Util.copyStream(content, os, null);
159    
160        return os.toByteArray();
161      }
162    
163    
164      
165      /**
166       * Starts the tests.
167       */
168      public void start() {
169         // the test message
170        String m = "This is the test message.";
171        System.out.println("Test message: \""+m+"\"");
172        System.out.println();
173        byte[] message = m.getBytes();
174    
175        try {
176          byte[] encoding;
177          byte[] received_message = null;
178          
179          System.out.println("\nDataOutputStream demo [create]:\n");
180          encoding = createData(message);
181          // transmit data
182          System.out.println("\nDataOutputStream demo [parse]:\n");
183          received_message = getData(encoding);
184          System.out.print("\nContent: ");
185          System.out.println(new String(received_message));
186    
187            } catch (Exception ex) {
188              ex.printStackTrace();
189              throw new RuntimeException(ex.toString());
190            }
191      }
192    
193      /**
194       * Main method.
195       */
196      public static void main(String argv[]) throws Exception {
197    
198            DemoUtil.initDemos();
199    
200        (new DataOutputStreamDemo()).start();
201        System.out.println("\nReady!");
202        DemoUtil.waitKey();
203      }
204    }