Return-Path: X-Original-To: apmail-commons-dev-archive@www.apache.org Delivered-To: apmail-commons-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 5119910B94 for ; Fri, 31 Jan 2014 22:45:31 +0000 (UTC) Received: (qmail 80063 invoked by uid 500); 31 Jan 2014 22:45:28 -0000 Delivered-To: apmail-commons-dev-archive@commons.apache.org Received: (qmail 79899 invoked by uid 500); 31 Jan 2014 22:45:28 -0000 Mailing-List: contact dev-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: "Commons Developers List" Delivered-To: mailing list dev@commons.apache.org Received: (qmail 79891 invoked by uid 99); 31 Jan 2014 22:45:28 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 31 Jan 2014 22:45:28 +0000 X-ASF-Spam-Status: No, hits=-0.0 required=5.0 tests=RCVD_IN_DNSWL_NONE,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of joerg.schaible@gmx.de designates 212.227.15.18 as permitted sender) Received: from [212.227.15.18] (HELO mout.gmx.net) (212.227.15.18) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 31 Jan 2014 22:45:20 +0000 Received: from floh.localnet ([109.192.110.248]) by mail.gmx.com (mrgmx001) with ESMTPSA (Nemesis) id 0McVGq-1Vrpwh2ndQ-00Hgi6 for ; Fri, 31 Jan 2014 23:45:00 +0100 From: =?ISO-8859-1?Q?J=F6rg?= Schaible To: Commons Developers List Subject: Fwd: svn commit: r1563259 - in /commons/proper/lang/trunk: pom.xml src/changes/changes.xml src/main/java/org/apache/commons/lang3/math/NumberUtils.java src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java Date: Fri, 31 Jan 2014 23:44:59 +0100 Message-ID: <2324677.McthvBTYLO@floh> Organization: N/A User-Agent: KMail/4.11.2 (Linux/3.10.25-gentoo; KDE/4.11.2; x86_64; ; ) MIME-Version: 1.0 Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" X-Provags-ID: V03:K0:vXQG9q/Yrpa2gyQXeKCXahvMl5ZypvZQp1aRvyIlnwbg0sOVGPi YZcMBBx2nzB97M0xTz3gDkpDavFPC/L7xrV8Ds1yJ3glL7J0LjXd3fV4xZ23K+EtoNqb0c6 cwV723xWBPgkGhy3245kFcokDPC4CPBiv4kR/hu8q7ZO8+EFEj+sLnS0GWOSp8xfH+uBQpF 8B4BV/XUaOgnrryrdS40Q== X-Virus-Checked: Checked by ClamAV on apache.org -1 Please revert, we have that already in org.apache.commons.lang3.Conversion --------------- Weitergeleitete Nachricht (Anfang) Betreff: svn commit: r1563259 - in /commons/proper/lang/trunk: pom.xml src/changes/changes.xml src/main/java/org/apache/commons/lang3/math/NumberUtils.java src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java Absender: djones-1oDqGaOF3Lkdnm+yROfE0A@public.gmane.org Datum: Fri, 31 Jan 2014 21:12:58 +0000 Newsgruppe: gmane.comp.jakarta.commons.scm Author: djones Date: Fri Jan 31 21:12:57 2014 New Revision: 1563259 URL: http://svn.apache.org/r1563259 Log: LANG-341: Please add number to byte[] methods. Suggested by Lilianne E. Blaze. Final patch from Vincent Ricard (with thanks to Henri Yandell). Modified: commons/proper/lang/trunk/pom.xml commons/proper/lang/trunk/src/changes/changes.xml commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/NumberUtils.java commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java Modified: commons/proper/lang/trunk/pom.xml URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/pom.xml?rev=1563259&r1=1563258&r2=1563259&view=diff ============================================================================== --- commons/proper/lang/trunk/pom.xml (original) +++ commons/proper/lang/trunk/pom.xml Fri Jan 31 21:12:57 2014 @@ -365,6 +365,9 @@ Travis Reeder + Vincent Ricard + + Antony Riley Modified: commons/proper/lang/trunk/src/changes/changes.xml URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/changes/changes.xml?rev=1563259&r1=1563258&r2=1563259&view=diff ============================================================================== --- commons/proper/lang/trunk/src/changes/changes.xml [utf-8] (original) +++ commons/proper/lang/trunk/src/changes/changes.xml [utf-8] Fri Jan 31 21:12:57 2014 @@ -22,6 +22,7 @@ + Please add number to byte[] methods org.apache.commons.lang3.reflect.FieldUtils.removeFinalModifier(Field) does not clean up after itself FastDateParser javadoc incorrectly states that SimpleDateFormat is used internally There should be a DifferenceBuilder with a ReflectionDifferenceBuilder implementation Modified: commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/NumberUtils.java URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/NumberUtils.java?rev=1563259&r1=1563258&r2=1563259&view=diff ============================================================================== --- commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/NumberUtils.java (original) +++ commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/math/NumberUtils.java Fri Jan 31 21:12:57 2014 @@ -21,6 +21,7 @@ import java.math.BigDecimal; import java.math.BigInteger; import org.apache.commons.lang3.StringUtils; +import org.apache.commons.lang3.Validate; /** *

