Example usage for com.google.common.primitives Longs fromByteArray

List of usage examples for com.google.common.primitives Longs fromByteArray

Introduction

In this page you can find the example usage for com.google.common.primitives Longs fromByteArray.

Prototype

public static long fromByteArray(byte[] bytes) 

Source Link

Document

Returns the long value whose big-endian representation is stored in the first 8 bytes of bytes ; equivalent to ByteBuffer.wrap(bytes).getLong() .

Usage

From source file:org.apache.beam.sdk.io.gcp.spanner.OrderedCode.java

/**
 * Returns the next long item (encoded via
 * {@code writeSignedNumIncreasing(long)}) from its internal encoded byte
 * array store and removes the item from the store.
 *
 * @see #writeSignedNumIncreasing(long)//from  w  ww.ja va  2  s. c o m
 */
public long readSignedNumIncreasing() {
    if ((encodedArrays == null) || (encodedArrays.size() == 0)
            || ((encodedArrays.get(0)).length - firstArrayPosition < 1)) {
        throw new IllegalArgumentException("Invalid encoded byte array");
    }

    byte[] store = encodedArrays.get(0);

    long xorMask = ((store[firstArrayPosition] & 0x80) == 0) ? ~0L : 0L;
    // Store first byte as an int rather than a (signed) byte -- to avoid
    // accidental byte-to-int promotion later which would extend the byte's
    // sign bit (if any).
    int firstByte = (store[firstArrayPosition] & 0xff) ^ (int) (xorMask & 0xff);

    // Now calculate and test length, and set x to raw (unmasked) result.
    int len;
    long x;
    if (firstByte != 0xff) {
        len = 7 - log2Floor(firstByte ^ 0xff);
        if (store.length - firstArrayPosition < len) {
            throw new IllegalArgumentException("Invalid encoded byte array");
        }
        x = xorMask; // Sign extend using xorMask.
        for (int i = firstArrayPosition; i < firstArrayPosition + len; i++) {
            x = (x << 8) | (store[i] & 0xff);
        }
    } else {
        len = 8;
        if (store.length - firstArrayPosition < len) {
            throw new IllegalArgumentException("Invalid encoded byte array");
        }
        int secondByte = (store[firstArrayPosition + 1] & 0xff) ^ (int) (xorMask & 0xff);
        if (secondByte >= 0x80) {
            if (secondByte < 0xc0) {
                len = 9;
            } else {
                int thirdByte = (store[firstArrayPosition + 2] & 0xff) ^ (int) (xorMask & 0xff);
                if (secondByte == 0xc0 && thirdByte < 0x80) {
                    len = 10;
                } else {
                    // Either len > 10 or len == 10 and #bits > 63.
                    throw new IllegalArgumentException("Invalid encoded byte array");
                }
            }
            if (store.length - firstArrayPosition < len) {
                throw new IllegalArgumentException("Invalid encoded byte array");
            }
        }
        x = Longs.fromByteArray(
                Arrays.copyOfRange(store, firstArrayPosition + len - 8, firstArrayPosition + len));
    }

    x ^= LENGTH_TO_MASK[len]; // Remove spurious header bits.

    if (len != getSignedEncodingLength(x)) {
        throw new IllegalArgumentException("Invalid encoded byte array");
    }

    if ((store.length - firstArrayPosition - len) == 0) {
        // We are done with the first array.
        encodedArrays.remove(0);
        firstArrayPosition = 0;
    } else {
        firstArrayPosition = firstArrayPosition + len;
    }

    return x;
}

From source file:org.opendedup.collections.LongByteArrayMap.java

@Override
public void vanish(boolean index) throws IOException {
    WriteLock l = this.hashlock.writeLock();
    l.lock();// w w w.j a v a2 s .  c o m
    try {
        if (index) {
            this.iterInit();
            SparseDataChunk ck = this.nextValue(false);
            while (ck != null) {
                for (HashLocPair p : ck.getFingers()) {
                    boolean rm = DedupFileStore.removeRef(p.hash, Longs.fromByteArray(p.hashloc));
                    if (!rm)
                        SDFSLogger.getLog().warn("unable to remove orphaned reference "
                                + StringUtils.getHexString(p.hash) + " loc=" + Longs.fromByteArray(p.hashloc));
                }
                ck = this.nextValue(false);
            }
        }
        if (!this.isClosed())
            this.close();
        File f = new File(this.filePath);
        f.delete();

    } catch (Exception e) {
        throw new IOException(e);
    } finally {
        l.unlock();
    }
}

