| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| Debug |
|
| 3.5;3.5 |
| 1 | package org.jscsi.target.util; | |
| 2 | ||
| 3 | ||
| 4 | import java.nio.ByteBuffer; | |
| 5 | ||
| 6 | ||
| 7 | /** | |
| 8 | * This class provides static methods for printing the bytes of {@link ByteBuffer} objects. The individual values will | |
| 9 | * be printed in hexadecimal format in a tabular arrangement. | |
| 10 | * | |
| 11 | * @author Andreas Ergenzinger, University of Konstanz | |
| 12 | */ | |
| 13 | 0 | public class Debug { |
| 14 | ||
| 15 | /** | |
| 16 | * The number of bytes to print per line. | |
| 17 | */ | |
| 18 | private static final int BYTES_PER_LINE = 4; | |
| 19 | ||
| 20 | /** | |
| 21 | * Prints the <i>buffer</i> content to <code>System.out</code>. | |
| 22 | * | |
| 23 | * @param buffer contains the bytes to print | |
| 24 | */ | |
| 25 | public static void printByteBuffer (final ByteBuffer buffer) { | |
| 26 | 0 | System.out.println(byteBufferToString(buffer)); |
| 27 | 0 | } |
| 28 | ||
| 29 | /** | |
| 30 | * Returns a string containing the buffered values in the defined format. | |
| 31 | * | |
| 32 | * @param buffer contains the bytes to return in the {@link String} | |
| 33 | * @return a {@link String} with the values in tabular arrangement | |
| 34 | */ | |
| 35 | public static String byteBufferToString (final ByteBuffer buffer) { | |
| 36 | ||
| 37 | 0 | if (buffer == null) return "null"; |
| 38 | ||
| 39 | 0 | final int numberOfBytes = buffer.limit(); |
| 40 | ||
| 41 | 0 | final StringBuilder sb = new StringBuilder(); |
| 42 | 0 | buffer.position(0); |
| 43 | int value; | |
| 44 | 0 | for (int i = 1; i <= numberOfBytes; ++i) { |
| 45 | 0 | sb.append("0x"); |
| 46 | 0 | value = 255 & buffer.get(); |
| 47 | 0 | if (value < 16) sb.append("0"); |
| 48 | 0 | sb.append(Integer.toHexString(value)); |
| 49 | 0 | if (i % BYTES_PER_LINE == 0) |
| 50 | 0 | sb.append("\n"); |
| 51 | else | |
| 52 | 0 | sb.append(" "); |
| 53 | } | |
| 54 | ||
| 55 | 0 | return sb.toString(); |
| 56 | } | |
| 57 | ||
| 58 | } |