Example usage for java.lang Integer BYTES

List of usage examples for java.lang Integer BYTES

Introduction

In this page you can find the example usage for java.lang Integer BYTES.

Prototype

int BYTES

To view the source code for java.lang Integer BYTES.

Click Source Link

Document

The number of bytes used to represent an int value in two's complement binary form.

Usage

From source file:org.hobbit.core.rabbit.RabbitMQUtils.java

/**
 * Reads a byte array from the given buffer assuming that it is preceded by
 * an int value containing the length of the byte array.
 *
 * @param buffer//from  w  ww .  j av  a  2s.c  o m
 *            the buffer containing an int containing the length of the byte
 *            array followed by the byte array itself
 * @return the byte array or null if the given byte array is null
 */
public static byte[] readByteArray(ByteBuffer buffer) {
    if (buffer == null) {
        return null;
    } else {
        if (buffer.remaining() < Integer.BYTES) {
            return new byte[0];
        } else {
            int length = buffer.getInt();
            byte[] data = new byte[length];
            buffer.get(data, 0, data.length);
            return data;
        }
    }
}

From source file:org.ranksys.compression.codecs.lemire.FastPFORVBCODEC.java

@Override
public int[] co(int[] in, int offset, int len) {
    IntegerCODEC pfor;/*  w w w  .  j  ava 2 s.  co m*/
    try {
        if (pool == null) {
            synchronized (this) {
                if (pool == null) {
                    pool = new GenericObjectPool<>(new FastPFORFactory());
                }
            }
        }
        pfor = pool.borrowObject();
    } catch (Exception ex) {
        LOG.log(Level.SEVERE, null, ex);
        return null;
    }
    int[] out = new int[len + 1024];
    IntWrapper inputoffset = new IntWrapper(offset);
    IntWrapper outputoffset = new IntWrapper(1);
    pfor.compress(in, inputoffset, len, out, outputoffset);
    out[0] = outputoffset.get() - 1;
    out = Arrays.copyOf(out, outputoffset.get());
    pool.returnObject(pfor);
    add(len * Integer.BYTES, outputoffset.intValue() * Integer.BYTES);

    return out;
}

From source file:io.pravega.controller.store.stream.tables.HistoryRecord.java

public HistoryRecord(int epoch, final List<Integer> segments, final long scaleTime, final int offset,
        boolean partial) {
    this.epoch = epoch;
    this.offset = offset;
    this.length = FIXED_FIELDS_LENGTH + segments.size() * Integer.BYTES;
    this.segments = segments;
    this.scaleTime = scaleTime;
    this.partial = partial;
}

From source file:ru.jts_dev.common.packets.IncomingMessageWrapper.java

public final int readInt() {
    if (payload.readableBytes() < Integer.BYTES)
        throw new IndexOutOfBoundsException("At least 4 bytes must be readable in payload");

    return payload.readInt();
}

From source file:com.px100systems.util.serialization.DataStream.java