From source file:net.bither.bitherj.utils.Utils.java

public static long parseLongFromAddress(InetAddress address) {
    byte[] bytes = address.getAddress();
    if (bytes.length >= Longs.BYTES) {
        return Longs.fromByteArray(bytes);
    } else {/* w  ww  .java 2s . c  o  m*/
        return Ints.fromByteArray(bytes);
    }
}

From source file:io.warp10.continuum.geo.GeoDirectory.java

/**
 * Force indexing of some data by fetching them and forwarding them onto the data topic
 * This enables someone with a ReadToken to re-index historical data
 *//*  ww  w .  j a v a 2 s.  c  o  m*/
private void doIndex(Request baseRequest, HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException {

    baseRequest.setHandled(true);

    //
    // Extract parameters
    //

    String token = request.getParameter(Constants.HTTP_PARAM_TOKEN);
    String[] selectors = request.getParameterValues(Constants.HTTP_PARAM_SELECTOR);

    if (null == selectors) {
        throw new IOException("Missing selector.");
    }

    if (selectors.length != 1) {
        throw new IOException("Can only specify a single selector per request.");
    }

    if (null == token) {
        throw new IOException("Missing token.");
    }

    //
    // A token can only be used if it has subscribed to GTS in this index    
    //

    if (!this.subscriptions.containsKey(token)) {
        throw new IOException("The provided token does not have any current subscriptions in this index.");
    }

    //
    // INFO(hbs): this will trigger billing for every one subscribing to GTS in this index, we consider this a marginal case
    //

    //
    // TODO(hbs): Issue a signed fetch request which will retrieve GTSWrappers (to be implemented in Fetch)
    //

    URL url = new URL(this.fetchEndpoint);

    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    conn.setDoOutput(false);
    conn.setDoInput(true);
    conn.setChunkedStreamingMode(8192);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

    long now = System.currentTimeMillis();
    StringBuilder sb = new StringBuilder(Long.toHexString(now));
    sb.append(":");
    byte[] content = (Long.toString(now) + ":" + token).getBytes(Charsets.ISO_8859_1);
    long hash = SipHashInline.hash24(this.SIPHASH_FETCH_PSK[0], this.SIPHASH_FETCH_PSK[1], content, 0,
            content.length);
    sb.append(Long.toHexString(hash));
    conn.setRequestProperty(Constants.getHeader(Configuration.HTTP_HEADER_FETCH_SIGNATURE), sb.toString());

    //
    // Build query string
    //
    // token
    // selector
    // format=wrapper
    // now + timespan
    // or start/stop
    //

    sb = new StringBuilder();
    sb.append(WarpURLEncoder.encode(Constants.HTTP_PARAM_TOKEN, "UTF-8"));
    sb.append("=");
    sb.append(WarpURLEncoder.encode(token, "UTF-8"));
    sb.append("&");
    sb.append(WarpURLEncoder.encode(Constants.HTTP_PARAM_SELECTOR, "UTF-8"));
    sb.append("=");
    sb.append(WarpURLEncoder.encode(selectors[0], "UTF-8"));
    sb.append("&");
    sb.append(WarpURLEncoder.encode(Constants.HTTP_PARAM_FORMAT, "UTF-8"));
    sb.append("=");
    sb.append(WarpURLEncoder.encode("wrapper", "UTF-8"));

    if (null != request.getParameter(Constants.HTTP_PARAM_NOW)
            && null != request.getParameter(Constants.HTTP_PARAM_TIMESPAN)) {
        sb.append("&");
        sb.append(WarpURLEncoder.encode(Constants.HTTP_PARAM_NOW, "UTF-8"));
        sb.append("=");
        sb.append(WarpURLEncoder.encode(request.getParameter(Constants.HTTP_PARAM_NOW), "UTF-8"));
        sb.append("&");
        sb.append(WarpURLEncoder.encode(Constants.HTTP_PARAM_TIMESPAN, "UTF-8"));
        sb.append("=");
        sb.append(WarpURLEncoder.encode(request.getParameter(Constants.HTTP_PARAM_TIMESPAN), "UTF-8"));
    } else if (null != request.getParameter(Constants.HTTP_PARAM_START)
            && null != request.getParameter(Constants.HTTP_PARAM_STOP)) {
        sb.append("&");
        sb.append(WarpURLEncoder.encode(Constants.HTTP_PARAM_START, "UTF-8"));
        sb.append("=");
        sb.append(WarpURLEncoder.encode(request.getParameter(Constants.HTTP_PARAM_START), "UTF-8"));
        sb.append("&");
        sb.append(WarpURLEncoder.encode(Constants.HTTP_PARAM_STOP, "UTF-8"));
        sb.append("=");
        sb.append(WarpURLEncoder.encode(request.getParameter(Constants.HTTP_PARAM_STOP), "UTF-8"));
    } else {
        throw new IOException(
                "Missing parameters " + Constants.HTTP_PARAM_START + "/" + Constants.HTTP_PARAM_STOP + " or "
                        + Constants.HTTP_PARAM_NOW + "/" + Constants.HTTP_PARAM_TIMESPAN);
    }

    byte[] postDataBytes = sb.toString().getBytes(Charsets.UTF_8);

    conn.setRequestProperty("Content-Length", Long.toString(postDataBytes.length));
    conn.getOutputStream().write(postDataBytes);

    InputStream in = conn.getInputStream();

    BufferedReader br = new BufferedReader(new InputStreamReader(in));

    TDeserializer deserializer = new TDeserializer(new TCompactProtocol.Factory());

    long total = 0L;

    while (true) {
        String line = br.readLine();

        if (null == line) {
            break;
        }

        //
        // Extract MAC
        //

        byte[] data = line.getBytes(Charsets.US_ASCII);

        long mac = Longs.fromByteArray(Hex.decode(new String(data, 0, 16, Charsets.US_ASCII)));

        //
        // Extract content and decode it
        //

        data = OrderPreservingBase64.decode(data, 16, data.length - 16);

        //
        // Compute hash
        //

        hash = SipHashInline.hash24(this.SIPHASH_FETCH_PSK[0], this.SIPHASH_FETCH_PSK[1], data, 0, data.length);

        //
        // Ignore invalid GTSWrapper
        //

        if (hash != mac) {
            Map<String, String> labels = new HashMap<String, String>();
            labels.put(SensisionConstants.SENSISION_LABEL_GEODIR, this.name);
            Sensision.update(SensisionConstants.SENSISION_CLASS_GEODIR_FETCH_INVALIDMACS, labels, 1);
            continue;
        }

        //
        // Extract GTSWrapper
        //

        GTSWrapper wrapper = new GTSWrapper();

        try {
            deserializer.deserialize(wrapper, Arrays.copyOfRange(data, 16, data.length));
            total += wrapper.getCount();
        } catch (TException te) {
            Map<String, String> labels = new HashMap<String, String>();
            labels.put(SensisionConstants.SENSISION_LABEL_GEODIR, this.name);
            Sensision.update(SensisionConstants.SENSISION_CLASS_GEODIR_FETCH_FAILEDDESER, labels, 1);
            continue;
        }

        //
        // Check encoder base
        //

        if (0L != wrapper.getBase()) {
            Map<String, String> labels = new HashMap<String, String>();
            labels.put(SensisionConstants.SENSISION_LABEL_GEODIR, this.name);
            Sensision.update(SensisionConstants.SENSISION_CLASS_GEODIR_FETCH_INVALIDBASE, labels, 1);
            continue;
        }

        //
        // Now push the encoder to Kafka
        //

        pushData(wrapper);
    }

    br.close();

    conn.disconnect();

    //
    // Flush Kafka
    //

    pushData(null);

    response.setContentType("text/plain");
    response.setStatus(HttpServletResponse.SC_OK);
    response.getWriter().println(total);
}