Example usage for com.google.common.primitives Bytes indexOf

List of usage examples for com.google.common.primitives Bytes indexOf

Introduction

In this page you can find the example usage for com.google.common.primitives Bytes indexOf.

Prototype

public static int indexOf(byte[] array, byte[] target) 

Source Link

Document

Returns the start position of the first occurrence of the specified target within array , or -1 if there is no such occurrence.

Usage

From source file:org.apache.directory.server.dhcp.options.NulTerminatedStringOption.java

@Override
protected byte[] getStringData() {
    byte[] data = super.getStringData();
    int length = Bytes.indexOf(data, (byte) 0);
    if (length >= 0)
        return Arrays.copyOf(data, length);
    return data;/*  w w  w. j  ava2  s . c  om*/
}

From source file:mvm.rya.api.resolver.impl.CustomDatatypeResolver.java

@Override
public RyaType deserialize(byte[] bytes) throws RyaTypeResolverException {
    if (!deserializable(bytes)) {
        throw new RyaTypeResolverException("Bytes not deserializable");
    }/*  w w w. j  a  v  a2  s .c o m*/
    RyaType rt = newInstance();
    int length = bytes.length;
    int indexOfType = Bytes.indexOf(bytes, TYPE_DELIM_BYTE);
    if (indexOfType < 1) {
        throw new RyaTypeResolverException("Not a datatype literal");
    }
    String label = deserializeData(new String(bytes, 0, indexOfType));
    rt.setDataType(new URIImpl(new String(bytes, indexOfType + 1, (length - indexOfType) - 3)));
    rt.setData(label);
    return rt;
}

From source file:org.apache.rya.api.resolver.impl.CustomDatatypeResolver.java

@Override
public RyaType deserialize(final byte[] bytes) throws RyaTypeResolverException {
    if (!deserializable(bytes)) {
        throw new RyaTypeResolverException("Bytes not deserializable");
    }//from  w w  w . j a va  2s.  c o m
    final RyaType rt = newInstance();
    final int length = bytes.length;
    final int indexOfType = Bytes.indexOf(bytes, TYPE_DELIM_BYTE);
    if (indexOfType < 1) {
        throw new RyaTypeResolverException("Not a datatype literal");
    }
    final String label = deserializeData(new String(bytes, 0, indexOfType, StandardCharsets.UTF_8));
    rt.setDataType(new URIImpl(
            new String(bytes, indexOfType + 1, (length - indexOfType) - 3, StandardCharsets.UTF_8)));
    rt.setData(label);
    return rt;
}

From source file:org.apache.rya.periodic.notification.serialization.BindingSetSerDe.java

private BindingSet fromBytes(final byte[] bsBytes) {
    try {/*from w  w  w. j  ava 2s . c om*/
        final int firstIndex = Bytes.indexOf(bsBytes, DELIM_BYTE);
        final byte[] varOrderBytes = Arrays.copyOf(bsBytes, firstIndex);
        final byte[] bsBytesNoVarOrder = Arrays.copyOfRange(bsBytes, firstIndex + 1, bsBytes.length);
        final VariableOrder varOrder = new VariableOrder(
                new String(varOrderBytes, StandardCharsets.UTF_8).split(";"));
        return getBindingSet(varOrder, bsBytesNoVarOrder);
    } catch (final Exception e) {
        log.trace("Unable to deserialize BindingSet: " + bsBytes);
        return new QueryBindingSet();
    }
}

From source file:com.google.gerrit.server.notedb.ChangeRevisionNote.java

private static String parsePushCert(Change.Id changeId, byte[] bytes, MutableInteger p)
        throws ConfigInvalidException {
    if (RawParseUtils.match(bytes, p.value, CERT_HEADER) < 0) {
        return null;
    }/*from  w  w  w.  j  a v a2 s  .co m*/
    int end = Bytes.indexOf(bytes, END_SIGNATURE);
    if (end < 0) {
        throw ChangeNotes.parseException(changeId, "invalid push certificate in note");
    }
    int start = p.value;
    p.value = end + END_SIGNATURE.length;
    return new String(bytes, start, p.value, UTF_8);
}

From source file:org.loggo.search.iterators.HostAndApplicationFilter.java

@Override
public boolean accept(Key k, Value v) {
    ByteSequence bs = k.getColumnQualifierData();
    byte[] bytes = bs.getBackingArray();
    int i = Bytes.indexOf(bytes, SEPARATOR);
    if (i < 0) {
        return false;
    }/* w  w  w  . j  ava  2s  . co  m*/
    boolean matchesApps = apps.length == 0;
    for (byte[] app : apps) {
        if (COMPARATOR.compare(app, 0, app.length, bytes, 0, i) == 0) {
            matchesApps = true;
            break;
        }
    }

    boolean matchesHost = hosts.length == 0;
    for (byte[] host : hosts) {
        if (COMPARATOR.compare(host, 0, host.length, bytes, i + SEPARATOR.length,
                bs.length() - i - SEPARATOR.length) == 0) {
            matchesHost = true;
            break;
        }
    }
    return matchesApps && matchesHost;
}

