1 2 17 18 19 package org.apache.poi.hssf.record; 20 21 import org.apache.poi.util.LittleEndian; 22 import org.apache.poi.util.StringUtil; 23 24 33 34 public class HeaderRecord 35 extends Record 36 { 37 public final static short sid = 0x14; 38 private byte field_1_header_len; 39 private String field_2_header; 40 41 public HeaderRecord() 42 { 43 } 44 45 52 53 public HeaderRecord(short id, short size, byte [] data) 54 { 55 super(id, size, data); 56 } 57 58 66 67 public HeaderRecord(short id, short size, byte [] data, int offset) 68 { 69 super(id, size, data, offset); 70 } 71 72 protected void validateSid(short id) 73 { 74 if (id != sid) 75 { 76 throw new RecordFormatException("NOT A HEADERRECORD"); 77 } 78 } 79 80 protected void fillFields(byte [] data, short size, int offset) 81 { 82 if (size > 0) 83 { 84 field_1_header_len = data[ 0 + offset ]; 85 field_2_header = StringUtil.getFromCompressedUnicode(data, 3 + offset, LittleEndian.ubyteToInt(field_1_header_len)); 87 } 88 } 89 90 96 97 public void setHeaderLength(byte len) 98 { 99 field_1_header_len = len; 100 } 101 102 108 109 public void setHeader(String header) 110 { 111 field_2_header = header; 112 } 113 114 120 121 public short getHeaderLength() 122 { 123 return (short)(0xFF & field_1_header_len); } 125 126 132 133 public String getHeader() 134 { 135 return field_2_header; 136 } 137 138 public String toString() 139 { 140 StringBuffer buffer = new StringBuffer (); 141 142 buffer.append("[HEADER]\n"); 143 buffer.append(" .length = ").append(getHeaderLength()) 144 .append("\n"); 145 buffer.append(" .header = ").append(getHeader()) 146 .append("\n"); 147 buffer.append("[/HEADER]\n"); 148 return buffer.toString(); 149 } 150 151 public int serialize(int offset, byte [] data) 152 { 153 int len = 4; 154 155 if (getHeaderLength() != 0) 156 { 157 len+=3; } 159 LittleEndian.putShort(data, 0 + offset, sid); 160 LittleEndian.putShort(data, 2 + offset, 161 ( short ) ((len - 4) + getHeaderLength())); 162 163 if (getHeaderLength() > 0) 164 { 165 data[ 4 + offset ] = (byte)getHeaderLength(); 166 StringUtil.putCompressedUnicode(getHeader(), data, 7 + offset); } 168 return getRecordSize(); 169 } 170 171 public int getRecordSize() 172 { 173 int retval = 4; 174 175 if (getHeaderLength() != 0) 176 { 177 retval+=3; } 179 retval += getHeaderLength(); 180 return retval; 181 } 182 183 public short getSid() 184 { 185 return this.sid; 186 } 187 188 public Object clone() { 189 HeaderRecord rec = new HeaderRecord(); 190 rec.field_1_header_len = field_1_header_len; 191 rec.field_2_header = field_2_header; 192 return rec; 193 } 194 } 195 | Popular Tags |