@Test public void testParseDouble() { double double1 = 2.0015E7; byte[] bytes1 = Double.toString(double1).getBytes(); assertEquals(double1, Bytes.parseDouble(bytes1, 0, bytes1.length), 0.0); double double2 = 1.345E-7; byte[] bytes2 = Double.toString(double2).getBytes(); assertEquals(double2, Bytes.parseDouble(bytes2, 0, bytes2.length), 0.0); double double3 = -1.345E-7; byte[] bytes3 = Double.toString(double3).getBytes(); assertEquals(double3, Bytes.parseDouble(bytes3, 0, bytes3.length), 0.0); double double4 = 4; byte[] bytes4 = Double.toString(double4).getBytes(); assertEquals(double4, Bytes.parseDouble(bytes4, 0, bytes4.length), 0.0); byte[] bytes5 = "0123456789.012345E012345".getBytes(); assertEquals(6789.012345E01, Bytes.parseDouble(bytes5, 6, 14), 0.0); }
public static double parseDouble(byte[] bytes, int start, int length) { if (bytes == null) { throw new NumberFormatException("String is null"); } if (length == 0) { throw new NumberFormatException("Empty byte array!"); } int offset = start; int end = start + length; while (offset < end && bytes[offset] == ' ') { offset++; } if (offset == end) { throw new NumberFormatException("blank byte array!"); } boolean sign = false; if (bytes[offset] == '-') { sign = true; offset++; } else if (bytes[offset] == '+') { offset++; } if (offset == end) { throw new NumberFormatException("the byte array only has a sign!"); } int mantSize = 0; int decicalOffset = -1; for (; offset < end; offset++) { if (!isDigit(bytes[offset])) { if ((bytes[offset] != '.') || (decicalOffset >= 0)) { break; } decicalOffset = mantSize; } mantSize++; } int exponentOffset = offset; offset -= mantSize; if (decicalOffset < 0) { decicalOffset = mantSize; } else { mantSize -= 1; } int fracExponent; if (mantSize > 18) { fracExponent = decicalOffset - 18; mantSize = 18; } else { fracExponent = decicalOffset - mantSize; } if (mantSize == 0) { return 0.0; } int frac1 = 0; for (; mantSize > 9; mantSize--) { int b = bytes[offset]; offset++; if (b == '.') { b = bytes[offset]; offset++; } frac1 = 10 * frac1 + (b - '0'); } int frac2 = 0; for (; mantSize > 0; mantSize--) { int b = bytes[offset]; offset++; if (b == '.') { b = bytes[offset]; offset++; } frac2 = 10 * frac2 + (b - '0'); } double fraction = (1.0e9 * frac1) + frac2; int exponent = 0; offset = exponentOffset; boolean expSign = false; if (offset < end) { if ((bytes[offset] != 'E') && (bytes[offset] != 'e')) { throw new NumberFormatException(new String(bytes, start, length)); } offset++; if (bytes[offset] == '-') { expSign = true; offset++; } else if (bytes[offset] == '+') { offset++; } for (; offset < end; offset++) { if (isDigit(bytes[offset])) { exponent = exponent * 10 + (bytes[offset] - '0'); } else { throw new NumberFormatException(new String(bytes, start, length)); } } } exponent = expSign ? (fracExponent - exponent) : (fracExponent + exponent); if (exponent < 0) { expSign = true; exponent = -exponent; } else { expSign = false; } if (exponent > maxExponent) { throw new NumberFormatException(new String(bytes, start, length)); } double dblExp = 1.0; for (int i = 0; exponent != 0; exponent >>= 1, i++) { if ((exponent & 01) == 01) { dblExp *= powersOf10[i]; } } fraction = (expSign) ? (fraction / dblExp) : (fraction * dblExp); return sign ? (-fraction) : fraction; }
Bytes { public static double parseDouble(byte[] bytes, int start, int length) { if (bytes == null) { throw new NumberFormatException("String is null"); } if (length == 0) { throw new NumberFormatException("Empty byte array!"); } int offset = start; int end = start + length; while (offset < end && bytes[offset] == ' ') { offset++; } if (offset == end) { throw new NumberFormatException("blank byte array!"); } boolean sign = false; if (bytes[offset] == '-') { sign = true; offset++; } else if (bytes[offset] == '+') { offset++; } if (offset == end) { throw new NumberFormatException("the byte array only has a sign!"); } int mantSize = 0; int decicalOffset = -1; for (; offset < end; offset++) { if (!isDigit(bytes[offset])) { if ((bytes[offset] != '.') || (decicalOffset >= 0)) { break; } decicalOffset = mantSize; } mantSize++; } int exponentOffset = offset; offset -= mantSize; if (decicalOffset < 0) { decicalOffset = mantSize; } else { mantSize -= 1; } int fracExponent; if (mantSize > 18) { fracExponent = decicalOffset - 18; mantSize = 18; } else { fracExponent = decicalOffset - mantSize; } if (mantSize == 0) { return 0.0; } int frac1 = 0; for (; mantSize > 9; mantSize--) { int b = bytes[offset]; offset++; if (b == '.') { b = bytes[offset]; offset++; } frac1 = 10 * frac1 + (b - '0'); } int frac2 = 0; for (; mantSize > 0; mantSize--) { int b = bytes[offset]; offset++; if (b == '.') { b = bytes[offset]; offset++; } frac2 = 10 * frac2 + (b - '0'); } double fraction = (1.0e9 * frac1) + frac2; int exponent = 0; offset = exponentOffset; boolean expSign = false; if (offset < end) { if ((bytes[offset] != 'E') && (bytes[offset] != 'e')) { throw new NumberFormatException(new String(bytes, start, length)); } offset++; if (bytes[offset] == '-') { expSign = true; offset++; } else if (bytes[offset] == '+') { offset++; } for (; offset < end; offset++) { if (isDigit(bytes[offset])) { exponent = exponent * 10 + (bytes[offset] - '0'); } else { throw new NumberFormatException(new String(bytes, start, length)); } } } exponent = expSign ? (fracExponent - exponent) : (fracExponent + exponent); if (exponent < 0) { expSign = true; exponent = -exponent; } else { expSign = false; } if (exponent > maxExponent) { throw new NumberFormatException(new String(bytes, start, length)); } double dblExp = 1.0; for (int i = 0; exponent != 0; exponent >>= 1, i++) { if ((exponent & 01) == 01) { dblExp *= powersOf10[i]; } } fraction = (expSign) ? (fraction / dblExp) : (fraction * dblExp); return sign ? (-fraction) : fraction; } }
Bytes { public static double parseDouble(byte[] bytes, int start, int length) { if (bytes == null) { throw new NumberFormatException("String is null"); } if (length == 0) { throw new NumberFormatException("Empty byte array!"); } int offset = start; int end = start + length; while (offset < end && bytes[offset] == ' ') { offset++; } if (offset == end) { throw new NumberFormatException("blank byte array!"); } boolean sign = false; if (bytes[offset] == '-') { sign = true; offset++; } else if (bytes[offset] == '+') { offset++; } if (offset == end) { throw new NumberFormatException("the byte array only has a sign!"); } int mantSize = 0; int decicalOffset = -1; for (; offset < end; offset++) { if (!isDigit(bytes[offset])) { if ((bytes[offset] != '.') || (decicalOffset >= 0)) { break; } decicalOffset = mantSize; } mantSize++; } int exponentOffset = offset; offset -= mantSize; if (decicalOffset < 0) { decicalOffset = mantSize; } else { mantSize -= 1; } int fracExponent; if (mantSize > 18) { fracExponent = decicalOffset - 18; mantSize = 18; } else { fracExponent = decicalOffset - mantSize; } if (mantSize == 0) { return 0.0; } int frac1 = 0; for (; mantSize > 9; mantSize--) { int b = bytes[offset]; offset++; if (b == '.') { b = bytes[offset]; offset++; } frac1 = 10 * frac1 + (b - '0'); } int frac2 = 0; for (; mantSize > 0; mantSize--) { int b = bytes[offset]; offset++; if (b == '.') { b = bytes[offset]; offset++; } frac2 = 10 * frac2 + (b - '0'); } double fraction = (1.0e9 * frac1) + frac2; int exponent = 0; offset = exponentOffset; boolean expSign = false; if (offset < end) { if ((bytes[offset] != 'E') && (bytes[offset] != 'e')) { throw new NumberFormatException(new String(bytes, start, length)); } offset++; if (bytes[offset] == '-') { expSign = true; offset++; } else if (bytes[offset] == '+') { offset++; } for (; offset < end; offset++) { if (isDigit(bytes[offset])) { exponent = exponent * 10 + (bytes[offset] - '0'); } else { throw new NumberFormatException(new String(bytes, start, length)); } } } exponent = expSign ? (fracExponent - exponent) : (fracExponent + exponent); if (exponent < 0) { expSign = true; exponent = -exponent; } else { expSign = false; } if (exponent > maxExponent) { throw new NumberFormatException(new String(bytes, start, length)); } double dblExp = 1.0; for (int i = 0; exponent != 0; exponent >>= 1, i++) { if ((exponent & 01) == 01) { dblExp *= powersOf10[i]; } } fraction = (expSign) ? (fraction / dblExp) : (fraction * dblExp); return sign ? (-fraction) : fraction; }  }
Bytes { public static double parseDouble(byte[] bytes, int start, int length) { if (bytes == null) { throw new NumberFormatException("String is null"); } if (length == 0) { throw new NumberFormatException("Empty byte array!"); } int offset = start; int end = start + length; while (offset < end && bytes[offset] == ' ') { offset++; } if (offset == end) { throw new NumberFormatException("blank byte array!"); } boolean sign = false; if (bytes[offset] == '-') { sign = true; offset++; } else if (bytes[offset] == '+') { offset++; } if (offset == end) { throw new NumberFormatException("the byte array only has a sign!"); } int mantSize = 0; int decicalOffset = -1; for (; offset < end; offset++) { if (!isDigit(bytes[offset])) { if ((bytes[offset] != '.') || (decicalOffset >= 0)) { break; } decicalOffset = mantSize; } mantSize++; } int exponentOffset = offset; offset -= mantSize; if (decicalOffset < 0) { decicalOffset = mantSize; } else { mantSize -= 1; } int fracExponent; if (mantSize > 18) { fracExponent = decicalOffset - 18; mantSize = 18; } else { fracExponent = decicalOffset - mantSize; } if (mantSize == 0) { return 0.0; } int frac1 = 0; for (; mantSize > 9; mantSize--) { int b = bytes[offset]; offset++; if (b == '.') { b = bytes[offset]; offset++; } frac1 = 10 * frac1 + (b - '0'); } int frac2 = 0; for (; mantSize > 0; mantSize--) { int b = bytes[offset]; offset++; if (b == '.') { b = bytes[offset]; offset++; } frac2 = 10 * frac2 + (b - '0'); } double fraction = (1.0e9 * frac1) + frac2; int exponent = 0; offset = exponentOffset; boolean expSign = false; if (offset < end) { if ((bytes[offset] != 'E') && (bytes[offset] != 'e')) { throw new NumberFormatException(new String(bytes, start, length)); } offset++; if (bytes[offset] == '-') { expSign = true; offset++; } else if (bytes[offset] == '+') { offset++; } for (; offset < end; offset++) { if (isDigit(bytes[offset])) { exponent = exponent * 10 + (bytes[offset] - '0'); } else { throw new NumberFormatException(new String(bytes, start, length)); } } } exponent = expSign ? (fracExponent - exponent) : (fracExponent + exponent); if (exponent < 0) { expSign = true; exponent = -exponent; } else { expSign = false; } if (exponent > maxExponent) { throw new NumberFormatException(new String(bytes, start, length)); } double dblExp = 1.0; for (int i = 0; exponent != 0; exponent >>= 1, i++) { if ((exponent & 01) == 01) { dblExp *= powersOf10[i]; } } fraction = (expSign) ? (fraction / dblExp) : (fraction * dblExp); return sign ? (-fraction) : fraction; }  static byte [] readByteArray(final DataInput in); static byte [] readByteArrayThrowsRuntime(final DataInput in); static void writeByteArray(final DataOutput out, final byte [] b); static void writeByteArray(final DataOutput out, final byte [] b,
      final int offset, final int length); static int writeByteArray(final byte [] tgt, final int tgtOffset,
      final byte [] src, final int srcOffset, final int srcLength); static int putBytes(byte[] tgtBytes, int tgtOffset, byte[] srcBytes,
      int srcOffset, int srcLength); static int putByte(byte[] bytes, int offset, byte b); static byte[] toBytes(ByteBuffer bb); static String toString(final byte [] b); static String toString(final byte [] b1,
                                String sep,
                                final byte [] b2); static String toString(final byte [] b, int off, int len); static String toStringBinary(final byte [] b); static String toStringBinary(ByteBuffer buf); static String toStringBinary(final byte [] b, int off, int len); static byte toBinaryFromHex(byte ch); static byte [] toBytesBinary(String in); static byte[] toBytes(String s); static byte[] toASCIIBytes(char[] chars); static byte [] toBytes(final boolean b); static boolean toBoolean(final byte [] b); static byte[] toBytes(long val); static long toLong(byte[] bytes); static long toLong(byte[] bytes, int offset); static long toLong(byte[] bytes, int offset, final int length); static int putLong(byte[] bytes, int offset, long val); static float toFloat(byte [] bytes); static float toFloat(byte [] bytes, int offset); static int putFloat(byte [] bytes, int offset, float f); static byte [] toBytes(final float f); static double toDouble(final byte [] bytes); static double toDouble(final byte [] bytes, final int offset); static int putDouble(byte [] bytes, int offset, double d); static byte [] toBytes(final double d); static byte[] toBytes(int val); static int toInt(byte[] bytes); static int toInt(byte[] bytes, int offset); static int toInt(byte[] bytes, int offset, final int length); static int putInt(byte[] bytes, int offset, int val); static byte[] toBytes(short val); static short toShort(byte[] bytes); static short toShort(byte[] bytes, int offset); static short toShort(byte[] bytes, int offset, final int length); static byte[] getBytes(ByteBuffer buf); static int putShort(byte[] bytes, int offset, short val); static byte[] toBytes(BigDecimal val); static BigDecimal toBigDecimal(byte[] bytes); static BigDecimal toBigDecimal(byte[] bytes, int offset); static BigDecimal toBigDecimal(byte[] bytes, int offset, final int length); static int putBigDecimal(byte[] bytes, int offset, BigDecimal val); static byte [] vintToBytes(final long vint); static byte[] vlongToBytes(long n); static long bytesToVint(final byte [] buffer); static long readVLong(final byte [] buffer, final int offset); static int compareTo(final byte [] left, final byte [] right); static int compareTo(byte[] buffer1, int offset1, int length1,
      byte[] buffer2, int offset2, int length2); static boolean equals(final byte [] left, final byte [] right); static boolean equals(final byte[] left, int leftOffset, int leftLen,
                               final byte[] right, int rightOffset, int rightLen); static boolean startsWith(byte[] bytes, byte[] prefix); static int hashCode(final byte [] b); static int hashCode(final byte [] b, final int length); static Integer mapKey(final byte [] b); static Integer mapKey(final byte [] b, final int length); static byte [] add(final byte [] a, final byte [] b); static byte [] add(final byte [] a, final byte [] b, final byte [] c); static byte [] head(final byte [] a, final int length); static byte [] tail(final byte [] a, final int length); static byte [] padHead(final byte [] a, final int length); static byte [] padTail(final byte [] a, final int length); static byte [][] split(final byte [] a, final byte [] b, final int num); static byte[][] splitPreserveAllTokens(byte[] str, char separatorChar, int[] target); static byte[][] splitPreserveAllTokens(byte[] str, int offset, int length, char separatorChar, int[] target); static byte[][] splitPreserveAllTokens(byte[] str, char separatorChar); static byte[][] splitPreserveAllTokens(byte[] str, int length, char separatorChar); static Iterable<byte[]> iterateOnSplits(
      final byte[] a, final byte[]b, final int num); static byte [][] toByteArrays(final String [] t); static byte [][] toByteArrays(final String column); static byte [][] toByteArrays(final byte [] column); static int binarySearch(byte [][]arr, byte []key, int offset,
      int length, RawComparator<byte []> comparator); static byte [] incrementBytes(byte[] value, long amount); static void writeStringFixedSize(final DataOutput out, String s,
      int size); static String readStringFixedSize(final DataInput in, int size); static int readFully(InputStream is, byte[] buffer, int offset, int length); static void skipFully(DataInput in, int len); static int parseInt(byte[] bytes, int start, int length); static int parseInt(byte[] bytes, int start, int length, int radix); static double parseDouble(byte[] bytes, int start, int length); }