Provides extra functionality for Java Number classes.

@@ -1433,4 +1434,239 @@ public class NumberUtils { return !allowSigns && foundDigit; } + // NOTE: toPrimitive(byte[]) method contents are taken from: + // http://svn.apache.org/repos/asf/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/DataInputStream.java + // toByteArray(primitive) method contents are taken from: + // http://svn.apache.org/repos/asf/harmony/enhanced/classlib/trunk/modules/luni/src/main/java/java/io/DataOutputStream.java + + + /** + *

+ * Converts a byte array to a {@code short}. + *

+ *

+ * The first two bytes of the + * array are converted from two's complement representation to a {@code short}. + * Any additional bytes are ignored. + *

+ *

+ * If the array is {@code null}, {@code 0} is returned. + *

+ * + * @param array the array to convert + * @return the converted value + * @throws IllegalArgumentException if the array has fewer than two elements + */ + public static short toShort(byte[] array) { + // Match toShort(String) + if (array == null) { + return 0; + } + + Validate.isTrue(array.length >= 2, "Byte array length too short: %d", array.length); + + return (short) (((array[0] & 0xff) << 8) | (array[1] & 0xff)); + } + + /** + *

+ * Converts a byte array to an {@code int}. + *

+ *

+ * The first four bytes of the + * array are converted from two's complement representation to an {@code int}. + * Any additional bytes are ignored. + *

+ *

+ * If the array is {@code null}, {@code 0} is returned. + *

+ * + * @param array the array to convert + * @return the converted value + * @throws IllegalArgumentException if the array has fewer than four elements + */ + public static int toInt(byte[] array) { + // Match toInt(String) + if (array == null) { + return 0; + } + + Validate.isTrue(array.length >= 4, "Byte array length too short: %d", array.length); + + return ((array[0] & 0xff) << 24) | ((array[1] & 0xff) << 16) | + ((array[2] & 0xff) << 8) | (array[3] & 0xff); + } + + + + /** + *

+ * Converts a byte array to a {@code long}. + *

+ *

+ * The first eight bytes of the + * array are converted from two's complement representation to a {@code long}. + * Any additional bytes are ignored. + *

+ *

+ * If the array is {@code null}, {@code 0} is returned. + *

+ * + * @param array the array to convert + * @return the converted value + * @throws IllegalArgumentException if the array has fewer than eight elements + */ + public static long toLong(byte[] array) { + // Match toLong(String) + if (array == null) { + return 0l; + } + + Validate.isTrue(array.length >= 8, "Byte array length too short: %d", array.length); + + int i1 = ((array[0] & 0xff) << 24) | ((array[1] & 0xff) << 16) | + ((array[2] & 0xff) << 8) | (array[3] & 0xff); + int i2 = ((array[4] & 0xff) << 24) | ((array[5] & 0xff) << 16) | + ((array[6] & 0xff) << 8) | (array[7] & 0xff); + + return ((i1 & 0xffffffffL) << 32) | (i2 & 0xffffffffL); + } + + + + /** + *

+ * Converts a byte array to a {@code float}. + *

+ *

+ * The first four bytes of the array are converted from IEEE 754 floating-point + * "single format" bit layout to a {@code float}. + *

+ *

+ * If the array is {@code null}, {@code 0} is returned. + *

+ * + * @param array the array to convert + * @return the converted value + * @throws IllegalArgumentException if the array has fewer than four elements + */ + public static float toFloat(byte[] array) { + // Match toFloat(String) + if (array == null) { + return 0.0f; + } + + Validate.isTrue(array.length >= 4, "Byte array length too short: %d", array.length); + + return Float.intBitsToFloat(toInt(array)); + } + + /** + *

+ * Converts a byte array to a {@code double}. + *

+ *

+ * The first eight bytes of the array are converted from IEEE 754 floating-point + * "double format" bit layout to a {@code double}. + *

+ *

+ * If the array is {@code null}, {@code 0} is returned. + *

+ * + * @param array the array to convert + * @return the converted value + * @throws IllegalArgumentException if the array has fewer than eight elements + */ + public static double toDouble(byte[] array) { + // Match toDouble(String) + if (array == null) { + return 0.0; + } + + Validate.isTrue(array.length >= 8, "Byte array length too short: %d", array.length); + + return Double.longBitsToDouble(toLong(array)); + } + + + /** + *

+ * Converts a {@code short} to a two-byte array in two's complement format. + *

+ * + * @param value the value to convert + * @return the byte array + */ + public static byte[] toByteArray(short value) { + byte[] buffer = new byte[2]; + buffer[0] = (byte) (value >> 8); + buffer[1] = (byte) value; + return buffer; + } + + /** + *

+ * Converts an {@code int} to a four-byte array in two's complement format. + *

+ * + * @param value the value to convert + * @return the byte array + */ + public static byte[] toByteArray(int value) { + byte[] buffer = new byte[4]; + buffer[0] = (byte) (value >> 24); + buffer[1] = (byte) (value >> 16); + buffer[2] = (byte) (value >> 8); + buffer[3] = (byte) value; + return buffer; + } + + + + /** + *

+ * Converts a {@code float} to a four-byte array in IEEE 754 floating- point + * "single format" bit layout. + *

+ * + * @param value the value to convert + * @return the byte array + */ + public static byte[] toByteArray(float value) { + return toByteArray(Float.floatToIntBits(value)); + } + + /** + *

+ * Converts a {@code long} to an eight-byte array in two's complement format. + *

+ * + * @param value the value to convert + * @return the byte array + */ + public static byte[] toByteArray(long value) { + byte[] buffer = new byte[8]; + buffer[0] = (byte) (value >> 56); + buffer[1] = (byte) (value >> 48); + buffer[2] = (byte) (value >> 40); + buffer[3] = (byte) (value >> 32); + buffer[4] = (byte) (value >> 24); + buffer[5] = (byte) (value >> 16); + buffer[6] = (byte) (value >> 8); + buffer[7] = (byte) value; + return buffer; + } + + /** + *

+ * Converts a {@code double} to an eight-byte array in IEEE 754 floating- point + * "double format" bit layout. + *

+ * + * @param value the value to convert + * @return the byte array + */ + public static byte[] toByteArray(double val) { + return toByteArray(Double.doubleToLongBits(val)); + } } Modified: commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java URL: http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java?rev=1563259&r1=1563258&r2=1563259&view=diff ============================================================================== --- commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java (original) +++ commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/math/NumberUtilsTest.java Fri Jan 31 21:12:57 2014 @@ -16,11 +16,7 @@ */ package org.apache.commons.lang3.math; -import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertNotNull; -import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; +import static org.junit.Assert.*; import java.lang.reflect.Constructor; import java.lang.reflect.Modifier; @@ -57,7 +53,7 @@ public class NumberUtilsTest { assertTrue("toInt(String) 1 failed", NumberUtils.toInt("12345") == 12345); assertTrue("toInt(String) 2 failed", NumberUtils.toInt("abc") == 0); assertTrue("toInt(empty) failed", NumberUtils.toInt("") == 0); - assertTrue("toInt(null) failed", NumberUtils.toInt(null) == 0); + assertTrue("toInt(null) failed", NumberUtils.toInt((String) null) == 0); } /** @@ -81,7 +77,7 @@ public class NumberUtilsTest { assertTrue("toLong(Long.MAX_VALUE) failed", NumberUtils.toLong(Long.MAX_VALUE+"") == Long.MAX_VALUE); assertTrue("toLong(Long.MIN_VALUE) failed", NumberUtils.toLong(Long.MIN_VALUE+"") == Long.MIN_VALUE); assertTrue("toLong(empty) failed", NumberUtils.toLong("") == 0l); - assertTrue("toLong(null) failed", NumberUtils.toLong(null) == 0l); + assertTrue("toLong(null) failed", NumberUtils.toLong((String) null) == 0l); } /** @@ -104,7 +100,7 @@ public class NumberUtilsTest { assertTrue("toFloat(Float.MAX_VALUE) failed", NumberUtils.toFloat(Float.MAX_VALUE+"") == Float.MAX_VALUE); assertTrue("toFloat(Float.MIN_VALUE) failed", NumberUtils.toFloat(Float.MIN_VALUE+"") == Float.MIN_VALUE); assertTrue("toFloat(empty) failed", NumberUtils.toFloat("") == 0.0f); - assertTrue("toFloat(null) failed", NumberUtils.toFloat(null) == 0.0f); + assertTrue("toFloat(null) failed", NumberUtils.toFloat((String) null) == 0.0f); } /** @@ -140,7 +136,7 @@ public class NumberUtilsTest { assertTrue("toDouble(Double.MAX_VALUE) failed", NumberUtils.toDouble(Double.MAX_VALUE+"") == Double.MAX_VALUE); assertTrue("toDouble(Double.MIN_VALUE) failed", NumberUtils.toDouble(Double.MIN_VALUE+"") == Double.MIN_VALUE); assertTrue("toDouble(empty) failed", NumberUtils.toDouble("") == 0.0d); - assertTrue("toDouble(null) failed", NumberUtils.toDouble(null) == 0.0d); + assertTrue("toDouble(null) failed", NumberUtils.toDouble((String) null) == 0.0d); } /** @@ -180,7 +176,7 @@ public class NumberUtilsTest { assertTrue("toShort(String) 1 failed", NumberUtils.toShort("12345") == 12345); assertTrue("toShort(String) 2 failed", NumberUtils.toShort("abc") == 0); assertTrue("toShort(empty) failed", NumberUtils.toShort("") == 0); - assertTrue("toShort(null) failed", NumberUtils.toShort(null) == 0); + assertTrue("toShort(null) failed", NumberUtils.toShort((String) null) == 0); } /** @@ -1399,5 +1395,94 @@ public class NumberUtilsTest { final float[] bF = new float[] { Float.NaN, 1.2f, Float.NaN, 3.7f, 27.0f, 42.0f, Float.NaN }; assertTrue(Float.isNaN(NumberUtils.max(bF))); } + + @Test + public void testByteArrayConversionArgChecking() throws Exception { + try { + NumberUtils.toShort(new byte[1]); + fail(); + } catch (IllegalArgumentException e) {} + + try { + NumberUtils.toInt(new byte[3]); + fail(); + } catch (IllegalArgumentException e) {} + + try { + NumberUtils.toLong(new byte[7]); + fail(); + } catch (IllegalArgumentException e) {} + + try { + NumberUtils.toFloat(new byte[3]); + fail(); + } catch (IllegalArgumentException e) {} + + try { + NumberUtils.toDouble(new byte[7]); + fail(); + } catch (IllegalArgumentException e) {} + } + + + @Test + public void testShortByteArrayConversion() { + // tests symmetry + assertEquals((short) 1, NumberUtils.toShort(NumberUtils.toByteArray((short) 1))); + + assertEquals(0, NumberUtils.toShort((byte[]) null)); + assertArrayEquals(new byte[] {(byte) 0x7F, (byte) 0xFF}, NumberUtils.toByteArray(Short.MAX_VALUE)); + assertArrayEquals(new byte[2], NumberUtils.toByteArray((short) 0)); + assertArrayEquals(new byte[] {(byte) 0xFF, (byte) 0xFF}, NumberUtils.toByteArray((short) -1)); + assertArrayEquals(new byte[] {(byte) 0x80, (byte) 0x00}, NumberUtils.toByteArray(Short.MIN_VALUE)); + } + + @Test + public void testIntByteArrayConversion() { + // tests symmetry + assertEquals(1, NumberUtils.toInt(NumberUtils.toByteArray(1))); + + assertEquals(0, NumberUtils.toInt((byte[]) null)); + assertArrayEquals(new byte[] {(byte) 0x7F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}, + NumberUtils.toByteArray(Integer.MAX_VALUE)); + assertArrayEquals(new byte[4], + NumberUtils.toByteArray(0)); + assertArrayEquals(new byte[] {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}, + NumberUtils.toByteArray(-1)); + assertArrayEquals(new byte[] {(byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00}, + NumberUtils.toByteArray(Integer.MIN_VALUE)); + } + + @Test + public void testLongByteArrayConversion() { + // tests symmetry + assertEquals(1l, NumberUtils.toLong(NumberUtils.toByteArray(1l))); + + assertEquals(0, NumberUtils.toLong((byte[]) null)); + assertArrayEquals(new byte[] {(byte) 0x7F, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}, + NumberUtils.toByteArray(Long.MAX_VALUE)); + assertArrayEquals(new byte[8], + NumberUtils.toByteArray(0L)); + assertArrayEquals(new byte[] {(byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF}, + NumberUtils.toByteArray(-1L)); + assertArrayEquals(new byte[] {(byte) 0x80, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00}, + NumberUtils.toByteArray(Long.MIN_VALUE)); + } + + @Test + public void testDoubleByteArrayConversion() { + // tests symmetry + assertEquals(0, NumberUtils.toDouble((byte[]) null), 0); + assertEquals(0, Double.compare(NumberUtils.toDouble(NumberUtils.toByteArray(1.1d)), 1.1d)); + } + + + @Test + public void testFloatByteArrayConversion() { + // tests symmetry + assertEquals(0, NumberUtils.toFloat((byte[]) null), 0); + assertEquals(0, Float.compare(NumberUtils.toFloat(NumberUtils.toByteArray(1.1f)), 1.1f)); + } + } --------------- Weitergeleitete Nachricht (Ende) --------------------------------------------------------------------- To unsubscribe, e-mail: dev-unsubscribe@commons.apache.org For additional commands, e-mail: dev-help@commons.apache.org