Java Code Examples for com.google.common.primitives.Bytes#indexOf()

The following examples show how to use com.google.common.primitives.Bytes#indexOf() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: StateTest.java    From zetasketch with Apache License 2.0 6 votes vote down vote up
/** Verify that the data is aliased when possible. */
@Test
@SuppressWarnings("boxing")
public void parseSparseData_AliasesArray() throws IOException {
  byte[] bytes = build(HyperLogLogPlusUniqueStateProto.newBuilder()
      .setSparseData(ByteString.copyFrom(new byte[] {1, 2, 3})));
  CodedInputStream stream = CodedInputStream.newInstance(bytes);
  stream.enableAliasing(true);
  State state = new State();
  state.parse(stream);

  // Preconditions.
  int idx = Bytes.indexOf(bytes, new byte[] {1, 2, 3});
  assertThat(idx).isAtLeast(0);
  assertThat(state.sparseData.toByteArray()).isEqualTo(new byte[] {1, 2, 3});

  // Modify the bytes, make sure that it is reflected in builder.
  bytes[idx + 1] = 4;
  assertThat(state.sparseData.toByteArray()).isEqualTo(new byte[] {1, 4, 3});
}
 
Example 2
Source File: ContentDetector.java    From news-crawl with Apache License 2.0 6 votes vote down vote up
public int getFirstMatch(byte[] content) {
    byte[] beginning = content;
    if (content.length > maxOffset) {
        beginning = Arrays.copyOfRange(content, 0, maxOffset);
    }
    OR:
    for (int i = 0; i < clues.length; i++) {
        byte[][] group = clues[i];
        for (byte[] clue : group) {
            if (Bytes.indexOf(beginning, clue) == -1)
                continue OR;
        }
        // success, all members of one group matched
        return i;
    }
    return -1;
}
 
Example 3
Source File: UpgradeUtilities.java    From big-c with Apache License 2.0 6 votes vote down vote up
/**
 * Corrupt the specified file.  Some random bytes within the file
 * will be changed to some random values.
 *
 * @throws IllegalArgumentException if the given file is not a file
 * @throws IOException if an IOException occurs while reading or writing the file
 */
public static void corruptFile(File file,
    byte[] stringToCorrupt,
    byte[] replacement) throws IOException {
  Preconditions.checkArgument(replacement.length == stringToCorrupt.length);
  if (!file.isFile()) {
    throw new IllegalArgumentException(
        "Given argument is not a file:" + file);
  }
  byte[] data = Files.toByteArray(file);
  int index = Bytes.indexOf(data, stringToCorrupt);
  if (index == -1) {
    throw new IOException(
        "File " + file + " does not contain string " +
        new String(stringToCorrupt));
  }

  for (int i = 0; i < stringToCorrupt.length; i++) {
    data[index + i] = replacement[i];
  }
  Files.write(data, file);
}
 
Example 4
Source File: UpgradeUtilities.java    From hadoop with Apache License 2.0 6 votes vote down vote up
/**
 * Corrupt the specified file.  Some random bytes within the file
 * will be changed to some random values.
 *
 * @throws IllegalArgumentException if the given file is not a file
 * @throws IOException if an IOException occurs while reading or writing the file
 */
public static void corruptFile(File file,
    byte[] stringToCorrupt,
    byte[] replacement) throws IOException {
  Preconditions.checkArgument(replacement.length == stringToCorrupt.length);
  if (!file.isFile()) {
    throw new IllegalArgumentException(
        "Given argument is not a file:" + file);
  }
  byte[] data = Files.toByteArray(file);
  int index = Bytes.indexOf(data, stringToCorrupt);
  if (index == -1) {
    throw new IOException(
        "File " + file + " does not contain string " +
        new String(stringToCorrupt));
  }

  for (int i = 0; i < stringToCorrupt.length; i++) {
    data[index + i] = replacement[i];
  }
  Files.write(data, file);
}
 
