1 2 17 18 19 package org.apache.poi.poifs.storage; 20 21 import java.io.IOException ; 22 import java.io.InputStream ; 23 import java.io.OutputStream ; 24 25 import java.util.Arrays ; 26 27 import org.apache.poi.poifs.common.POIFSConstants; 28 import org.apache.poi.util.IOUtils; 29 import org.apache.poi.util.IntegerField; 30 import org.apache.poi.util.LittleEndian; 31 import org.apache.poi.util.LittleEndianConsts; 32 33 38 39 public class DocumentBlock 40 extends BigBlock 41 { 42 private static final byte _default_value = ( byte ) 0xFF; 43 private byte[] _data; 44 private int _bytes_read; 45 46 53 54 public DocumentBlock(final RawDataBlock block) 55 throws IOException 56 { 57 _data = block.getData(); 58 _bytes_read = _data.length; 59 } 60 61 68 69 public DocumentBlock(final InputStream stream) 70 throws IOException 71 { 72 this(); 73 int count = IOUtils.readFully(stream, _data); 74 75 _bytes_read = (count == -1) ? 0 76 : count; 77 } 78 79 82 83 private DocumentBlock() 84 { 85 _data = new byte[ POIFSConstants.BIG_BLOCK_SIZE ]; 86 Arrays.fill(_data, _default_value); 87 } 88 89 94 95 public int size() 96 { 97 return _bytes_read; 98 } 99 100 105 106 public boolean partiallyRead() 107 { 108 return _bytes_read != POIFSConstants.BIG_BLOCK_SIZE; 109 } 110 111 114 115 public static byte getFillByte() 116 { 117 return _default_value; 118 } 119 120 130 131 public static DocumentBlock [] convert(final byte [] array, 132 final int size) 133 { 134 DocumentBlock[] rval = 135 new DocumentBlock[ (size + POIFSConstants.BIG_BLOCK_SIZE - 1) / POIFSConstants.BIG_BLOCK_SIZE ]; 136 int offset = 0; 137 138 for (int k = 0; k < rval.length; k++) 139 { 140 rval[ k ] = new DocumentBlock(); 141 if (offset < array.length) 142 { 143 int length = Math.min(POIFSConstants.BIG_BLOCK_SIZE, 144 array.length - offset); 145 146 System.arraycopy(array, offset, rval[ k ]._data, 0, length); 147 if (length != POIFSConstants.BIG_BLOCK_SIZE) 148 { 149 Arrays.fill(rval[ k ]._data, length, 150 POIFSConstants.BIG_BLOCK_SIZE, 151 _default_value); 152 } 153 } 154 else 155 { 156 Arrays.fill(rval[ k ]._data, _default_value); 157 } 158 offset += POIFSConstants.BIG_BLOCK_SIZE; 159 } 160 return rval; 161 } 162 163 170 171 public static void read(final DocumentBlock [] blocks, 172 final byte [] buffer, final int offset) 173 { 174 int firstBlockIndex = offset / POIFSConstants.BIG_BLOCK_SIZE; 175 int firstBlockOffset = offset % POIFSConstants.BIG_BLOCK_SIZE; 176 int lastBlockIndex = (offset + buffer.length - 1) 177 / POIFSConstants.BIG_BLOCK_SIZE; 178 179 if (firstBlockIndex == lastBlockIndex) 180 { 181 System.arraycopy(blocks[ firstBlockIndex ]._data, 182 firstBlockOffset, buffer, 0, buffer.length); 183 } 184 else 185 { 186 int buffer_offset = 0; 187 188 System.arraycopy(blocks[ firstBlockIndex ]._data, 189 firstBlockOffset, buffer, buffer_offset, 190 POIFSConstants.BIG_BLOCK_SIZE 191 - firstBlockOffset); 192 buffer_offset += POIFSConstants.BIG_BLOCK_SIZE - firstBlockOffset; 193 for (int j = firstBlockIndex + 1; j < lastBlockIndex; j++) 194 { 195 System.arraycopy(blocks[ j ]._data, 0, buffer, buffer_offset, 196 POIFSConstants.BIG_BLOCK_SIZE); 197 buffer_offset += POIFSConstants.BIG_BLOCK_SIZE; 198 } 199 System.arraycopy(blocks[ lastBlockIndex ]._data, 0, buffer, 200 buffer_offset, buffer.length - buffer_offset); 201 } 202 } 203 204 205 206 215 216 void writeData(final OutputStream stream) 217 throws IOException 218 { 219 doWriteData(stream, _data); 220 } 221 222 223 } 225 | Popular Tags |