Example usage for java.lang Byte MAX_VALUE

List of usage examples for java.lang Byte MAX_VALUE

Introduction

In this page you can find the example usage for java.lang Byte MAX_VALUE.

Prototype

byte MAX_VALUE

To view the source code for java.lang Byte MAX_VALUE.

Click Source Link

Document

A constant holding the maximum value a byte can have, 27-1.

Usage

From source file:org.deephacks.confit.serialization.ValueSerializationTest.java

@Test
public void test_random_write_read() throws Exception {
    for (int i = 0; i < 1000; i++) {
        ArrayList<Integer> ids = new ArrayList<>();
        ValueWriter writer = new ValueWriter();

        int stringId = new Random().nextInt();
        ids.add(stringId);/* w  w  w  . j  a v  a2 s  .  com*/
        String stringValue = RandomStringUtils.randomAlphanumeric(255);
        writer.putValue(stringId, stringValue);

        int booleanId = new Random().nextInt();
        ids.add(booleanId);
        boolean booleanValue = random(2) == 0;
        writer.putValue(booleanId, booleanValue);

        int longId = new Random().nextInt();
        ids.add(longId);
        long longValue = new Random().nextLong();
        writer.putValue(longId, longValue);

        int intId = new Random().nextInt();
        ids.add(intId);
        int intValue = new Random().nextInt();
        writer.putValue(intId, intValue);

        int shortId = new Random().nextInt();
        ids.add(shortId);
        short shortValue = (short) (new Random().nextInt() % Short.MAX_VALUE);
        writer.putValue(shortId, shortValue);

        int byteId = new Random().nextInt();
        ids.add(byteId);
        byte byteValue = (byte) (new Random().nextInt() % Byte.MAX_VALUE);
        writer.putValue(byteId, byteValue);

        int floatId = new Random().nextInt();
        ids.add(floatId);
        float floatValue = new Random().nextFloat();
        writer.putValue(floatId, floatValue);

        int doubleId = new Random().nextInt();
        ids.add(doubleId);
        double doubleValue = new Random().nextDouble();
        writer.putValue(doubleId, doubleValue);

        int referencesId = new Random().nextInt();
        ids.add(referencesId);
        Collection<String> instances = randomStrings();
        ReferenceList list = new ReferenceList(referencesId);
        list.getInstances().addAll(instances);
        writer.putValue(referencesId, list);

        int stringsId = new Random().nextInt();
        ids.add(stringsId);
        Collection<String> strings = randomStrings();
        writer.putValues(stringsId, strings, String.class);

        int booleansId = new Random().nextInt();
        ids.add(booleansId);
        Collection<Boolean> booleans = randomBooleans();
        writer.putValues(booleansId, booleans, Boolean.class);

        int longsId = new Random().nextInt();
        ids.add(longsId);
        Collection<Long> longs = randomLongs();
        writer.putValues(longsId, longs, Long.class);

        int intsId = new Random().nextInt();
        ids.add(intsId);
        Collection<Integer> integers = randomInts();
        writer.putValues(intsId, integers, Integer.class);

        int shortsId = new Random().nextInt();
        ids.add(shortsId);
        Collection<Short> shorts = randomShorts();
        writer.putValues(shortsId, shorts, Short.class);

        int bytesId = new Random().nextInt();
        ids.add(bytesId);
        Collection<Byte> bytes = randomBytes();
        writer.putValues(bytesId, bytes, Byte.class);

        int floatsId = new Random().nextInt();
        ids.add(floatsId);
        Collection<Float> floats = randomFloats();
        writer.putValues(floatsId, floats, Float.class);

        int doublesId = new Random().nextInt();
        ids.add(doublesId);
        Collection<Double> doubles = randomDoubles();
        writer.putValues(doublesId, doubles, Double.class);

        byte[] values = writer.write();

        ValueReader reader = new ValueReader(values);

        Integer[] idsArray = convert(reader.getIds(), Integer.class);
        assertThat(ids, hasItems(idsArray));

        assertEquals(booleanValue, reader.getValue(booleanId));

        assertEquals(longValue, reader.getValue(longId));

        assertEquals(intValue, reader.getValue(intId));

        assertEquals(shortValue, reader.getValue(shortId));

        assertEquals(byteValue, reader.getValue(byteId));

        assertEquals(doubleValue, reader.getValue(doubleId));

        assertEquals(floatValue, reader.getValue(floatId));

        ReferenceList referenceList = (ReferenceList) reader.getValue(referencesId);
        List<String> instanceList = referenceList.getInstances();
        assertThat(instances, hasItems((String[]) instanceList.toArray(new String[instanceList.size()])));

        Collection<String> collection = (Collection<String>) reader.getValue(stringsId);
        assertThat(strings, hasItems(collection.toArray(new String[collection.size()])));

        Boolean[] boolArray = ((Collection<Boolean>) reader.getValue(booleansId)).toArray(new Boolean[0]);
        assertThat(booleans, hasItems(boolArray));

        Long[] longArray = ((Collection<Long>) reader.getValue(longsId)).toArray(new Long[0]);
        assertThat(longs, hasItems(longArray));

        Integer[] intArray = ((Collection<Integer>) reader.getValue(intsId)).toArray(new Integer[0]);
        assertThat(integers, hasItems(intArray));

        Short[] shortArray = ((Collection<Short>) reader.getValue(shortsId)).toArray(new Short[0]);
        assertThat(shorts, hasItems(shortArray));

        Byte[] byteArray = ((Collection<Byte>) reader.getValue(bytesId)).toArray(new Byte[0]);
        assertThat(bytes, hasItems(byteArray));

        Double[] doubleArray = ((Collection<Double>) reader.getValue(doublesId)).toArray(new Double[0]);
        assertThat(doubles, hasItems(doubleArray));

        Float[] floatArray = ((Collection<Float>) reader.getValue(floatsId)).toArray(new Float[0]);
        assertThat(floats, hasItems(floatArray));
    }

}