Example 5
Source File: DataStatisticsStoreImpl.java    From geowave with Apache License 2.0 6 votes vote down vote up
public static InternalDataStatistics<?, ?, ?> setFields(
    final GeoWaveMetadata entry,
    final InternalDataStatistics<?, ?, ?> basicStats,
    final short adapterId) {
  if (basicStats != null) {
    basicStats.setAdapterId(adapterId);
    final int index = Bytes.indexOf(entry.getPrimaryId(), (byte) 0);
    if ((index > 0) && (index < (entry.getPrimaryId().length - 1))) {
      basicStats.setType(
          new BaseStatisticsType(Arrays.copyOfRange(entry.getPrimaryId(), 0, index)));

      basicStats.setExtendedId(
          StringUtils.stringFromBinary(
              Arrays.copyOfRange(entry.getPrimaryId(), index + 1, entry.getPrimaryId().length)));
    } else {
      basicStats.setType(new BaseStatisticsType(entry.getPrimaryId()));
    }
    final byte[] visibility = entry.getVisibility();
    if (visibility != null) {
      basicStats.setVisibility(visibility);
    }
  }
  return basicStats;
}
 
Example 6
Source File: DhcpMessageDecoder.java    From dhcp4j with Apache License 2.0 5 votes vote down vote up
@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;
    return new String(bytes, 0, slen, Charsets.ISO_8859_1);
}
 
Example 7
Source File: Protocl.java    From wechatbot-xposed with Apache License 2.0 5 votes vote down vote up
public static byte[] Unpack(byte[] buffer,readerData reader){
    ByteBuffer buf = ByteBuffer.allocate(102400);
    long length = buffer.length;
    int biao = 0;
    if (length <= ConstHeaderLength+ConstSaveDataLength+ConstFooterLength) {
        return buffer;
    }
    if (Bytes.indexOf(buffer, ConstHeader.getBytes()) == -1 || Bytes.indexOf(buffer, ConstFooter.getBytes()) == -1){
        return buffer;
    }
    int start = Bytes.indexOf(buffer, ConstHeader.getBytes());
    if (start == -1) {
        return buffer;
    }
    int end =  Bytes.indexOf(Arrays.copyOfRange(buffer,start+ConstHeaderLength,buffer.length), ConstFooter.getBytes());
    if (end == -1) {
        return buffer;
    }
    end += start + ConstHeaderLength;
    biao = start + ConstHeaderLength;
    int messageLength = (int)bytes2int(Arrays.copyOfRange(buffer,biao,biao+ConstSaveDataLength));
    if (end-start-ConstHeaderLength-ConstSaveDataLength != messageLength) {
        return Arrays.copyOfRange(buffer,end+ConstFooterLength,buffer.length);
    }
    biao += ConstSaveDataLength;

    reader.setReader(Arrays.copyOfRange(buffer,biao,biao+messageLength));
    biao += messageLength + ConstFooterLength;
    if (biao == length) {
        return new byte[]{};
    }

    return Arrays.copyOfRange(buffer,biao,buffer.length);
}
 
Example 8
Source File: NulTerminatedStringOption.java    From dhcp4j with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 9
Source File: Http2SniffInterceptor.java    From NetBare with MIT License 5 votes vote down vote up
@Override
protected void intercept(@NonNull HttpRequestChain chain, @NonNull ByteBuffer buffer,
                         int index) throws IOException {
    if (index == 0) {
        HttpRequest request = chain.request();
        if (mLog == null) {
            mLog = new NetBareXLog(request.protocol(), request.ip(), request.port());
        }
        // HTTP2 is forces to use SSL connection.
        if (request.isHttps()) {
            if (buffer.hasRemaining() && Bytes.indexOf(buffer.array(),
                    Http2.CONNECTION_PREFACE) == buffer.position()) {
                mLog.i("Send a connection preface to remote server.");
                request.session().protocol = HttpProtocol.HTTP_2;
                if (buffer.remaining() == Http2.CONNECTION_PREFACE.length) {
                    // Skip preface frame data.
                    mCallback.onRequest(request, buffer);
                    return;
                } else {
                    ByteBuffer prefaceBuffer = ByteBuffer.allocate(Http2.CONNECTION_PREFACE.length);
                    prefaceBuffer.put(Http2.CONNECTION_PREFACE);
                    prefaceBuffer.flip();
                    mCallback.onRequest(request, prefaceBuffer);
                    // The remaining data continues.
                    buffer.position(buffer.position() + Http2.CONNECTION_PREFACE.length);
                }
            }
        }
    }
    if (buffer.hasRemaining()) {
        chain.process(buffer);
    }
}
 