From source file:org.apache.directory.server.dhcp.io.DhcpMessageDecoder.java

@Nonnull
private static String decodeString(@Nonnull ByteBuffer buffer, @Nonnegative int len) throws IOException {
    byte[] bytes = decodeBytes(buffer, len);
    // find zero-terminator
    int slen = Bytes.indexOf(bytes, (byte) 0);
    if (slen < 0)
        slen = bytes.length;/*w  w w  .  j  a  va 2  s . c o m*/
    return new String(bytes, 0, slen, Charsets.ISO_8859_1);
}

From source file:co.cask.tephra.persist.CommitMarkerCodec.java

private boolean isMarkerValid() {
    // rawKey should have the expected length and the matching bytes should start at index 0
    return rawKey.getLength() == KEY_BYTES.length && Bytes.indexOf(rawKey.getData(), KEY_BYTES) == 0;
}

From source file:com.bazaarvoice.dropwizard.caching.memcached.CachedResponseTranscoder.java

@Override
public CachedResponse decode(CachedData d) {
    try {/*from   w  ww . j  a  va2 s .  c o  m*/
        byte[] cachedData = d.getData();
        int headerSeparatorIndex = Bytes.indexOf(cachedData, HEADER_SEPARATOR);

        if (headerSeparatorIndex < 0) {
            return null;
        }

        BufferedReader headerReader = new BufferedReader(new InputStreamReader(
                new ByteArrayInputStream(cachedData, 0, headerSeparatorIndex), Charsets.US_ASCII));

        int statusCode = readStatusCode(headerReader);
        MultivaluedMap<String, String> headers = readHeaders(headerReader);
        byte[] responseContent = Arrays.copyOfRange(cachedData, headerSeparatorIndex + HEADER_SEPARATOR.length,
                cachedData.length);

        return new CachedResponse(statusCode, headers, responseContent);
    } catch (IOException ex) {
        throw new RuntimeException("Corrupted cache entry", ex);
    }
}

From source file:mvm.rya.api.resolver.triple.impl.WholeRowTripleResolver.java

@Override
public RyaStatement deserialize(TABLE_LAYOUT table_layout, TripleRow tripleRow)
        throws TripleRowResolverException {
    try {/*from  w w  w.j  a  v  a 2  s  .  c  o m*/
        assert tripleRow != null && table_layout != null;
        byte[] row = tripleRow.getRow();
        int firstIndex = Bytes.indexOf(row, DELIM_BYTE);
        int secondIndex = Bytes.lastIndexOf(row, DELIM_BYTE);
        int typeIndex = Bytes.indexOf(row, TYPE_DELIM_BYTE);
        byte[] first = Arrays.copyOf(row, firstIndex);
        byte[] second = Arrays.copyOfRange(row, firstIndex + 1, secondIndex);
        byte[] third = Arrays.copyOfRange(row, secondIndex + 1, typeIndex);
        byte[] type = Arrays.copyOfRange(row, typeIndex, row.length);
        byte[] columnFamily = tripleRow.getColumnFamily();
        boolean contextExists = columnFamily != null && columnFamily.length > 0;
        RyaURI context = (contextExists) ? (new RyaURI(new String(columnFamily))) : null;
        byte[] columnQualifier = tripleRow.getColumnQualifier();
        String qualifier = columnQualifier != null && columnQualifier.length > 0 ? new String(columnQualifier)
                : null;
        Long timestamp = tripleRow.getTimestamp();
        byte[] columnVisibility = tripleRow.getColumnVisibility();
        byte[] value = tripleRow.getValue();

        switch (table_layout) {
        case SPO: {
            byte[] obj = Bytes.concat(third, type);
            return new RyaStatement(new RyaURI(new String(first)), new RyaURI(new String(second)),
                    RyaContext.getInstance().deserialize(obj), context, qualifier, columnVisibility, value,
                    timestamp);
        }
        case PO: {
            byte[] obj = Bytes.concat(second, type);
            return new RyaStatement(new RyaURI(new String(third)), new RyaURI(new String(first)),
                    RyaContext.getInstance().deserialize(obj), context, qualifier, columnVisibility, value,
                    timestamp);
        }
        case OSP: {
            byte[] obj = Bytes.concat(first, type);
            return new RyaStatement(new RyaURI(new String(second)), new RyaURI(new String(third)),
                    RyaContext.getInstance().deserialize(obj), context, qualifier, columnVisibility, value,
                    timestamp);
        }
        }
    } catch (RyaTypeResolverException e) {
        throw new TripleRowResolverException(e);
    }
    throw new TripleRowResolverException(
            "TripleRow[" + tripleRow + "] with Table layout[" + table_layout + "] is not deserializable");
}