From source file:it.unipmn.di.dcs.common.conversion.Base64.java

/**
 * Decode a Base64 data./*from   w  w  w  . ja va  2 s .c om*/
 *
 * Original method name is <em>decode</em>.
 */
public static byte[] Decode(char[] data, int off, int len) {
    char[] ibuf = new char[4];
    int ibufcount = 0;
    byte[] obuf = new byte[len / 4 * 3 + 3];
    int obufcount = 0;
    for (int i = off; i < off + len; i++) {
        char ch = data[i];
        if (ch == S_BASE64PAD || ch < S_DECODETABLE.length && S_DECODETABLE[ch] != Byte.MAX_VALUE) {
            ibuf[ibufcount++] = ch;
            if (ibufcount == ibuf.length) {
                ibufcount = 0;
                obufcount += Decode0(ibuf, obuf, obufcount);
            }
        }
    }
    if (obufcount == obuf.length)
        return obuf;
    byte[] ret = new byte[obufcount];
    System.arraycopy(obuf, 0, ret, 0, obufcount);
    return ret;
}

From source file:org.broadinstitute.gatk.utils.MathUtilsUnitTest.java

/**
 * Tests that we get unique values for the valid (non-null-producing) input space for {@link MathUtils#fastGenerateUniqueHashFromThreeIntegers(int, int, int)}.
 *///from w w  w.j  a  v  a  2 s  . c  om
@Test
public void testGenerateUniqueHashFromThreePositiveIntegers() {
    logger.warn("Executing testGenerateUniqueHashFromThreePositiveIntegers");

    final Set<Long> observedLongs = new HashSet<>();
    for (short i = 0; i < Byte.MAX_VALUE; i++) {
        for (short j = 0; j < Byte.MAX_VALUE; j++) {
            for (short k = 0; k < Byte.MAX_VALUE; k++) {
                final Long aLong = MathUtils.fastGenerateUniqueHashFromThreeIntegers(i, j, k);
                //System.out.println(String.format("%s, %s, %s: %s", i, j, k, aLong));
                Assert.assertTrue(observedLongs.add(aLong));
            }
        }
    }

    for (short i = Byte.MAX_VALUE; i <= Short.MAX_VALUE && i > 0; i += 128) {
        for (short j = Byte.MAX_VALUE; j <= Short.MAX_VALUE && j > 0; j += 128) {
            for (short k = Byte.MAX_VALUE; k <= Short.MAX_VALUE && k > 0; k += 128) {
                final Long aLong = MathUtils.fastGenerateUniqueHashFromThreeIntegers(i, j, k);
                // System.out.println(String.format("%s, %s, %s: %s", i, j, k, aLong));
                Assert.assertTrue(observedLongs.add(aLong));
            }
        }
    }
}