Example 10
Source File: NulTerminatedStringOption.java    From dhcp4j with Apache License 2.0 5 votes vote down vote up
@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;
}
 
Example 11
Source File: FileUploadsUtil.java    From raml-module-builder with Apache License 2.0 5 votes vote down vote up
private static MimeMultipart split(byte[] pattern, byte[] input, int sizeLimit) {

    MimeMultipart mmp = new MimeMultipart();
    int start = 0;
    int pos   = Bytes.indexOf(input, pattern);
    int size  = input.length;
    int entryCount = 0;
    ByteBuffer buffer = ByteBuffer.wrap(input);

    while(pos != -1 && start < size){

      int end = pos + pattern.length;
      if(entryCount != 0){
        //dont add the boundary itself - which is what you have in the first iteration
        buffer.position(start);

        //not a copy but points to the buffer
        //used for the indexOf functionality to keep checking
        //further on in the buffer - current pos -> end of buffer
        byte[] tmpBuffer = buffer.slice().array();

        //set limit - now that limit is set re-slice to only get the needed
        //area -
        buffer.limit(end);

        try {
          MimeBodyPart mbp = new MimeBodyPart(new InternetHeaders(), buffer.slice().array());
          mmp.addBodyPart(mbp);
        } catch (MessagingException e) {
          log.error(e.getMessage(), e);
        }
        pos = Bytes.indexOf(tmpBuffer, pattern);
      }
      entryCount++;
      start = end;
    }
    return mmp;
  }
 
Example 12
Source File: CustomDatatypeResolver.java    From rya with Apache License 2.0 5 votes vote down vote up
@Override
public RyaType deserialize(final byte[] bytes) throws RyaTypeResolverException {
    if (!deserializable(bytes)) {
        throw new RyaTypeResolverException("Bytes not deserializable");
    }
    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");
    }
    String data = deserializeData(new String(bytes, 0, indexOfType, StandardCharsets.UTF_8));
    rt.setDataType(SimpleValueFactory.getInstance().createIRI(new String(bytes, indexOfType + 1, (length - indexOfType) - 3, StandardCharsets.UTF_8)));
    if (RDF.LANGSTRING.equals(rt.getDataType())) {
        final int langDelimiterPos = data.lastIndexOf(LiteralLanguageUtils.LANGUAGE_DELIMITER);
        final String parsedData = data.substring(0, langDelimiterPos);
        final String language = data.substring(langDelimiterPos + 1, data.length());
        if (language != null && Literals.isValidLanguageTag(language)) {
            rt.setLanguage(language);
        } else {
            rt.setLanguage(LiteralLanguageUtils.UNDETERMINED_LANGUAGE);
        }
        data = parsedData;
    }
    rt.setData(data);
    return rt;
}
 
