本文整理匯總了Java中com.google.common.io.ByteArrayDataOutput.toByteArray方法的典型用法代碼示例。如果您正苦於以下問題:Java ByteArrayDataOutput.toByteArray方法的具體用法?Java ByteArrayDataOutput.toByteArray怎麽用?Java ByteArrayDataOutput.toByteArray使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類com.google.common.io.ByteArrayDataOutput
的用法示例。
在下文中一共展示了ByteArrayDataOutput.toByteArray方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: encodeString
import com.google.common.io.ByteArrayDataOutput; //導入方法依賴的package包/類
/**
* Encodes a string in either UTF-8 or UTF-16 and returns the bytes of the encoded string.
* Strings are prefixed by 2 values. The first is the number of characters in the string.
* The second is the encoding length (number of bytes in the string).
*
* <p>Here's an example UTF-8-encoded string of ab©:
* <pre>03 04 61 62 C2 A9 00</pre>
*
* @param str The string to be encoded.
* @param type The encoding type that the {@link ResourceString} should be encoded in.
* @return The encoded string.
*/
public static byte[] encodeString(String str, Type type) {
byte[] bytes = str.getBytes(type.charset());
// The extra 5 bytes is for metadata (character count + byte count) and the NULL terminator.
ByteArrayDataOutput output = ByteStreams.newDataOutput(bytes.length + 5);
encodeLength(output, str.length(), type);
if (type == Type.UTF8) { // Only UTF-8 strings have the encoding length.
encodeLength(output, bytes.length, type);
}
output.write(bytes);
// NULL-terminate the string
if (type == Type.UTF8) {
output.write(0);
} else {
output.writeShort(0);
}
return output.toByteArray();
}
示例2: encodeString
import com.google.common.io.ByteArrayDataOutput; //導入方法依賴的package包/類
/**
* Encodes a string in either UTF-8 or UTF-16 and returns the bytes of the encoded string.
* Strings are prefixed by 2 values. The first is the number of characters in the string.
* The second is the encoding length (number of bytes in the string).
*
* <p>Here's an example UTF-8-encoded string of ab©:
* <pre>03 04 61 62 C2 A9 00</pre>
*
* @param str The string to be encoded.
* @param type The encoding type that the {@link ResourceString} should be encoded in.
* @return The encoded string.
*/
public static byte[] encodeString(String str, Type type) {
byte[] bytes = str.getBytes(type.charset());
// The extra 5 bytes is for metadata (character count + byte count) and the NULL terminator.
ByteArrayDataOutput output = ByteStreams.newDataOutput(bytes.length + 5);
encodeLength(output, str.length(), type);
if (type == Type.UTF8) { // Only UTF-8 strings have the encoding length.
encodeLength(output, bytes.length, type);
}
output.write(bytes);
// NULL-terminate the string
if (type == Type.UTF8) {
output.write(0);
} else {
output.writeShort(0);
}
return output.toByteArray();
}
示例3: readLine
import com.google.common.io.ByteArrayDataOutput; //導入方法依賴的package包/類
private String readLine() throws IOException {
ByteArrayDataOutput out = ByteStreams.newDataOutput(300);
int i = 0;
int c;
while ((c = raf.read()) != -1) {
i++;
out.write((byte) c);
if (c == LINE_SEP.charAt(0)) {
break;
}
}
if (i == 0) {
return null;
}
return new String(out.toByteArray(), Charsets.UTF_8);
}
示例4: serializeBinary
import com.google.common.io.ByteArrayDataOutput; //導入方法依賴的package包/類
static byte[] serializeBinary(TagContext tags) throws TagContextSerializationException {
// Use a ByteArrayDataOutput to avoid needing to handle IOExceptions.
final ByteArrayDataOutput byteArrayDataOutput = ByteStreams.newDataOutput();
byteArrayDataOutput.write(VERSION_ID);
int totalChars = 0; // Here chars are equivalent to bytes, since we're using ascii chars.
for (Iterator<Tag> i = InternalUtils.getTags(tags); i.hasNext(); ) {
Tag tag = i.next();
totalChars += tag.getKey().getName().length();
totalChars += tag.getValue().asString().length();
encodeTag(tag, byteArrayDataOutput);
}
if (totalChars > TAGCONTEXT_SERIALIZED_SIZE_LIMIT) {
throw new TagContextSerializationException(
"Size of TagContext exceeds the maximum serialized size "
+ TAGCONTEXT_SERIALIZED_SIZE_LIMIT);
}
return byteArrayDataOutput.toByteArray();
}
示例5: findShortestSeparator
import com.google.common.io.ByteArrayDataOutput; //導入方法依賴的package包/類
@Override
public Slice findShortestSeparator(Slice start, Slice limit) {
Slice userStart = extractUserKey(start);
Slice userLimit = extractUserKey(limit);
Slice tmp = userComparator.findShortestSeparator(userStart, userLimit);
if (tmp.size() < userStart.size() && userComparator.compare(userStart, tmp) < 0) {
ByteArrayDataOutput buffer = ByteStreams.newDataOutput();
buffer.write(tmp.data(), 0, tmp.size());
try {
Coding.putFixed64(buffer, packSequenceAndType(MAX_SEQUENCE_NUMBER, VALUE_TYPE_FOR_SEEK));
} catch (IOException e) {
// should never exception
return start;
}
tmp = new Slice(buffer.toByteArray());
Preconditions.checkState(compare(start, tmp) < 0);
Preconditions.checkState(compare(tmp, limit) < 0);
return tmp;
}
return start;
}
示例6: testSerialization
import com.google.common.io.ByteArrayDataOutput; //導入方法依賴的package包/類
@Test
public void testSerialization() throws Exception
{
Range<Token> full = new Range<>(tok(-1), tok(-1));
// populate and validate the tree
mt.maxsize(256);
mt.init();
for (TreeRange range : mt.invalids())
range.addAll(new HIterator(range.right));
byte[] initialhash = mt.hash(full);
ByteArrayDataOutput out = ByteStreams.newDataOutput();
MerkleTree.serializer.serialize(mt, out, MessagingService.current_version);
byte[] serialized = out.toByteArray();
ByteArrayDataInput in = ByteStreams.newDataInput(serialized);
MerkleTree restored = MerkleTree.serializer.deserialize(in, MessagingService.current_version);
assertHashEquals(initialhash, restored.hash(full));
}
示例7: createWDLPacket4
import com.google.common.io.ByteArrayDataOutput; //導入方法依賴的package包/類
/**
* Creates the WDL packet #4.
*
* This packet specifies the initial overrides for what chunks can and
* cannot be saved.
*
* This packet starts with an int stating the number of keys, and then a
* series of values for 1 range group. The range group starts with its name
* (the key in ranges), then an int (the number of ranges) and then each of
* the ranges as generated by
* {@link #writeProtectionRange(ProtectionRange, ByteArrayDataOutput)}.
*/
public static byte[] createWDLPacket4(
Map<String, List<ProtectionRange>> ranges) {
ByteArrayDataOutput output = ByteStreams.newDataOutput();
output.writeInt(4);
output.writeInt(ranges.size());
for (String key : ranges.keySet()) {
output.writeUTF(key);
List<ProtectionRange> rangeGroup = ranges.get(key);
output.writeInt(rangeGroup.size());
for (ProtectionRange range : rangeGroup) {
writeProtectionRange(range, output);
}
}
return output.toByteArray();
}
示例8: createWDLPacket5
import com.google.common.io.ByteArrayDataOutput; //導入方法依賴的package包/類
/**
* Creates the WDL packet #5.
*
* This packet specifies adds additional overrides to or sets all of the
* overrides in a single group.
*
* This packet starts with a String stating the group, then a boolean that
* specifies whether it is setting (true) or adding (false) the ranges, and
* then an int (the number of ranges that will be added). Then, each range,
* formated by
* {@link #writeProtectionRange(ProtectionRange, ByteArrayDataOutput)}.
*/
public static byte[] createWDLPacket5(String group,
boolean replace, List<ProtectionRange> ranges) {
ByteArrayDataOutput output = ByteStreams.newDataOutput();
output.writeInt(5);
output.writeUTF(group);
output.writeBoolean(replace);
output.writeInt(ranges.size());
for (ProtectionRange range : ranges) {
writeProtectionRange(range, output);
}
return output.toByteArray();
}
示例9: createWDLPacket7
import com.google.common.io.ByteArrayDataOutput; //導入方法依賴的package包/類
/**
* Creates the WDL packet #7.
*
* This packet replaces all of the ranges with the given tag with a new set
* of ranges.
*
* This packet starts with a String stating the group, then a second string
* that specifies the tag to replace. After that, there is an int stating
* the number of ranges, and then each range as formated by
* {@link #writeProtectionRange(ProtectionRange, ByteArrayDataOutput)}.
*/
public static byte[] createWDLPacket7(String group,
String tag, List<ProtectionRange> newRanges) {
ByteArrayDataOutput output = ByteStreams.newDataOutput();
output.writeInt(7);
output.writeUTF(group);
output.writeUTF(tag);
output.writeInt(newRanges.size());
for (ProtectionRange range : newRanges) {
writeProtectionRange(range, output);
}
return output.toByteArray();
}
示例10: generatePacket
import com.google.common.io.ByteArrayDataOutput; //導入方法依賴的package包/類
@Override
public byte[] generatePacket(Object... data)
{
ByteArrayDataOutput dat = ByteStreams.newDataOutput();
Collection<NetworkModHandler >networkMods = FMLNetworkHandler.instance().getNetworkIdMap().values();
dat.writeInt(networkMods.size());
for (NetworkModHandler handler : networkMods)
{
dat.writeUTF(handler.getContainer().getModId());
dat.writeInt(handler.getNetworkId());
}
// TODO send the other id maps as well
return dat.toByteArray();
}
示例11: keyBytes
import com.google.common.io.ByteArrayDataOutput; //導入方法依賴的package包/類
private byte[][] keyBytes(int[] keys, IBM index) throws Exception {
byte[][] bytes;
if (atomized) {
long[] sizes = bitmaps.serializeAtomizedSizeInBytes(index, keys);
ByteArrayDataOutput[] dataOutputs = new ByteArrayDataOutput[keys.length];
for (int i = 0; i < keys.length; i++) {
dataOutputs[i] = sizes[i] < 0 ? null : ByteStreams.newDataOutput((int) sizes[i]);
}
bitmaps.serializeAtomized(index, keys, dataOutputs);
bytes = new byte[keys.length][];
for (int i = 0; i < keys.length; i++) {
bytes[i] = dataOutputs[i] == null ? null : dataOutputs[i].toByteArray();
}
} else {
long size = serializedSizeInBytes(bitmaps, index);
ByteArrayDataOutput dataOutput = ByteStreams.newDataOutput((int) size);
dataOutput.write(FilerIO.intBytes(lastId));
bitmaps.serialize(index, dataOutput);
bytes = new byte[][] { dataOutput.toByteArray() };
}
return bytes;
}
示例12: sendToChannel
import com.google.common.io.ByteArrayDataOutput; //導入方法依賴的package包/類
private void sendToChannel(MessageAgent agent, Player player) {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF(agent.getSubChannel());
agent.appendPayload(out);
byte[] data = out.toByteArray();
player.sendPluginMessage(plugin, CHANNEL, data);
// register listener
if (agent instanceof MessageCallback) {
MessageCallback callback = (MessageCallback) agent;
registerListener(callback);
}
}
示例13: toByteArray
import com.google.common.io.ByteArrayDataOutput; //導入方法依賴的package包/類
@Override
public byte[] toByteArray(boolean shrink) throws IOException {
ByteArrayDataOutput output = ByteStreams.newDataOutput();
for (Chunk chunk : chunks) {
output.write(chunk.toByteArray(shrink));
}
return output.toByteArray();
}
示例14: toByteArray
import com.google.common.io.ByteArrayDataOutput; //導入方法依賴的package包/類
@Override
public byte[] toByteArray(boolean shrink) throws IOException {
ByteArrayDataOutput output = ByteStreams.newDataOutput();
for (Chunk chunk : chunks) {
output.write(chunk.toByteArray(shrink));
}
return output.toByteArray();
}
示例15: create
import com.google.common.io.ByteArrayDataOutput; //導入方法依賴的package包/類
public static CloseLocalHistoryPayload create(final LocalHistoryIdentifier historyId) {
final ByteArrayDataOutput out = ByteStreams.newDataOutput();
try {
historyId.writeTo(out);
} catch (IOException e) {
// This should never happen
LOG.error("Failed to serialize {}", historyId, e);
throw new RuntimeException("Failed to serialize " + historyId, e);
}
return new CloseLocalHistoryPayload(historyId, out.toByteArray());
}