本文整理汇总了Java中com.google.common.primitives.Bytes.indexOf方法的典型用法代码示例。如果您正苦于以下问题:Java Bytes.indexOf方法的具体用法?Java Bytes.indexOf怎么用?Java Bytes.indexOf使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类com.google.common.primitives.Bytes
的用法示例。
在下文中一共展示了Bytes.indexOf方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: corruptFile
import com.google.common.primitives.Bytes; //导入方法依赖的package包/类
/**
* 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);
}
示例2: getFirstMatch
import com.google.common.primitives.Bytes; //导入方法依赖的package包/类
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;
}
示例3: recv
import com.google.common.primitives.Bytes; //导入方法依赖的package包/类
public byte[] recv(Socket socket) throws DisconnectedException, ReadTimeoutException {
try {
BufferedInputStream inStream = new BufferedInputStream(socket.getInputStream());
if (timestamp == 0)
timestamp = System.currentTimeMillis();
while (!new String(buffer).contains(packetDelimiter)) {
// Disconnected
long time = System.currentTimeMillis() - timestamp;
if (time >= TIMEOUT) {
clear();
throw new DisconnectedException();
}
// Timeout
if (inStream.available() < 2)
throw new ReadTimeoutException();
timestamp = System.currentTimeMillis();
// Read
byte[] read = new byte[100000];
int numRead = inStream.read(read);
read = Arrays.copyOfRange(read, 0, numRead);
// Combine saved and new bytes
byte[] newBytes = new byte[buffer.length + read.length];
System.arraycopy(buffer, 0, newBytes, 0, buffer.length);
System.arraycopy(read, 0, newBytes, buffer.length, read.length);
// Save
buffer = newBytes;
}
int index = Bytes.indexOf(buffer, packetDelimiter.getBytes());
byte[] packet = Arrays.copyOfRange(buffer, 0, index);
buffer = Arrays.copyOfRange(buffer, index + packetDelimiter.length(), buffer.length);
return decode(packet);
}
catch (IOException e) {
Logger.exception(e);
clear();
throw new DisconnectedException();
}
}