Example 13
Source File: HttpHeaderSeparateInterceptor.java    From NetBare with MIT License 4 votes vote down vote up
@Override
protected void intercept(@NonNull HttpRequestChain chain, @NonNull ByteBuffer buffer, int index)
        throws IOException {
    if (mLog == null) {
        mLog = new NetBareXLog(Protocol.TCP, chain.request().ip(), chain.request().port());
    }
    if (mRequestHeaderHandled) {
        chain.process(buffer);
        return;
    }
    if (!buffer.hasRemaining()) {
        chain.process(buffer);
        return;
    }
    buffer = mergeRequestBuffer(buffer);
    // Check the part end line.
    int headerEndIndex = Bytes.indexOf(buffer.array(), NetBareUtils.PART_END_BYTES);
    if (headerEndIndex < 0) {
        mLog.w("Http request header data is not enough.");
        // Not found the part end line, maybe the data is not enough, wait next buffer coming.
        pendRequestBuffer(buffer);
    } else {
        mRequestHeaderHandled = true;
        // Check whether the header and the body are in the same buffer.
        boolean hasMultiPart = headerEndIndex < buffer.limit() - NetBareUtils.PART_END_BYTES.length;
        if (hasMultiPart) {
            mLog.w("Multiple http request parts are founded.");
            // Separate the header and body data to two buffers.
            int offset = headerEndIndex + NetBareUtils.PART_END_BYTES.length;
            ByteBuffer headerBuffer = ByteBuffer.wrap(buffer.array(), buffer.position(), offset);
            // Allocate a new buffer, do not use wrap, different buffers will share the same array.
            ByteBuffer bodyBuffer = ByteBuffer.allocate(buffer.limit() - offset);
            bodyBuffer.put(buffer.array(), offset, buffer.limit() - offset);
            bodyBuffer.flip();
            chain.process(headerBuffer);
            chain.process(bodyBuffer);
        } else {
            chain.process(buffer);
        }
    }
}
 
Example 14
Source File: PhoenixResultSet.java    From phoenix with Apache License 2.0 4 votes vote down vote up
/**
 * Separate the actual cell data from the serialized list of dynamic column PColumns and
 * return the deserialized list of dynamic column PColumns for the current row
 * @return Deserialized list of dynamic column PColumns or null if there are no dynamic columns
 */
private List<PColumn> getDynColsListAndSeparateFromActualData() {
    Cell base = this.currentRow.getValue(0);
    final byte[] valueArray = CellUtil.cloneValue(base);
    // We inserted the known byte array before appending the serialized list of dynamic columns
    final byte[] anchor = Arrays.copyOf(DYN_COLS_METADATA_CELL_QUALIFIER,
            DYN_COLS_METADATA_CELL_QUALIFIER.length);
    // Reverse the arrays to find the last occurrence of the sub-array in the value array
    ArrayUtils.reverse(valueArray);
    ArrayUtils.reverse(anchor);
    final int pos = valueArray.length - Bytes.indexOf(valueArray, anchor);
    // There are no dynamic columns to process so return immediately
    if (pos >= valueArray.length) {
        return null;
    }
    ArrayUtils.reverse(valueArray);

    // Separate the serialized list of dynamic column PColumns from the actual cell data
    byte[] actualCellDataBytes = Arrays.copyOfRange(valueArray, 0,
            pos - DYN_COLS_METADATA_CELL_QUALIFIER.length);
    ImmutableBytesWritable actualCellData = new ImmutableBytesWritable(actualCellDataBytes);
    ImmutableBytesWritable key = new ImmutableBytesWritable();
    currentRow.getKey(key);
    // Store only the actual cell data as part of the current row
    this.currentRow = new TupleProjector.ProjectedValueTuple(key.get(), key.getOffset(),
            key.getLength(), base.getTimestamp(),
            actualCellData.get(), actualCellData.getOffset(), actualCellData.getLength(), 0);

    byte[] dynColsListBytes = Arrays.copyOfRange(valueArray, pos, valueArray.length);
    List<PColumn> dynCols = new ArrayList<>();
    try {
        List<PTableProtos.PColumn> dynColsProtos = DynamicColumnMetaDataProtos
                .DynamicColumnMetaData.parseFrom(dynColsListBytes).getDynamicColumnsList();
        for (PTableProtos.PColumn colProto : dynColsProtos) {
            dynCols.add(PColumnImpl.createFromProto(colProto));
        }
    } catch (InvalidProtocolBufferException e) {
        return null;
    }
    return dynCols;
}
 
Example 15
Source File: WSDL20ProcessorImpl.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
@Override
public boolean canProcess(byte[] content) {
    return Bytes.indexOf(content, WSDL20_NAMESPACE.getBytes()) > 0;
}
 
