| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ScsiNameStringIdentifier |
|
| 1.75;1.75 |
| 1 | package org.jscsi.target.scsi.inquiry; | |
| 2 | ||
| 3 | ||
| 4 | import java.nio.ByteBuffer; | |
| 5 | ||
| 6 | ||
| 7 | /** | |
| 8 | * The null-terminated, null-padded (see 4.4.2) SCSI NAME STRING field contains a UTF-8 format string. The number of | |
| 9 | * bytes in the SCSI NAME STRING field (i.e., the value in the IDENTIFIER LENGTH field) shall be no larger than 256 and | |
| 10 | * shall be a multiple of four. | |
| 11 | * | |
| 12 | * @author Andreas Ergenzinger | |
| 13 | */ | |
| 14 | public class ScsiNameStringIdentifier extends Identifier { | |
| 15 | ||
| 16 | /** | |
| 17 | * The maximum {@link #size()} of this {@link Identifier}. | |
| 18 | */ | |
| 19 | private static final int MAX_SIZE = 256; | |
| 20 | ||
| 21 | /** | |
| 22 | * The identifying string. | |
| 23 | */ | |
| 24 | private final String nameString; | |
| 25 | ||
| 26 | /** | |
| 27 | * The logical unit name extension is a UTF-8 string containing no more than 16 hexadecimal digits. The logical unit | |
| 28 | * name extension is assigned by the SCSI target device vendor and shall be assigned so the logical unit name is | |
| 29 | * worldwide unique. | |
| 30 | * <p> | |
| 31 | * This Logical Unit Name Extension has been randomly generated.<br> | |
| 32 | * (0x493f51ba986f9800 = 5278027150164727808) | |
| 33 | */ | |
| 34 | private static final String logicalUnitNameExtension = "493f51ba986f9800"; | |
| 35 | ||
| 36 | 0 | public ScsiNameStringIdentifier (String targetName) { |
| 37 | ||
| 38 | /* | |
| 39 | * The SCSI NAME STRING field starts with either:<br> ...<br> c) The four UTF-8 characters "iqn." concatenated | |
| 40 | * with an iSCSI Name for an iSCSI-name based identifier (see iSCSI). If the ASSOCIATION field is set to 00b | |
| 41 | * (i.e., logical unit) and the SCSI NAME STRING field starts with the four UTF-8 characters "iqn.", the SCSI | |
| 42 | * NAME STRING field ends with the five UTF-8 characters ",L,0x" concatenated with 16 hexadecimal digits for the | |
| 43 | * logical unit name extension. | |
| 44 | */ | |
| 45 | 0 | nameString = targetName + ",L,0x" + logicalUnitNameExtension; |
| 46 | 0 | } |
| 47 | ||
| 48 | public void serialize (ByteBuffer byteBuffer, int index) { | |
| 49 | 0 | byteBuffer.position(index); |
| 50 | 0 | final int size = size();// this many bytes will be written |
| 51 | 0 | int stringLength = Math.min(nameString.length(), MAX_SIZE); |
| 52 | 0 | if (stringLength == MAX_SIZE) --stringLength;// at least one null character as padding |
| 53 | // copy string characters | |
| 54 | 0 | for (int i = 0; i < stringLength; ++i) |
| 55 | 0 | byteBuffer.put((byte) nameString.charAt(i)); |
| 56 | // add padding | |
| 57 | 0 | for (int i = 0; i < size - stringLength; ++i) |
| 58 | 0 | byteBuffer.put((byte) 0); |
| 59 | 0 | } |
| 60 | ||
| 61 | public int size () { | |
| 62 | 0 | return Math.min(nameString.length() + getNullTerminatedPaddingLength(), MAX_SIZE); |
| 63 | } | |
| 64 | ||
| 65 | /** | |
| 66 | * Returns the number of null-character padding bytes that have to be appended to the {@link #nameString} when used | |
| 67 | * in serialized form. | |
| 68 | * | |
| 69 | * @return the required number of null-character padding bytes | |
| 70 | */ | |
| 71 | private int getNullTerminatedPaddingLength () { | |
| 72 | 0 | return 4 - (nameString.length() % 4); |
| 73 | } | |
| 74 | } |