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

Quick Search    Search Deep

Source code: com/chaoswg/xtc4y/classdesc/ByteContainer.java


1   //$Header: /cvsroot/xtc4y/xtc4y/src/com/chaoswg/xtc4y/classdesc/ByteContainer.java,v 1.1.1.1 2003/08/07 13:40:30 toggm Exp $
2   /******************************************************************************
3    * XTC4y - eXtreme Testing Collection 4 you                                   *
4    * -------------------------------------------------------------------------- *
5    * URL: http://www.chaoswg.com/xtc4y                                          *
6    * Author: Mike Toggweiler (2.dog@gmx.ch)                                     *
7    *                                                                            *
8    * Last Updated: $Date: 2003/08/07 13:40:30 $, by $Author: toggm $            *
9    * Version: $Revision: 1.1.1.1 $                                                  *
10   * -------------------------------------------------------------------------- *
11   * COPYRIGHT:   (c) 2003 by Mike Toggweiler                                   *
12   *                                                                            *
13   * This program is free software; you can redistribute it and/or modify       *
14   * it under the terms of the GNU General Public License as published by       *
15   * the Free Software Foundation; either version 2 of the License, or          *
16   * (at your option) any later version.                                        *
17   *****************************************************************************/
18  package com.chaoswg.xtc4y.classdesc;
19  
20  import java.io.DataInputStream;
21  import java.io.DataOutputStream;
22  import java.io.ByteArrayInputStream;
23  import java.io.ByteArrayOutputStream;
24  import java.io.IOException;
25  
26  /**
27   * this class is used to store several different data types into a byte
28   * container a retrieve at last a byte array or a DataInputStream
29   * @author Mike Toggweiler
30   **/
31  public class ByteContainer {
32      private ByteArrayOutputStream baos;
33      private DataOutputStream dos;
34  
35      public ByteContainer() {
36    baos = new ByteArrayOutputStream();
37    dos = new DataOutputStream(baos);
38      }
39  
40      /**
41       * Add a byte to the container
42       **/
43      public void addByte(byte b) {
44    try {
45        dos.writeByte(b);
46    }
47    catch (IOException ioe) {
48    }
49      }
50  
51      /**
52       * Add a short to the container
53       **/
54      public void addShort(short s) {
55    try {
56        dos.writeShort(s);
57    }
58    catch (IOException io) {
59    }
60      }
61  
62      /**
63       * Add an int to the container
64       **/
65      public void addInt(int i) {
66    try {
67        dos.writeInt(i);
68    }
69    catch (IOException ioe) {
70    }
71      }
72  
73      /**
74       * Add a byte array to the container
75       **/
76      public void addByteArray(byte[] array) {
77    try {
78        for (int i=0; i<array.length; ++i) {
79      dos.writeByte(array[i]);
80        }
81    }
82    catch (IOException ioe) {
83    }
84      }
85  
86      /**
87       * @return the current byte array
88       **/
89      public byte[] getByteArray() {
90    synchronized(dos) {
91        try {
92      byte[] b = baos.toByteArray();
93      
94      //write back bytes for further usage
95      dos.write(b);
96      
97      return b;
98        }
99        catch (IOException ioe) {
100       }
101       return new byte[0];
102   }
103     }
104 
105     /**
106      * @return a datainput stream connected to the current byte array
107      **/
108     public DataInputStream getDataInputStream() {
109   byte[] b = getByteArray();  
110   return new DataInputStream(new ByteArrayInputStream(b));  
111     }
112 }