Example 16
Source File: WSDL11ProcessorImpl.java    From carbon-apimgt with Apache License 2.0 4 votes vote down vote up
public boolean canProcess(byte[] content) {
    return Bytes.indexOf(content, WSDL11_NAMESPACE.getBytes()) > 0;
}
 
Example 17
Source File: CommitMarkerCodec.java    From phoenix-tephra with Apache License 2.0 4 votes vote down vote up
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;
}
 
Example 18
Source File: ResourceConfiguration.java    From android-arscblamer with Apache License 2.0 4 votes vote down vote up
private String byteArrayToString(byte[] data) {
  int length = Bytes.indexOf(data, (byte) 0);
  return new String(data, 0, length >= 0 ? length : data.length, Charsets.US_ASCII);
}
 
Example 19
Source File: Utilities.java    From JavaSerialKiller with MIT License 4 votes vote down vote up
public static byte[] serializeRequest(
        byte[] message, byte[] selectedMessage, boolean isEncoded, boolean isCompressed, String command,
        IExtensionHelpers helpers, String payloadType
) {

    int selectedOffset = 0;
    int endingOffset = 0;

    if (selectedMessage != null) {
        selectedOffset = Bytes.indexOf(message, selectedMessage);
        endingOffset = selectedOffset + selectedMessage.length;

    } else if (ChildTab.selectedMessage != null) {
        byte[] payload = ChildTab.selectedMessage;
        if (ChildTab.isCompressed) {
            payload = gzipByteArray(payload);
        }
        if (ChildTab.isEncoded) {
            payload = Base64.getEncoder().encode(payload);
        }

        selectedOffset = Bytes.indexOf(message, payload);
        endingOffset = selectedOffset + payload.length;
    }

    if (ChildTab.selectedMessage != null || selectedMessage != null) {

        byte[] beginningArray = Arrays.copyOfRange(message, 0, selectedOffset);
        byte[] endingArray = Arrays.copyOfRange(message, endingOffset, message.length);

        byte[] exploitArray = getExploitPayload(payloadType, command);

        ChildTab.selectedMessage = exploitArray;


        if (isCompressed) {
            exploitArray = gzipByteArray(exploitArray);
            ChildTab.isCompressed = true;
        } else {
            ChildTab.isCompressed = false;
        }

        byte[] output;

        if (isEncoded) {
            ChildTab.isEncoded = true;
            byte[] base64EncodedExploit = Base64.getEncoder().encode(exploitArray);

            output = Bytes.concat(beginningArray, base64EncodedExploit, endingArray);
        } else {
            ChildTab.isEncoded = false;
            output = Bytes.concat(beginningArray, exploitArray, endingArray);
        }

        IRequestInfo iRequestInfo = helpers.analyzeRequest(output);

        int bodyOffset = iRequestInfo.getBodyOffset();

        java.util.List<String> headers = iRequestInfo.getHeaders();

        byte[] newBody = new byte[output.length - bodyOffset];

        System.arraycopy(output, bodyOffset, newBody, 0, output.length - bodyOffset);

        return helpers.buildHttpMessage(headers, newBody);
    } else {
        return message;
    }
}
 
Example 20
Source File: FileUploadsUtil.java    From raml-module-builder with Apache License 2.0 4 votes vote down vote up
public static MimeMultipart MultiPartFormData(Buffer buffer, int sizeLimit, String encoding) throws IOException {

    int contentLength = buffer.length();
    ArrayList<Byte> vBytes = new ArrayList<>();
    byte[] b = new byte[1];
    int paramCount = 0;

    // if the file they are trying to upload is too big, throw an exception
    if(contentLength > sizeLimit) {
      throw new IOException("File has exceeded size limit.");
    }

    byte[] bytes = buffer.getBytes();

    byte[] separator = "\n".getBytes(Charset.forName(encoding));

    int endBoundaryPos = Bytes.indexOf(bytes, separator);

    //only copy the boundary (the first line) - not copying all content
    byte[] boundary = Arrays.copyOfRange(bytes, 0, endBoundaryPos + separator.length);

    return split(boundary, bytes, sizeLimit);

  }