From source file:com.gargoylesoftware.htmlunit.WebClient3Test.java

/**
 * Ensure that response stream can be read more than one time.
 * @throws Exception if an error occurs/*  w ww.j ava  2 s  .c  o m*/
 */
@Test
public void readStreamTwice() throws Exception {
    final String html = "<html>\n" + "<body>\n" + "<iframe src='binaryFile.bin'></iframe>\n"
            + "<iframe src='foo.html'></iframe>\n" + "</body></html>";

    final MockWebConnection mockConnection = getMockWebConnection();
    final byte[] binaryContent = new byte[4818];
    final Random random = new Random();
    for (int i = 0; i < binaryContent.length; i++) {
        binaryContent[i] = (byte) (random.nextInt(Byte.MAX_VALUE));
    }
    mockConnection.setDefaultResponse(binaryContent, 200, "OK", "application/octet-stream");
    final URL urlFoo = new URL(getDefaultUrl(), "foo.html");
    mockConnection.setResponse(urlFoo, "<html></html>");

    final WebDriver driver = loadPage2(html);
    final WebElement iframe1 = driver.findElement(By.tagName("iframe"));
    if (driver instanceof HtmlUnitDriver) {
        final HtmlInlineFrame htmlUnitIFrame1 = (HtmlInlineFrame) toHtmlElement(iframe1);
        final WebResponse iframeWebResponse = htmlUnitIFrame1.getEnclosedPage().getWebResponse();
        byte[] receivedBytes = IOUtils.toByteArray(iframeWebResponse.getContentAsStream());
        receivedBytes = IOUtils.toByteArray(iframeWebResponse.getContentAsStream());
        assertArrayEquals(binaryContent, receivedBytes);
    }
}

From source file:Base64Utils.java

public static byte[] base64Decode(byte[] data, int off, int len) {
    char[] ibuf = new char[4];
    int ibufcount = 0;
    byte[] obuf = new byte[len / 4 * 3 + 3];
    int obufcount = 0;
    for (int i = off; i < off + len; i++) {
        char ch = (char) data[i];
        if (ch == S_BASE64PAD || ch < S_DECODETABLE.length && S_DECODETABLE[ch] != Byte.MAX_VALUE) {
            ibuf[ibufcount++] = ch;//w ww . j  av  a 2s  . c  o  m
            if (ibufcount == ibuf.length) {
                ibufcount = 0;
                obufcount += base64Decode0(ibuf, obuf, obufcount);
            }
        }
    }
    if (obufcount == obuf.length) {
        return obuf;
    }
    byte[] ret = new byte[obufcount];
    System.arraycopy(obuf, 0, ret, 0, obufcount);
    return ret;
}

From source file:org.echocat.jomon.net.cluster.channel.Message.java

@Override
public String toString() {
    final int length = getLength();
    final int offset = getOffset();
    final char[] trimmedData = encodeHex(copyOfRange(getData(), offset, offset + (length > 80 ? 80 : length)),
            false);//  w  ww. j a va 2 s.com
    // noinspection StringBufferReplaceableByString
    return new StringBuilder().append((int) getCommand() + ((int) Byte.MAX_VALUE * -1)).append(":")
            .append(trimmedData).append(length > 80 ? "..." : "").toString();
}

From source file:com.tesora.dve.common.TestDataGenerator.java

