1 package org.jscsi.target.scsi.sense.senseDataDescriptor; 2 3 4 import java.nio.ByteBuffer; 5 6 import org.jscsi.target.scsi.ISerializable; 7 8 9 /** 10 * Sense data descriptors are part of sense data and provide specific sense information. A given type of sense data 11 * descriptor shall be included in the sense data only when the information it contains is valid. 12 * 13 * @author Andreas Ergenzinger 14 */ 15 public abstract class SenseDataDescriptor implements ISerializable { 16 17 /** 18 * This value plus the length of all sub-class-specific fields equal the length in bytes of the serialized sense 19 * data descriptor. 20 */ 21 private static final int COMMON_FIELDS_LENGTH = 2; 22 23 /** 24 * The descriptor type field determines sense data descriptor's (non-abstract) class. 25 * 26 * @see SenseDataDescriptorType 27 */ 28 private final SenseDataDescriptorType descriptorType; 29 30 /** 31 * The ADDITIONAL LENGTH field indicates the number of sense data descriptor specific bytes that follow in the sense 32 * data descriptor. 33 */ 34 private final int additionalLength; 35 36 /** 37 * The abstract constructor. 38 * 39 * @param descriptorType determines the specific type of the sense data descriptor 40 * @param additionalLength the length in bytes of all additional fields 41 * @see #COMMON_FIELDS_LENGTH 42 */ 43 public SenseDataDescriptor (final SenseDataDescriptorType descriptorType, final int additionalLength) { 44 this.descriptorType = descriptorType; 45 this.additionalLength = additionalLength; 46 } 47 48 /** 49 * Serializes the fields common to all sense data descriptors. 50 * 51 * @param byteBuffer where the serialized fields will be stored 52 * @param index the position of the first byte of the sense data descriptor in the {@link ByteBuffer} 53 */ 54 private final void serializeCommonFields (final ByteBuffer byteBuffer, final int index) { 55 byteBuffer.position(index); 56 byteBuffer.put(descriptorType.getValue()); 57 byteBuffer.put((byte) additionalLength); 58 } 59 60 /** 61 * Serializes all fields which are not common to all sense data descriptors, which means those that are 62 * sub-type-specific. 63 * 64 * @param byteBuffer where the serialized fields will be stored 65 * @param index the position of the first byte of the sense data descriptor in the {@link ByteBuffer} 66 */ 67 protected abstract void serializeSpecificFields (ByteBuffer byteBuffer, final int index); 68 69 private final int getAdditionalLength () { 70 return additionalLength; 71 } 72 73 public void serialize (ByteBuffer byteBuffer, int index) { 74 serializeCommonFields(byteBuffer, index); 75 serializeSpecificFields(byteBuffer, index); 76 } 77 78 public int size () { 79 return COMMON_FIELDS_LENGTH + getAdditionalLength(); 80 } 81 }