1 16 17 package org.apache.poi.hpsf; 18 19 import java.io.ByteArrayInputStream ; 20 import java.io.ByteArrayOutputStream ; 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.OutputStream ; 24 import java.io.UnsupportedEncodingException ; 25 import java.util.Iterator ; 26 import java.util.LinkedList ; 27 import java.util.ListIterator ; 28 29 import org.apache.poi.util.LittleEndian; 30 import org.apache.poi.util.LittleEndianConsts; 31 32 33 34 45 public class MutablePropertySet extends PropertySet 46 { 47 48 53 public MutablePropertySet() 54 { 55 56 byteOrder = LittleEndian.getUShort(BYTE_ORDER_ASSERTION); 57 58 59 format = LittleEndian.getUShort(FORMAT_ASSERTION); 60 61 63 osVersion = (OS_WIN32 << 16) | 0x0A04; 64 65 66 classID = new ClassID(); 67 68 70 sections = new LinkedList (); 71 sections.add(new MutableSection()); 72 } 73 74 75 76 84 public MutablePropertySet(final PropertySet ps) 85 { 86 byteOrder = ps.getByteOrder(); 87 format = ps.getFormat(); 88 osVersion = ps.getOSVersion(); 89 setClassID(ps.getClassID()); 90 clearSections(); 91 for (final Iterator i = ps.getSections().iterator(); i.hasNext();) 92 { 93 final MutableSection s = new MutableSection((Section) (i.next())); 94 addSection(s); 95 } 96 } 97 98 99 100 103 private final int OFFSET_HEADER = 104 BYTE_ORDER_ASSERTION.length + 105 FORMAT_ASSERTION.length + 106 LittleEndianConsts.INT_SIZE + 107 ClassID.LENGTH + 108 LittleEndianConsts.INT_SIZE; 109 110 111 112 117 public void setByteOrder(final int byteOrder) 118 { 119 this.byteOrder = byteOrder; 120 } 121 122 123 124 129 public void setFormat(final int format) 130 { 131 this.format = format; 132 } 133 134 135 136 141 public void setOSVersion(final int osVersion) 142 { 143 this.osVersion = osVersion; 144 } 145 146 147 148 156 public void setClassID(final ClassID classID) 157 { 158 this.classID = classID; 159 } 160 161 162 163 166 public void clearSections() 167 { 168 sections = null; 169 } 170 171 172 173 180 public void addSection(final Section section) 181 { 182 if (sections == null) 183 sections = new LinkedList (); 184 sections.add(section); 185 } 186 187 188 189 198 public void write(final OutputStream out) 199 throws WritingNotSupportedException, IOException 200 { 201 202 final int nrSections = sections.size(); 203 int length = 0; 204 205 206 length += TypeWriter.writeToStream(out, (short) getByteOrder()); 207 length += TypeWriter.writeToStream(out, (short) getFormat()); 208 length += TypeWriter.writeToStream(out, (int) getOSVersion()); 209 length += TypeWriter.writeToStream(out, getClassID()); 210 length += TypeWriter.writeToStream(out, (int) nrSections); 211 int offset = OFFSET_HEADER; 212 213 216 offset += nrSections * (ClassID.LENGTH + LittleEndian.INT_SIZE); 217 final int sectionsBegin = offset; 218 for (final ListIterator i = sections.listIterator(); i.hasNext();) 219 { 220 final MutableSection s = (MutableSection) i.next(); 221 final ClassID formatID = s.getFormatID(); 222 if (formatID == null) 223 throw new NoFormatIDException(); 224 length += TypeWriter.writeToStream(out, s.getFormatID()); 225 length += TypeWriter.writeUIntToStream(out, offset); 226 try 227 { 228 offset += s.getSize(); 229 } 230 catch (HPSFRuntimeException ex) 231 { 232 final Throwable cause = ex.getReason(); 233 if (cause instanceof UnsupportedEncodingException ) 234 throw new IllegalPropertySetDataException(cause); 235 else 236 throw ex; 237 } 238 } 239 240 241 offset = sectionsBegin; 242 for (final ListIterator i = sections.listIterator(); i.hasNext();) 243 { 244 final MutableSection s = (MutableSection) i.next(); 245 offset += s.write(out); 246 } 247 } 248 249 250 251 265 public InputStream toInputStream() 266 throws IOException , WritingNotSupportedException 267 { 268 final ByteArrayOutputStream psStream = new ByteArrayOutputStream (); 269 write(psStream); 270 psStream.close(); 271 final byte[] streamData = psStream.toByteArray(); 272 return new ByteArrayInputStream (streamData); 273 } 274 275 } 276 | Popular Tags |