Bytes { public static double parseDouble(byte[] bytes, int start, int length) { if (bytes == null) { throw new NumberFormatException("String is null"); } if (length == 0) { throw new NumberFormatException("Empty byte array!"); } int offset = start; int end = start + length; while (offset < end && bytes[offset] == ' ') { offset++; } if (offset == end) { throw new NumberFormatException("blank byte array!"); } boolean sign = false; if (bytes[offset] == '-') { sign = true; offset++; } else if (bytes[offset] == '+') { offset++; } if (offset == end) { throw new NumberFormatException("the byte array only has a sign!"); } int mantSize = 0; int decicalOffset = -1; for (; offset < end; offset++) { if (!isDigit(bytes[offset])) { if ((bytes[offset] != '.') || (decicalOffset >= 0)) { break; } decicalOffset = mantSize; } mantSize++; } int exponentOffset = offset; offset -= mantSize; if (decicalOffset < 0) { decicalOffset = mantSize; } else { mantSize -= 1; } int fracExponent; if (mantSize > 18) { fracExponent = decicalOffset - 18; mantSize = 18; } else { fracExponent = decicalOffset - mantSize; } if (mantSize == 0) { return 0.0; } int frac1 = 0; for (; mantSize > 9; mantSize--) { int b = bytes[offset]; offset++; if (b == '.') { b = bytes[offset]; offset++; } frac1 = 10 * frac1 + (b - '0'); } int frac2 = 0; for (; mantSize > 0; mantSize--) { int b = bytes[offset]; offset++; if (b == '.') { b = bytes[offset]; offset++; } frac2 = 10 * frac2 + (b - '0'); } double fraction = (1.0e9 * frac1) + frac2; int exponent = 0; offset = exponentOffset; boolean expSign = false; if (offset < end) { if ((bytes[offset] != 'E') && (bytes[offset] != 'e')) { throw new NumberFormatException(new String(bytes, start, length)); } offset++; if (bytes[offset] == '-') { expSign = true; offset++; } else if (bytes[offset] == '+') { offset++; } for (; offset < end; offset++) { if (isDigit(bytes[offset])) { exponent = exponent * 10 + (bytes[offset] - '0'); } else { throw new NumberFormatException(new String(bytes, start, length)); } } } exponent = expSign ? (fracExponent - exponent) : (fracExponent + exponent); if (exponent < 0) { expSign = true; exponent = -exponent; } else { expSign = false; } if (exponent > maxExponent) { throw new NumberFormatException(new String(bytes, start, length)); } double dblExp = 1.0; for (int i = 0; exponent != 0; exponent >>= 1, i++) { if ((exponent & 01) == 01) { dblExp *= powersOf10[i]; } } fraction = (expSign) ? (fraction / dblExp) : (fraction * dblExp); return sign ? (-fraction) : fraction; }  static byte [] readByteArray(final DataInput in); static byte [] readByteArrayThrowsRuntime(final DataInput in); static void writeByteArray(final DataOutput out, final byte [] b); static void writeByteArray(final DataOutput out, final byte [] b,
      final int offset, final int length); static int writeByteArray(final byte [] tgt, final int tgtOffset,
      final byte [] src, final int srcOffset, final int srcLength); static int putBytes(byte[] tgtBytes, int tgtOffset, byte[] srcBytes,
      int srcOffset, int srcLength); static int putByte(byte[] bytes, int offset, byte b); static byte[] toBytes(ByteBuffer bb); static String toString(final byte [] b); static String toString(final byte [] b1,
                                String sep,
                                final byte [] b2); static String toString(final byte [] b, int off, int len); static String toStringBinary(final byte [] b); static String toStringBinary(ByteBuffer buf); static String toStringBinary(final byte [] b, int off, int len); static byte toBinaryFromHex(byte ch); static byte [] toBytesBinary(String in); static byte[] toBytes(String s); static byte[] toASCIIBytes(char[] chars); static byte [] toBytes(final boolean b); static boolean toBoolean(final byte [] b); static byte[] toBytes(long val); static long toLong(byte[] bytes); static long toLong(byte[] bytes, int offset); static long toLong(byte[] bytes, int offset, final int length); static int putLong(byte[] bytes, int offset, long val); static float toFloat(byte [] bytes); static float toFloat(byte [] bytes, int offset); static int putFloat(byte [] bytes, int offset, float f); static byte [] toBytes(final float f); static double toDouble(final byte [] bytes); static double toDouble(final byte [] bytes, final int offset); static int putDouble(byte [] bytes, int offset, double d); static byte [] toBytes(final double d); static byte[] toBytes(int val); static int toInt(byte[] bytes); static int toInt(byte[] bytes, int offset); static int toInt(byte[] bytes, int offset, final int length); static int putInt(byte[] bytes, int offset, int val); static byte[] toBytes(short val); static short toShort(byte[] bytes); static short toShort(byte[] bytes, int offset); static short toShort(byte[] bytes, int offset, final int length); static byte[] getBytes(ByteBuffer buf); static int putShort(byte[] bytes, int offset, short val); static byte[] toBytes(BigDecimal val); static BigDecimal toBigDecimal(byte[] bytes); static BigDecimal toBigDecimal(byte[] bytes, int offset); static BigDecimal toBigDecimal(byte[] bytes, int offset, final int length); static int putBigDecimal(byte[] bytes, int offset, BigDecimal val); static byte [] vintToBytes(final long vint); static byte[] vlongToBytes(long n); static long bytesToVint(final byte [] buffer); static long readVLong(final byte [] buffer, final int offset); static int compareTo(final byte [] left, final byte [] right); static int compareTo(byte[] buffer1, int offset1, int length1,
      byte[] buffer2, int offset2, int length2); static boolean equals(final byte [] left, final byte [] right); static boolean equals(final byte[] left, int leftOffset, int leftLen,
                               final byte[] right, int rightOffset, int rightLen); static boolean startsWith(byte[] bytes, byte[] prefix); static int hashCode(final byte [] b); static int hashCode(final byte [] b, final int length); static Integer mapKey(final byte [] b); static Integer mapKey(final byte [] b, final int length); static byte [] add(final byte [] a, final byte [] b); static byte [] add(final byte [] a, final byte [] b, final byte [] c); static byte [] head(final byte [] a, final int length); static byte [] tail(final byte [] a, final int length); static byte [] padHead(final byte [] a, final int length); static byte [] padTail(final byte [] a, final int length); static byte [][] split(final byte [] a, final byte [] b, final int num); static byte[][] splitPreserveAllTokens(byte[] str, char separatorChar, int[] target); static byte[][] splitPreserveAllTokens(byte[] str, int offset, int length, char separatorChar, int[] target); static byte[][] splitPreserveAllTokens(byte[] str, char separatorChar); static byte[][] splitPreserveAllTokens(byte[] str, int length, char separatorChar); static Iterable<byte[]> iterateOnSplits(
      final byte[] a, final byte[]b, final int num); static byte [][] toByteArrays(final String [] t); static byte [][] toByteArrays(final String column); static byte [][] toByteArrays(final byte [] column); static int binarySearch(byte [][]arr, byte []key, int offset,
      int length, RawComparator<byte []> comparator); static byte [] incrementBytes(byte[] value, long amount); static void writeStringFixedSize(final DataOutput out, String s,
      int size); static String readStringFixedSize(final DataInput in, int size); static int readFully(InputStream is, byte[] buffer, int offset, int length); static void skipFully(DataInput in, int len); static int parseInt(byte[] bytes, int start, int length); static int parseInt(byte[] bytes, int start, int length, int radix); static double parseDouble(byte[] bytes, int start, int length); static final int SIZEOF_BOOLEAN; static final int SIZEOF_BYTE; static final int SIZEOF_CHAR; static final int SIZEOF_DOUBLE; static final int SIZEOF_FLOAT; static final int SIZEOF_INT; static final int SIZEOF_LONG; static final int SIZEOF_SHORT; static final int ESTIMATED_HEAP_TAX; static Comparator<byte []> BYTES_COMPARATOR; static RawComparator<byte []> BYTES_RAWCOMPARATOR; static final int LONG_BYTES; static final double powersOf10; }