public void writeInteger(Integer data) {
    try {//from ww  w  . j a  v  a2s  .c  o m
        if (data == null)
            dos.writeInt(NULL);
        else {
            dos.writeInt(Integer.BYTES);
            dos.writeInt(data);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:org.onosproject.drivers.barefoot.TofinoPipelineProgrammable.java

private ByteBuffer nameBuffer(PiPipeconf pipeconf) {
    // Length of the name + name.
    String name = pipeconf.id().toString();
    return ByteBuffer.allocate(Integer.BYTES + name.length()).order(ByteOrder.LITTLE_ENDIAN)
            .putInt(name.length()).put(name.getBytes(StandardCharsets.UTF_8));
}

From source file:edu.umass.cs.reconfiguration.reconfigurationutils.ReconfigurationPacketDemultiplexer.java

@Override
public JSONObject processHeader(byte[] msg, NIOHeader header) {
    long t = System.nanoTime();
    int type;//from  w w w.j  ava 2s.co m
    JSONObject json = null;
    log.log(Level.FINEST, "{0} processHeader received message with header {1}", new Object[] { this, header });
    if (msg.length >= Integer.BYTES
            && ReconfigurationPacket.PacketType.intToType
                    .containsKey(type = ByteBuffer.wrap(msg, 0, 4).getInt())
            && JSONPacket.couldBeJSON(msg, Integer.BYTES)
            && type != PacketType.REPLICABLE_CLIENT_REQUEST.getInt()) {
        json = super.processHeader(msg, Integer.BYTES, header, true);
        if (json != null) {
            if (Util.oneIn(50))
                DelayProfiler.updateDelayNano("processHeader", t);
            return json;
        }
    } else if (!BYTEIFICATION && JSONPacket.couldBeJSON(msg)
            && (json = super.processHeader(msg, header, true)) != null) {
        return json;
    }
    // else prefix msg with addresses
    byte[] stamped = new byte[NIOHeader.BYTES + msg.length];
    ByteBuffer bbuf = ByteBuffer.wrap(stamped);
    bbuf.put(header.toBytes());
    bbuf.put(msg);
    if (Util.oneIn(50))
        DelayProfiler.updateDelayNano("processHeader", t);
    return new JSONMessenger.JSONObjectWrapper(stamped);
}

From source file:nl.salp.warcraft4j.util.DataTypeUtil.java

/**
 * Create a fixed-size 32-bit (4 byte) byte[] from an int value using a byte order.
 *
 * @param value     The value.//w  w  w.  jav a  2s  .  co m
 * @param byteOrder The byte order, using {@code ByteOrder#BIG_ENDIAN} when the byte order is {@code null}.
 *
 * @return The byte[].
 */
public static byte[] toByteArray(int value, ByteOrder byteOrder) {
    byte[] intArray = new byte[Integer.BYTES];
    ByteBuffer.wrap(intArray).order(Optional.ofNullable(byteOrder).orElse(ByteOrder.BIG_ENDIAN)).putInt(value);
    return intArray;
}

From source file:io.pravega.controller.store.stream.tables.HistoryRecord.java

/**
 * Return latest record in the history table.
 *
 * @param historyTable  history table/*w w  w. j ava 2  s . c o m*/
 * @param ignorePartial If latest entry is partial entry, if ignore is set then read the previous
 * @return returns the history record if it is found.
 */
public static Optional<HistoryRecord> readLatestRecord(final byte[] historyTable, boolean ignorePartial) {
    if (historyTable.length < PARTIAL_FIELDS_FIXED_LENGTH) {
        return Optional.empty();
    }

    final int backToTop = BitConverter.readInt(historyTable, historyTable.length - (Integer.BYTES));

    // ignore partial means if latest record is partial, read the previous record
    Optional<HistoryRecord> record = readRecord(historyTable, historyTable.length - backToTop, false);
    assert record.isPresent();

    if (ignorePartial && record.get().isPartial()) {
        return fetchPrevious(record.get(), historyTable);
    } else {
        return record;
    }
}

From source file:org.onosproject.drivers.barefoot.TofinoPipelineProgrammable.java

private ByteBuffer extensionBuffer(PiPipeconf pipeconf, ExtensionType extType) {
    if (!pipeconf.extension(extType).isPresent()) {
        log.warn("Missing extension {} in pipeconf {}", extType, pipeconf.id());
        throw new ExtensionException();
    }// w  ww  .  j a  va2 s.  c o m
    try {
        byte[] bytes = IOUtils.toByteArray(pipeconf.extension(extType).get());
        // Length of the extension + bytes.
        return ByteBuffer.allocate(Integer.BYTES + bytes.length).order(ByteOrder.LITTLE_ENDIAN)
                .putInt(bytes.length).put(bytes);
    } catch (IOException ex) {
        log.warn("Unable to read extension {} from pipeconf {}: {}", extType, pipeconf.id(), ex.getMessage());
        throw new ExtensionException();
    }
}