KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > poi > hpsf > TypeWriter


1
2 /* ====================================================================
3    Copyright 2002-2004 Apache Software Foundation
4
5    Licensed under the Apache License, Version 2.0 (the "License");
6    you may not use this file except in compliance with the License.
7    You may obtain a copy of the License at
8
9        http://www.apache.org/licenses/LICENSE-2.0
10
11    Unless required by applicable law or agreed to in writing, software
12    distributed under the License is distributed on an "AS IS" BASIS,
13    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14    See the License for the specific language governing permissions and
15    limitations under the License.
16 ==================================================================== */

17         
18 package org.apache.poi.hpsf;
19
20 import java.io.IOException JavaDoc;
21 import java.io.OutputStream JavaDoc;
22
23 import org.apache.poi.util.LittleEndian;
24
25 /**
26  * <p>Class for writing little-endian data and more.</p>
27  *
28  * @author Rainer Klute <a
29  * HREF="mailto:klute@rainer-klute.de">&lt;klute@rainer-klute.de&gt;</a>
30  * @version $Id: TypeWriter.java,v 1.4 2004/04/09 13:05:16 glens Exp $
31  * @since 2003-02-20
32  */

33 public class TypeWriter
34 {
35
36     /**
37      * <p>Writes a two-byte value (short) to an output stream.</p>
38      *
39      * @param out The stream to write to.
40      * @param n The value to write.
41      * @return The number of bytes that have been written.
42      * @exception IOException if an I/O error occurs
43      */

44     public static int writeToStream(final OutputStream JavaDoc out, final short n)
45         throws IOException JavaDoc
46     {
47         final int length = LittleEndian.SHORT_SIZE;
48         byte[] buffer = new byte[length];
49         LittleEndian.putUShort(buffer, 0, n);
50         out.write(buffer, 0, length);
51         return length;
52     }
53
54
55
56     /**
57      * <p>Writes a four-byte value to an output stream.</p>
58      *
59      * @param out The stream to write to.
60      * @param n The value to write.
61      * @exception IOException if an I/O error occurs
62      * @return The number of bytes written to the output stream.
63      */

64     public static int writeToStream(final OutputStream JavaDoc out, final int n)
65         throws IOException JavaDoc
66     {
67         final int l = LittleEndian.INT_SIZE;
68         final byte[] buffer = new byte[l];
69         LittleEndian.putInt(buffer, 0, n);
70         out.write(buffer, 0, l);
71         return l;
72         
73     }
74
75
76
77     /**
78      * <p>Writes an unsigned two-byte value to an output stream.</p>
79      *
80      * @param out The stream to write to
81      * @param n The value to write
82      * @exception IOException if an I/O error occurs
83      */

84     public static void writeUShortToStream(final OutputStream JavaDoc out, final int n)
85         throws IOException JavaDoc
86     {
87         int high = n & 0xFFFF0000;
88         if (high != 0)
89             throw new IllegalPropertySetDataException
90                 ("Value " + n + " cannot be represented by 2 bytes.");
91         writeToStream(out, (short) n);
92     }
93
94
95
96     /**
97      * <p>Writes an unsigned four-byte value to an output stream.</p>
98      *
99      * @param out The stream to write to.
100      * @param n The value to write.
101      * @return The number of bytes that have been written to the output stream.
102      * @exception IOException if an I/O error occurs
103      */

104     public static int writeUIntToStream(final OutputStream JavaDoc out, final long n)
105         throws IOException JavaDoc
106     {
107         long high = n & 0xFFFFFFFF00000000L;
108         if (high != 0 && high != 0xFFFFFFFF00000000L)
109             throw new IllegalPropertySetDataException
110                 ("Value " + n + " cannot be represented by 4 bytes.");
111         return writeToStream(out, (int) n);
112     }
113
114
115
116     /**
117      * <p>Writes a 16-byte {@link ClassID} to an output stream.</p>
118      *
119      * @param out The stream to write to
120      * @param n The value to write
121      * @exception IOException if an I/O error occurs
122      */

123     public static int writeToStream(final OutputStream JavaDoc out, final ClassID n)
124         throws IOException JavaDoc
125     {
126         byte[] b = new byte[16];
127         n.write(b, 0);
128         out.write(b, 0, b.length);
129         return b.length;
130     }
131
132
133
134     /**
135      * <p>Writes an array of {@link Property} instances to an output stream
136      * according to the Horrible Property Stream Format.</p>
137      *
138      * @param out The stream to write to
139      * @param properties The array to write to the stream
140      * @exception IOException if an I/O error occurs
141      */

142     public static void writeToStream(final OutputStream JavaDoc out,
143                                      final Property[] properties,
144                                      final int codepage)
145         throws IOException JavaDoc, UnsupportedVariantTypeException
146     {
147         /* If there are no properties don't write anything. */
148         if (properties == null)
149             return;
150
151         /* Write the property list. This is a list containing pairs of property
152          * ID and offset into the stream. */

153         for (int i = 0; i < properties.length; i++)
154         {
155             final Property p = (Property) properties[i];
156             writeUIntToStream(out, p.getID());
157             writeUIntToStream(out, p.getSize());
158         }
159
160         /* Write the properties themselves. */
161         for (int i = 0; i < properties.length; i++)
162         {
163             final Property p = (Property) properties[i];
164             long type = p.getType();
165             writeUIntToStream(out, type);
166             VariantSupport.write(out, (int) type, p.getValue(), codepage);
167         }
168     }
169
170
171
172
173
174
175
176 }
177
Popular Tags