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

Quick Search    Search Deep

Source code: jreceiver/util/text/WordFormatB.java


1   /* $Header: /cvsroot/jreceiver/jreceiver/src/jreceiver/util/text/WordFormatB.java,v 1.2 2002/12/29 00:44:08 reedesau Exp $ */
2   package jreceiver.util.text;
3   
4   import java.io.ByteArrayOutputStream;
5   import java.io.IOException;
6   import java.text.FieldPosition;
7   
8   /**
9   * Formatter to output an object in 4 bytes.
10  * <p>
11  * Implementation presently assumes 4-byte integers.
12  * <p>
13  * Word example:
14  * <pre>
15  * String pattern = "{0,word}{1,word,be}{2,word,le}";
16  * MessageFormatB mfb = new MessageFormatB(pattern);
17  * Object[] objs = { new Integer(0xAABBCCDD),
18  *                   new Integer(0xAABBCCDD),
19  *                   new Integer(0xAABBCCDD) };
20  * byte[] = mfb.format(objs);
21  * </pre>
22  * {0,word} encoded as a 4-byte word using the default big-endian (Mac) order (result = AA BB CC DD)
23  * <P>
24  * {1,word,be} same as {0}
25  * <P>
26  * {2,word,le} encoded as a 4-byte word using the little-endian (Intel) order (result = DD CC BB AA)
27  * <P>
28  * There are two byte orders available for formatting words:
29  * <p>
30  * <pre>
31  * --------------------------- ---------------------------
32  *|            MSB            |           LSB             |
33  *| ------------------------- | ------------------------- |
34  *| Big Endian                | Little Endian             |
35  *| MostSignificantByteFirst  | LeastSignificantByteFirst |
36  *| "Network" Order           | "VAX" order               |
37  *| Java/Sun/Mac/IBM390       | Intel/Windows/AMD/DEC/VAX |
38  *| used by Blefuscudians     | used by Lilliputians      |
39  *| 0xCAFEBABE = CA FE BA BE  | 0xCAFEBABE = BE BA FE CA  |
40  * --------------------------- ---------------------------
41  * </pre>
42  * MSB is the default of Java and therefore the default of
43  * JReceiver.
44  *
45  * @author Reed Esau
46  * @version $Revision: 1.2 $ $Date: 2002/12/29 00:44:08 $
47  */
48  public class WordFormatB extends FormatB {
49  
50      public static final String ID = "word";
51  
52      public static int BIG_ENDIAN = 0;
53      public static int LITTLE_ENDIAN = 1;
54  
55  /**
56  * set an option string
57  *
58  * @param option
59  */
60      public void setOption(String option) {
61          if (option.equalsIgnoreCase("le")) {
62              endian = LITTLE_ENDIAN;
63          }
64          else if (option.equalsIgnoreCase("be")) {
65              endian = BIG_ENDIAN;
66          }
67      }
68  
69      public void setEndian(int endian) {
70          this.endian = endian;
71      }
72  
73  /** format the object in 4 bytes */
74      public final ByteArrayOutputStream format(Object obj,
75                                                ByteArrayOutputStream toAppendTo,
76                                                FieldPosition pos) throws IOException {
77  
78          if (obj instanceof Number) {
79              int val = ((Number)obj).intValue();
80              toAppendTo = format(val, toAppendTo, pos);
81          }
82          else {
83              System.err.println("formatting object "
84                                 +obj.getClass().getName()
85                                 +" with "
86                                 +getClass().getName()
87                                 +" not supported");
88          }
89  
90          return toAppendTo;
91      }
92  
93  
94      public final ByteArrayOutputStream format(int val,
95                                                ByteArrayOutputStream toAppendTo,
96                                                FieldPosition pos) throws IOException {
97  
98          byte[] ar = new byte[4];
99  
100         if (endian == BIG_ENDIAN) {
101 // store value as a big endian dword
102             ar[0] = (byte)( val >> 24 );
103             ar[1] = (byte)( val >> 16 );
104             ar[2] = (byte)( val >> 8  );
105             ar[3] = (byte)( val >> 0  );
106         }
107         else {
108 // store value as a little endian dword
109             ar[0] = (byte)( val >> 0  );
110             ar[1] = (byte)( val >> 8  );
111             ar[2] = (byte)( val >> 16 );
112             ar[3] = (byte)( val >> 24 );
113         }
114 
115         toAppendTo.write(ar);
116 
117         return toAppendTo;
118     }
119 
120 /**
121 */
122     public String toString() {
123         return "WordFormatB: "
124               + ((endian == BIG_ENDIAN) ? "big-endian" : "little-endian");
125     }
126 
127 //
128 // internal data members
129 //
130 
131     private int endian = BIG_ENDIAN;
132 }
133 /*
134 JRECEIVER MODIFIED BSD LICENSE
135 
136 Copyright (c) 2001-2002, Reed Esau (reed.esau@pobox.com) All rights reserved.
137 
138 Redistribution and use in source and binary forms, with or without
139 modification, are permitted provided that the following conditions are
140 met:
141 
142 Redistributions of source code must retain the above copyright notice,
143 this list of conditions and the following disclaimer.
144 
145 Redistributions in binary form must reproduce the above copyright notice,
146 this list of conditions and the following disclaimer in the documentation
147 and/or other materials provided with the distribution.
148 
149 Neither the name of the JReceiver Project
150 (http://jreceiver.sourceforge.net) nor the names of its contributors may
151 be used to endorse or promote products derived from this software without
152 specific prior written permission.
153 
154 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
155 IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
156 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
157 PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
158 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
159 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
160 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
161 INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
162 CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
163 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
164 POSSIBILITY OF SUCH DAMAGE.
165 */
166 
167 
168 
169 
170