| 23 | import org.apache.kafka.common.KafkaException; |
| 24 | |
| 25 | public class Utils { |
| 26 | |
| 27 | private static final Pattern HOST_PORT_PATTERN = Pattern.compile("\\[?(.+?)\\]?:(\\d+)"); |
| 28 | |
| 29 | public static String NL = System.getProperty("line.separator"); |
| 30 | |
| 31 | /** |
| 32 | * Turn the given UTF8 byte array into a string |
| 33 | * |
| 34 | * @param bytes The byte array |
| 35 | * @return The string |
| 36 | */ |
| 37 | public static String utf8(byte[] bytes) { |
| 38 | try { |
| 39 | return new String(bytes, "UTF8"); |
| 40 | } catch (UnsupportedEncodingException e) { |
| 41 | throw new RuntimeException("This shouldn't happen.", e); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | /** |
| 46 | * Turn a string into a utf8 byte[] |
| 47 | * |
| 48 | * @param string The string |
| 49 | * @return The byte[] |
| 50 | */ |
| 51 | public static byte[] utf8(String string) { |
| 52 | try { |
| 53 | return string.getBytes("UTF8"); |
| 54 | } catch (UnsupportedEncodingException e) { |
| 55 | throw new RuntimeException("This shouldn't happen.", e); |
| 56 | } |
| 57 | } |
| 58 | |
| 59 | /** |
| 60 | * Read an unsigned integer from the current position in the buffer, incrementing the position by 4 bytes |
| 61 | * |
| 62 | * @param buffer The buffer to read from |
| 63 | * @return The integer read, as a long to avoid signedness |
| 64 | */ |
| 65 | public static long readUnsignedInt(ByteBuffer buffer) { |
| 66 | return buffer.getInt() & 0xffffffffL; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Read an unsigned integer from the given position without modifying the buffers position |
| 71 | * |
| 72 | * @param buffer the buffer to read from |
| 73 | * @param index the index from which to read the integer |
| 74 | * @return The integer read, as a long to avoid signedness |
| 75 | */ |
| 76 | public static long readUnsignedInt(ByteBuffer buffer, int index) { |
| 77 | return buffer.getInt(index) & 0xffffffffL; |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | * Read an unsigned integer stored in little-endian format from the {@link InputStream}. |
| 82 | * |
nothing calls this directly
no outgoing calls
no test coverage detected