001: /* ====================================================================
002: Licensed to the Apache Software Foundation (ASF) under one or more
003: contributor license agreements. See the NOTICE file distributed with
004: this work for additional information regarding copyright ownership.
005: The ASF licenses this file to You under the Apache License, Version 2.0
006: (the "License"); you may not use this file except in compliance with
007: the License. You may obtain a copy of the License at
008:
009: http://www.apache.org/licenses/LICENSE-2.0
010:
011: Unless required by applicable law or agreed to in writing, software
012: distributed under the License is distributed on an "AS IS" BASIS,
013: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: See the License for the specific language governing permissions and
015: limitations under the License.
016: ==================================================================== */
017:
018: package org.apache.poi.hpsf;
019:
020: import java.io.IOException;
021: import java.io.OutputStream;
022:
023: import org.apache.poi.util.LittleEndian;
024:
025: /**
026: * <p>Class for writing little-endian data and more.</p>
027: *
028: * @author Rainer Klute <a
029: * href="mailto:klute@rainer-klute.de"><klute@rainer-klute.de></a>
030: * @version $Id: TypeWriter.java 489730 2006-12-22 19:18:16Z bayard $
031: * @since 2003-02-20
032: */
033: public class TypeWriter {
034:
035: /**
036: * <p>Writes a two-byte value (short) to an output stream.</p>
037: *
038: * @param out The stream to write to.
039: * @param n The value to write.
040: * @return The number of bytes that have been written.
041: * @exception IOException if an I/O error occurs
042: */
043: public static int writeToStream(final OutputStream out,
044: final short n) throws IOException {
045: final int length = LittleEndian.SHORT_SIZE;
046: byte[] buffer = new byte[length];
047: LittleEndian.putShort(buffer, 0, n); // FIXME: unsigned
048: out.write(buffer, 0, length);
049: return length;
050: }
051:
052: /**
053: * <p>Writes a four-byte value to an output stream.</p>
054: *
055: * @param out The stream to write to.
056: * @param n The value to write.
057: * @exception IOException if an I/O error occurs
058: * @return The number of bytes written to the output stream.
059: */
060: public static int writeToStream(final OutputStream out, final int n)
061: throws IOException {
062: final int l = LittleEndian.INT_SIZE;
063: final byte[] buffer = new byte[l];
064: LittleEndian.putInt(buffer, 0, n);
065: out.write(buffer, 0, l);
066: return l;
067:
068: }
069:
070: /**
071: * <p>Writes a eight-byte value to an output stream.</p>
072: *
073: * @param out The stream to write to.
074: * @param n The value to write.
075: * @exception IOException if an I/O error occurs
076: * @return The number of bytes written to the output stream.
077: */
078: public static int writeToStream(final OutputStream out, final long n)
079: throws IOException {
080: final int l = LittleEndian.LONG_SIZE;
081: final byte[] buffer = new byte[l];
082: LittleEndian.putLong(buffer, 0, n);
083: out.write(buffer, 0, l);
084: return l;
085:
086: }
087:
088: /**
089: * <p>Writes an unsigned two-byte value to an output stream.</p>
090: *
091: * @param out The stream to write to
092: * @param n The value to write
093: * @exception IOException if an I/O error occurs
094: */
095: public static void writeUShortToStream(final OutputStream out,
096: final int n) throws IOException {
097: int high = n & 0xFFFF0000;
098: if (high != 0)
099: throw new IllegalPropertySetDataException("Value " + n
100: + " cannot be represented by 2 bytes.");
101: writeToStream(out, (short) n);
102: }
103:
104: /**
105: * <p>Writes an unsigned four-byte value to an output stream.</p>
106: *
107: * @param out The stream to write to.
108: * @param n The value to write.
109: * @return The number of bytes that have been written to the output stream.
110: * @exception IOException if an I/O error occurs
111: */
112: public static int writeUIntToStream(final OutputStream out,
113: final long n) throws IOException {
114: long high = n & 0xFFFFFFFF00000000L;
115: if (high != 0 && high != 0xFFFFFFFF00000000L)
116: throw new IllegalPropertySetDataException("Value " + n
117: + " cannot be represented by 4 bytes.");
118: return writeToStream(out, (int) n);
119: }
120:
121: /**
122: * <p>Writes a 16-byte {@link ClassID} to an output stream.</p>
123: *
124: * @param out The stream to write to
125: * @param n The value to write
126: * @return The number of bytes written
127: * @exception IOException if an I/O error occurs
128: */
129: public static int writeToStream(final OutputStream out,
130: final ClassID n) throws IOException {
131: byte[] b = new byte[16];
132: n.write(b, 0);
133: out.write(b, 0, b.length);
134: return b.length;
135: }
136:
137: /**
138: * <p>Writes an array of {@link Property} instances to an output stream
139: * according to the Horrible Property Stream Format.</p>
140: *
141: * @param out The stream to write to
142: * @param properties The array to write to the stream
143: * @param codepage The codepage number to use for writing strings
144: * @exception IOException if an I/O error occurs
145: * @throws UnsupportedVariantTypeException if HPSF does not support some
146: * variant type.
147: */
148: public static void writeToStream(final OutputStream out,
149: final Property[] properties, final int codepage)
150: throws IOException, UnsupportedVariantTypeException {
151: /* If there are no properties don't write anything. */
152: if (properties == null)
153: return;
154:
155: /* Write the property list. This is a list containing pairs of property
156: * ID and offset into the stream. */
157: for (int i = 0; i < properties.length; i++) {
158: final Property p = properties[i];
159: writeUIntToStream(out, p.getID());
160: writeUIntToStream(out, p.getSize());
161: }
162:
163: /* Write the properties themselves. */
164: for (int i = 0; i < properties.length; i++) {
165: final Property p = properties[i];
166: long type = p.getType();
167: writeUIntToStream(out, type);
168: VariantSupport.write(out, (int) type, p.getValue(),
169: codepage);
170: }
171: }
172:
173: /**
174: * <p>Writes a double value value to an output stream.</p>
175: *
176: * @param out The stream to write to.
177: * @param n The value to write.
178: * @exception IOException if an I/O error occurs
179: * @return The number of bytes written to the output stream.
180: */
181: public static int writeToStream(final OutputStream out,
182: final double n) throws IOException {
183: final int l = LittleEndian.DOUBLE_SIZE;
184: final byte[] buffer = new byte[l];
185: LittleEndian.putDouble(buffer, 0, n);
186: out.write(buffer, 0, l);
187: return l;
188: }
189:
190: }
|