protected Object getColumnValue(ColumnMetadata cm) {
    Object cv = null;//w w w .j ava 2s  .com
    Calendar cal = Calendar.getInstance();

    switch (cm.getDataType()) {
    case Types.BIT:
    case Types.BOOLEAN:
        cv = Boolean.TRUE;
        break;
    case Types.BIGINT:
        cv = Long.MAX_VALUE;
        break;
    case Types.CHAR:
    case Types.VARCHAR:
        cv = StringUtils.left(baseString, cm.getSize());
        break;
    case Types.SMALLINT:
        cv = Short.MAX_VALUE;
        break;
    case Types.TINYINT:
        cv = Byte.MAX_VALUE;
        break;
    case Types.INTEGER:
        cv = Integer.MAX_VALUE;
        break;
    case Types.DOUBLE:
        cv = new Double(1234.5678); // TODO need to handle s,p
        break;
    case Types.FLOAT:
        cv = new Float(123.56); // TODO need to handle s,p
        break;
    case Types.DECIMAL:
        cv = new BigDecimal("12345.6789"); // TODO need to handle s,p
        break;
    case Types.DATE:
        cal.setTimeInMillis(123456789);
        cal.set(Calendar.HOUR_OF_DAY, 0);
        cal.set(Calendar.MINUTE, 0);
        cal.set(Calendar.SECOND, 0);
        cal.set(Calendar.MILLISECOND, 0);
        cv = cal.getTime();
        break;
    case Types.TIMESTAMP:
        cal.setTimeInMillis(123456789);
        cv = cal.getTime();
        break;
    case Types.TIME:
        cv = new Time(123456789);
        break;
    default:
        break;
    }

    return cv;
}

From source file:Base64.java

/**
 * Decode the base64 data./*from ww w  . j ava 2  s  . c  om*/
 * @param data The base64 encoded data to be decoded
 * @return The decoded data
 */
public static byte[] decode(String data) {
    int ibufcount = 0;
    int slen = data.length();
    char[] ibuf = new char[slen < BUF_SIZE + 3 ? slen : BUF_SIZE + 3];
    byte[] obuf = new byte[(slen >> 2) * 3 + 3];
    int obufcount = 0;
    int blen;

    for (int i = 0; i < slen; i += BUF_SIZE) {
        // buffer may contain unprocessed characters from previous step
        if (i + BUF_SIZE <= slen) {
            data.getChars(i, i + BUF_SIZE, ibuf, ibufcount);
            blen = BUF_SIZE + ibufcount;
        } else {
            data.getChars(i, slen, ibuf, ibufcount);
            blen = slen - i + ibufcount;
        }

        for (int j = ibufcount; j < blen; j++) {
            char ch = ibuf[j];
            if (ch == S_BASE64PAD || ch < S_DECODETABLE.length && S_DECODETABLE[ch] != Byte.MAX_VALUE) {
                ibuf[ibufcount++] = ch;
                // as soon as we have 4 chars process them
                if (ibufcount == 4) {
                    ibufcount = 0;
                    obufcount += decode0(ibuf, obuf, obufcount);
                }
            }
        }
    }
    if (obufcount == obuf.length) {
        return obuf;
    }
    byte[] ret = new byte[obufcount];
    System.arraycopy(obuf, 0, ret, 0, obufcount);
    return ret;
}

From source file:gedi.util.MathUtils.java

/**
 * Throws an exception if n is either a real or to big to be represented by a byte.
 * @param n/*from   w  w  w  . j  ava  2  s  .  com*/
 * @return
 */
public static byte byteValueExact(Number n) {
    if (n instanceof Byte)
        return n.byteValue();
    double d = n.doubleValue();
    long l = n.longValue();
    if (d == (double) l) {
        if (l >= Byte.MIN_VALUE && l <= Byte.MAX_VALUE)
            return (byte) l;
    }
    throw new NumberFormatException();
}

From source file:net.spfbl.core.Analise.java

public static synchronized void setAnaliseExpires(int expires) {
    if (expires < 0 || expires > Byte.MAX_VALUE) {
        Server.logError("invalid analise expires integer value '" + expires + "'.");
    } else {/* w w w.  ja v a  2 s .c  o m*/
        ANALISE_EXPIRES = (byte) expires;
    }
}