1 2 17 18 19 package org.apache.poi.util; 20 21 import org.apache.poi.util.LittleEndian.BufferUnderrunException; 22 23 import java.io.*; 24 25 31 32 public class ByteField 33 implements FixedField 34 { 35 private static final byte _default_value = 0; 36 private byte _value; 37 private final int _offset; 38 39 47 48 public ByteField(final int offset) 49 throws ArrayIndexOutOfBoundsException 50 { 51 this(offset, _default_value); 52 } 53 54 63 64 public ByteField(final int offset, final byte value) 65 throws ArrayIndexOutOfBoundsException 66 { 67 if (offset < 0) 68 { 69 throw new ArrayIndexOutOfBoundsException ( 70 "offset cannot be negative"); 71 } 72 _offset = offset; 73 set(value); 74 } 75 76 86 87 public ByteField(final int offset, final byte [] data) 88 throws ArrayIndexOutOfBoundsException 89 { 90 this(offset); 91 readFromBytes(data); 92 } 93 94 106 107 public ByteField(final int offset, final byte value, final byte [] data) 108 throws ArrayIndexOutOfBoundsException 109 { 110 this(offset, value); 111 writeToBytes(data); 112 } 113 114 119 120 public byte get() 121 { 122 return _value; 123 } 124 125 130 131 public void set(final byte value) 132 { 133 _value = value; 134 } 135 136 145 146 public void set(final byte value, final byte [] data) 147 throws ArrayIndexOutOfBoundsException 148 { 149 set(value); 150 writeToBytes(data); 151 } 152 153 154 155 163 164 public void readFromBytes(final byte [] data) 165 throws ArrayIndexOutOfBoundsException 166 { 167 _value = data[ _offset ]; 168 } 169 170 181 182 public void readFromStream(final InputStream stream) 183 throws IOException, BufferUnderrunException 184 { 185 _value = 186 (LittleEndian.readFromStream(stream, 187 LittleEndianConsts.BYTE_SIZE))[ 0 ]; 188 } 189 190 200 201 public void writeToBytes(final byte [] data) 202 throws ArrayIndexOutOfBoundsException 203 { 204 data[ _offset ] = _value; 205 } 206 207 212 213 public String toString() 214 { 215 return String.valueOf(_value); 216 } 217 218 219 } 221 | Popular Tags |