List of usage examples for io.netty.buffer ByteBuf toString
public abstract String toString(int index, int length, Charset charset);
From source file:org.apache.tajo.util.NumberUtil.java
License:Apache License
/** * Parses the byte buffer argument as if it was an int value and returns the * result. Throws NumberFormatException if the byte array does not represent an * int quantity. The second argument specifies the radix to use when parsing * the value./*from ww w . j a v a2 s. c o m*/ * * @param radix the base to use for conversion. * @return the value represented by the argument * @throws NumberFormatException if the argument could not be parsed as an int quantity. */ public static int parseInt(ByteBuf bytes, int start, int length, int radix) { if (!PlatformDependent.hasUnsafe()) { return parseInt(bytes.array(), start, length); } if (bytes == null) { throw new NumberFormatException("String is null"); } if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) { throw new NumberFormatException("Invalid radix: " + radix); } if (length == 0 || bytes.writerIndex() < start + length) { throw new NumberFormatException("Empty string or Invalid buffer!"); } long memoryAddress = bytes.memoryAddress(); int offset = start; boolean negative = PlatformDependent.getByte(memoryAddress + start) == '-'; if (negative || PlatformDependent.getByte(memoryAddress + start) == '+') { offset++; if (length == 1) { throw new NumberFormatException(bytes.toString(start, length, Charset.defaultCharset())); } } return parseIntInternal(bytes, memoryAddress, start, length, offset, radix, negative); }
From source file:org.apache.tajo.util.NumberUtil.java
License:Apache License
/** * @param bytes the string byte buffer * @param memoryAddress the offheap memory address * @param start/*from w w w.jav a 2 s . c o m*/ * @param length * @param radix the base to use for conversion. * @param offset the starting position after the sign (if exists) * @param radix the base to use for conversion. * @param negative whether the number is negative. * @return the value represented by the argument * @throws NumberFormatException if the argument could not be parsed as an int quantity. */ private static int parseIntInternal(ByteBuf bytes, long memoryAddress, int start, int length, int offset, int radix, boolean negative) { byte separator = '.'; int max = Integer.MIN_VALUE / radix; int result = 0, end = start + length; while (offset < end) { int digit = digit(PlatformDependent.getByte(memoryAddress + offset++), radix); if (digit == -1) { if (PlatformDependent.getByte(memoryAddress + offset - 1) == separator) { // We allow decimals and will return a truncated integer in that case. // Therefore we won't throw an exception here (checking the fractional // part happens below.) break; } throw new NumberFormatException(bytes.toString(start, length, Charset.defaultCharset())); } if (max > result) { throw new NumberFormatException(bytes.toString(start, length, Charset.defaultCharset())); } int next = result * radix - digit; if (next > result) { throw new NumberFormatException(bytes.toString(start, length, Charset.defaultCharset())); } result = next; } // This is the case when we've encountered a decimal separator. The fractional // part will not change the number, but we will verify that the fractional part // is well formed. while (offset < end) { int digit = digit(PlatformDependent.getByte(memoryAddress + offset++), radix); if (digit == -1) { throw new NumberFormatException(bytes.toString(start, length, Charset.defaultCharset())); } } if (!negative) { result = -result; if (result < 0) { throw new NumberFormatException(bytes.toString(start, length, Charset.defaultCharset())); } } return result; }
From source file:org.apache.tajo.util.NumberUtil.java
License:Apache License
/** * Parses the byte buffer argument as if it was an long value and returns the * result. Throws NumberFormatException if the string does not represent an * long quantity. The second argument specifies the radix to use when parsing * the value.//from w w w .jav a 2 s . c o m * * @param bytes the string byte buffer * @param start * @param length a UTF-8 encoded string representation of a long quantity. * @param radix the base to use for conversion. * @return the value represented by the argument * @throws NumberFormatException if the argument could not be parsed as an long quantity. */ public static long parseLong(ByteBuf bytes, int start, int length, int radix) { if (!PlatformDependent.hasUnsafe()) { return parseInt(bytes.array(), start, length); } if (bytes == null) { throw new NumberFormatException("String is null"); } if (radix < Character.MIN_RADIX || radix > Character.MAX_RADIX) { throw new NumberFormatException("Invalid radix: " + radix); } if (length == 0 || bytes.writerIndex() < start + length) { throw new NumberFormatException("Empty string or Invalid buffer!"); } long memoryAddress = bytes.memoryAddress(); int offset = start; boolean negative = PlatformDependent.getByte(memoryAddress + start) == '-'; if (negative || PlatformDependent.getByte(memoryAddress + start) == '+') { offset++; if (length == 1) { throw new NumberFormatException(bytes.toString(start, length, Charset.defaultCharset())); } } return parseLongInternal(bytes, memoryAddress, start, length, offset, radix, negative); }
From source file:org.apache.tajo.util.NumberUtil.java
License:Apache License
/** * /** Parses the byte buffer argument as if it was an long value and returns the * result. Throws NumberFormatException if the string does not represent an * long quantity. The second argument specifies the radix to use when parsing * the value./*from w w w. ja va 2s . c o m*/ * * @param bytes the string byte buffer * @param memoryAddress the offheap memory address * @param start * @param length a UTF-8 encoded string representation of a long quantity. * @param offset the starting position after the sign (if exists) * @param radix the base to use for conversion. * @param negative whether the number is negative. * @return the value represented by the argument * @throws NumberFormatException if the argument could not be parsed as an long quantity. */ private static long parseLongInternal(ByteBuf bytes, long memoryAddress, int start, int length, int offset, int radix, boolean negative) { byte separator = '.'; long max = Long.MIN_VALUE / radix; long result = 0, end = start + length; while (offset < end) { int digit = digit(PlatformDependent.getByte(memoryAddress + offset++), radix); if (digit == -1 || max > result) { if (PlatformDependent.getByte(memoryAddress + offset - 1) == separator) { // We allow decimals and will return a truncated integer in that case. // Therefore we won't throw an exception here (checking the fractional // part happens below.) break; } throw new NumberFormatException(bytes.toString(start, length, Charset.defaultCharset())); } long next = result * radix - digit; if (next > result) { throw new NumberFormatException(bytes.toString(start, length, Charset.defaultCharset())); } result = next; } // This is the case when we've encountered a decimal separator. The fractional // part will not change the number, but we will verify that the fractional part // is well formed. while (offset < end) { int digit = digit(PlatformDependent.getByte(memoryAddress + offset++), radix); if (digit == -1) { throw new NumberFormatException(bytes.toString(start, length, Charset.defaultCharset())); } } if (!negative) { result = -result; if (result < 0) { throw new NumberFormatException(bytes.toString(start, length, Charset.defaultCharset())); } } return result; }
From source file:org.dcache.xrootd.protocol.messages.AuthenticationRequest.java
License:Open Source License
public static String deserializeProtocol(ByteBuf buffer) { String protocol = buffer.toString(buffer.readerIndex(), 4, US_ASCII).trim(); /* toString does not advance the index */ buffer.readerIndex(buffer.readerIndex() + 4); return protocol; }
From source file:org.dcache.xrootd.protocol.messages.LoginRequest.java
License:Open Source License
public LoginRequest(ByteBuf buffer) { super(buffer, kXR_login); int pos = buffer.indexOf(8, 16, (byte) 0); // User name is padded with '\0' if (pos > -1) { username = buffer.toString(8, pos - 8, US_ASCII); } else {/*from w w w . j a va 2s. co m*/ username = buffer.toString(8, 8, US_ASCII); } pid = buffer.getInt(4); capver = buffer.getUnsignedByte(18); role = buffer.getUnsignedByte(19); int tlen = buffer.getInt(20); token = buffer.toString(24, tlen, US_ASCII); }
From source file:org.dcache.xrootd.protocol.messages.MvRequest.java
License:Open Source License
public MvRequest(ByteBuf buffer) { super(buffer, kXR_mv); int dlen = buffer.getInt(20); int end = 24 + dlen; int psep = buffer.indexOf(24, end, (byte) 0x20); if (psep == -1) { throw new IllegalArgumentException("kXR_mv needs two paths!"); }// w ww . j a va2 s . c o m String source = buffer.toString(24, psep - 24, US_ASCII); String target = buffer.toString(psep + 1, end - (psep + 1), US_ASCII); int osep = source.indexOf("?"); if (osep > -1) { sourcePath = source.substring(0, osep); sourceOpaque = source.substring(osep + 1); } else { sourcePath = source; } osep = target.indexOf("?"); if (osep > -1) { targetPath = target.substring(0, osep); targetOpaque = target.substring(osep + 1); } else { targetPath = target; } }
From source file:org.dcache.xrootd.protocol.messages.PathRequest.java
License:Open Source License
private void setPathAndOpaque(ByteBuf buffer, int begin, int length) { int end = begin + length; int pos = buffer.indexOf(begin, end, XrootdProtocol.OPAQUE_DELIMITER); if (pos > -1) { setPath(buffer.toString(begin, pos - begin, US_ASCII)); setOpaque(buffer.toString(pos + 1, end - (pos + 1), US_ASCII)); } else {/* www.j a v a 2 s .c o m*/ setPath(buffer.toString(begin, end - begin, US_ASCII)); setOpaque(""); } }
From source file:org.dcache.xrootd.protocol.messages.PrepareRequest.java
License:Open Source License
public PrepareRequest(ByteBuf buffer) { super(buffer, kXR_prepare); options = buffer.getUnsignedShort(4); priority = buffer.getUnsignedShort(5); int plen = buffer.getInt(20); int end = 24 + plen; plist = buffer.toString(24, end - 24, US_ASCII).split("\n"); }
From source file:org.dcache.xrootd.protocol.messages.QueryRequest.java
License:Open Source License
public QueryRequest(ByteBuf buffer) { super(buffer, kXR_query); reqcode = buffer.getUnsignedShort(4); fhandle = buffer.getInt(8);/*from w w w. j av a 2 s . c om*/ int alen = buffer.getInt(20); /* The protocol spec doesn't state anything about trailing zeros in args, * however the xrdfs client sends zero terminated paths. */ args = NULL_CHARACTER.trimTrailingFrom(buffer.toString(24, alen, US_ASCII)); }