1 2 17 18 package org.apache.poi.hpsf; 19 20 import java.io.IOException ; 21 import java.io.OutputStream ; 22 23 import org.apache.poi.util.LittleEndian; 24 25 33 public class TypeWriter 34 { 35 36 44 public static int writeToStream(final OutputStream out, final short n) 45 throws IOException 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 64 public static int writeToStream(final OutputStream out, final int n) 65 throws IOException 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 84 public static void writeUShortToStream(final OutputStream out, final int n) 85 throws IOException 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 104 public static int writeUIntToStream(final OutputStream out, final long n) 105 throws IOException 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 123 public static int writeToStream(final OutputStream out, final ClassID n) 124 throws IOException 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 142 public static void writeToStream(final OutputStream out, 143 final Property[] properties, 144 final int codepage) 145 throws IOException , UnsupportedVariantTypeException 146 { 147 148 if (properties == null) 149 return; 150 151 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 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 |