下面列出了怎么用com.google.common.io.LittleEndianDataInputStream的API类实例代码及写法,或者点击链接到github查看源代码。
private final void doStart() throws IOException {
ReadUtil.readCheckType(m_stream, AXML_CHUNK_TYPE);
/*chunk size*/
ReadUtil.readInt(m_stream);
m_strings = StringBlock.read(new ExtDataInput((DataInput) new LittleEndianDataInputStream(m_stream)));
ReadUtil.readCheckType(m_stream, RESOURCEIDS_CHUNK_TYPE);
int chunkSize = ReadUtil.readInt(m_stream);
if (chunkSize < 8 || (chunkSize % 4) != 0) {
throw new IOException("Invalid resource ids size (" + chunkSize + ").");
}
m_resourceIDs = ReadUtil.readIntArray(m_stream, chunkSize / 4 - 2);
resetState();
}
public float[] get() {
try (LittleEndianDataInputStream dis = new LittleEndianDataInputStream(new BufferedInputStream(input))) {
int size = dis.readInt();
float[] floats = new float[size];
float next = 0f;
int read = 0;
while (read < size) {
next = nextFloat(dis);
floats[read++] = next;
}
if (read != size)
throw new RuntimeException(String.format(Locale.US, "Not correct size, expected %d, but was %d", size, read));
return floats;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
private ARSCDecoder(InputStream arscStream, ResTable resTable, boolean storeFlagsOffsets, boolean keepBroken) {
arscStream = mCountIn = new CountingInputStream(arscStream);
if (storeFlagsOffsets) {
mFlagsOffsets = new ArrayList<FlagsOffset>();
} else {
mFlagsOffsets = null;
}
// We need to explicitly cast to DataInput as otherwise the constructor is ambiguous.
// We choose DataInput instead of InputStream as ExtDataInput wraps an InputStream in
// a DataInputStream which is big-endian and ignores the little-endian behavior.
mIn = new ExtDataInput((DataInput) new LittleEndianDataInputStream(arscStream));
mResTable = resTable;
mKeepBroken = keepBroken;
}
public void open(InputStream stream) {
close();
if (stream != null) {
// We need to explicitly cast to DataInput as otherwise the constructor is ambiguous.
// We choose DataInput instead of InputStream as ExtDataInput wraps an InputStream in
// a DataInputStream which is big-endian and ignores the little-endian behavior.
m_reader = new ExtDataInput((DataInput) new LittleEndianDataInputStream(stream));
}
}
private float nextFloat(LittleEndianDataInputStream dis) {
try {
float f = dis.readFloat();
return f;
} catch (IOException e) {
return Float.MIN_VALUE;
}
}
public VectorIterator(File vectorsFile, long dimensions, boolean sparse) throws IOException {
this.sparse = sparse;
this.input = new LittleEndianDataInputStream(new FileInputStream(vectorsFile));
parseHeadline(this.input);
if (this.dimensions != (int) dimensions) {
throw new IOException("inconsistent number of dimensions.");
}
setCurrentContent();
}
private void parseHeadline(LittleEndianDataInputStream input) throws IOException {
ByteArrayDataOutput out = ByteStreams.newDataOutput();
byte c;
while((c = input.readByte()) != '\n') {
out.writeByte(c);
}
String[] headline = new String(out.toByteArray(), StandardCharsets.UTF_8).split(" ");
this.numberOfVectors = Integer.parseInt(headline[0]);
this.dimensions = Integer.parseInt(headline[1]);
}
public ClickHouseRowBinaryInputStream(InputStream is, TimeZone timeZone, ClickHouseProperties properties) {
this.in = new LittleEndianDataInputStream(is);
if (properties.isUseServerTimeZoneForDates()) {
this.timeZone = timeZone;
} else {
this.timeZone = TimeZone.getDefault();
}
}
private static void checkStartBytes(KdbxHeader kdbxHeader, InputStream decryptedInputStream) throws IOException {
LittleEndianDataInputStream ledis = new LittleEndianDataInputStream(decryptedInputStream);
byte [] startBytes = new byte[32];
ledis.readFully(startBytes);
if (!Arrays.equals(startBytes, kdbxHeader.getStreamStartBytes())) {
throw new IllegalStateException("Inconsistent stream start bytes. This usually means the credentials were wrong.");
}
}
private static int getInt(LittleEndianDataInputStream ledis) throws IOException {
short fieldLength = ledis.readShort();
if (fieldLength != 4) {
throw new IllegalStateException("Int required but length was " + fieldLength);
}
return ledis.readInt();
}
private static long getLong(LittleEndianDataInputStream ledis) throws IOException {
short fieldLength = ledis.readShort();
if (fieldLength != 8) {
throw new IllegalStateException("Long required but length was " + fieldLength);
}
return ledis.readLong();
}
private List<NamedTag> read(byte[] data) {
ArrayList<NamedTag> list = new ArrayList<>();
ByteArrayInputStream baos = new ByteArrayInputStream(data);
try (NBTInputStream in = new NBTInputStream((DataInput) new LittleEndianDataInputStream(baos))) {
while (baos.available() > 0) {
NamedTag nt = in.readNamedTag();
list.add(nt);
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
@Override
public int[] deserialize(File file) throws IOException {
LittleEndianDataInputStream is = new LittleEndianDataInputStream(new BufferedInputStream(new FileInputStream(file)));
int[] ret = new int[(int)(file.length() / 4)];
for (int i = 0; i < ret.length; ++i) {
ret[i] = is.readInt();
}
return ret;
}
public TempFileIterator(
Path tempPath,
int tmpCount,
Serializer<K> keySerializer
) throws IOException {
this.tmpCount = tmpCount;
this.keySerializer = keySerializer;
in = new LittleEndianDataInputStream(new BufferedInputStream(Files.newInputStream(tempPath), 131072));
}
public Reader(Path path, Comparator<K> comparator, Serializer<K> keySerializer, Serializer<V> valueSerializer, final boolean mlockFiles) throws IOException {
this.comparator = comparator;
indexFile = path.resolve("index.bin");
sizeInBytes = Files.size(indexFile);
buffer = new MMapBuffer(indexFile, FileChannel.MapMode.READ_ONLY, ByteOrder.LITTLE_ENDIAN);
try {
stuffToClose = SharedReference.create((Closeable)buffer);
final MemoryDataInput in = new MemoryDataInput(buffer.memory());
if (sizeInBytes < Header.length()) {
throw new IOException("file is less than header length bytes");
}
final byte[] headerBytes = new byte[Header.length()];
in.seek(sizeInBytes - Header.length());
in.readFully(headerBytes);
final LittleEndianDataInputStream headerStream = new LittleEndianDataInputStream(new ByteArrayInputStream(headerBytes));
final Header header = new HeaderSerializer().read(headerStream);
hasDeletions = header.hasDeletions;
size = header.size;
if (header.fileLength != sizeInBytes) {
log.error(header.fileLength);
throw new IOException("file length written to last 8 bytes of file does not match file length, file is inconsistent");
}
rootLevel = Level.build(buffer.memory(), keySerializer, valueSerializer, comparator, header.hasDeletions, header.indexLevels);
rootLevelStartAddress = header.rootLevelStartAddress;
if (mlockFiles) buffer.mlock(0, buffer.memory().length());
} catch (Throwable t) {
Closeables2.closeQuietly(buffer, log);
Throwables.propagateIfInstanceOf(t, IOException.class);
throw Throwables.propagate(t);
}
}
private static byte[] readBytesAndSkipPadding(LittleEndianDataInputStream input, int size)
throws IOException {
byte[] result = new byte[size];
input.readFully(result);
long overage = size % AAPT_FLAT_FILE_ALIGNMENT;
if (overage != 0) {
ByteStreams.skipFully(input, AAPT_FLAT_FILE_ALIGNMENT - overage);
}
return result;
}
@SuppressWarnings("IOResourceOpenedButNotSafelyClosed")
public InputStreamSliceInput(InputStream inputStream)
{
pushbackInputStream = new PushbackInputStream(inputStream);
countingInputStream = new CountingInputStream(pushbackInputStream);
dataInputStream = new LittleEndianDataInputStream(countingInputStream);
}
@Test
public void whenWriteReadLittleEndian_thenCorrect() throws IOException {
final int value = 100;
final LittleEndianDataOutputStream writer = new LittleEndianDataOutputStream(new FileOutputStream("src/test/resources/test_le.txt"));
writer.writeInt(value);
writer.close();
final LittleEndianDataInputStream reader = new LittleEndianDataInputStream(new DataInputStream(new FileInputStream("src/test/resources/test_le.txt")));
final int result = reader.readInt();
reader.close();
assertEquals(value, result);
}
public SerializedKeyValueIterator(final InputStream input, final SmartSerializer<K> keySerializer, final SmartSerializer<V> valueSerializer) throws IOException {
this.input = input;
this.in = new LittleEndianDataInputStream(input);
this.keySerializer = keySerializer;
this.valueSerializer = valueSerializer;
}
public ClickHouseLZ4Stream(InputStream stream) {
this.stream = stream;
dataWrapper = new LittleEndianDataInputStream(stream);
}
/**
* Populate a KdbxHeader from the input stream supplied
* @param kdbxHeader a header to be populated
* @param inputStream an input stream
* @return the populated KdbxHeader
* @throws IOException on error
*/
public static KdbxHeader readKdbxHeader(KdbxHeader kdbxHeader, InputStream inputStream) throws IOException {
MessageDigest digest = Encryption.getMessageDigestInstance();
// we do not close this stream, otherwise we lose our place in the underlying stream
DigestInputStream digestInputStream = new DigestInputStream(inputStream, digest);
// we do not close this stream, otherwise we lose our place in the underlying stream
LittleEndianDataInputStream ledis = new LittleEndianDataInputStream(digestInputStream);
if (!verifyMagicNumber(ledis)) {
throw new IllegalStateException("Magic number did not match");
}
if (!verifyFileVersion(ledis)) {
throw new IllegalStateException("File version did not match");
}
byte headerType;
while ((headerType = ledis.readByte()) != HeaderType.END) {
switch (headerType) {
case HeaderType.COMMENT:
getByteArray(ledis);
break;
case HeaderType.CIPHER_ID:
kdbxHeader.setCipherUuid(getByteArray(ledis));
break;
case HeaderType.COMPRESSION_FLAGS:
kdbxHeader.setCompressionFlags(getInt(ledis));
break;
case HeaderType.MASTER_SEED:
kdbxHeader.setMasterSeed(getByteArray(ledis));
break;
case HeaderType.TRANSFORM_SEED:
kdbxHeader.setTransformSeed(getByteArray(ledis));
break;
case HeaderType.TRANSFORM_ROUNDS:
kdbxHeader.setTransformRounds(getLong(ledis));
break;
case HeaderType.ENCRYPTION_IV:
kdbxHeader.setEncryptionIv(getByteArray(ledis));
break;
case HeaderType.PROTECTED_STREAM_KEY:
kdbxHeader.setProtectedStreamKey(getByteArray(ledis));
break;
case HeaderType.STREAM_START_BYTES:
kdbxHeader.setStreamStartBytes(getByteArray(ledis));
break;
case HeaderType.INNER_RANDOM_STREAM_ID:
kdbxHeader.setInnerRandomStreamId(getInt(ledis));
break;
default: throw new IllegalStateException("Unknown File Header");
}
}
// consume length etc. following END flag
getByteArray(ledis);
kdbxHeader.setHeaderHash(digest.digest());
return kdbxHeader;
}
private static byte [] getByteArray(LittleEndianDataInputStream ledis) throws IOException {
short fieldLength = ledis.readShort();
byte [] value = new byte[fieldLength];
ledis.readFully(value);
return value;
}
public static String readString(LittleEndianDataInputStream objectInputStream) throws IOException {
int size = objectInputStream.readInt();
byte read[] = new byte[size];
objectInputStream.read(read);
return new String(read);
}
public static void copyLevelDat(File folderTo, File in) throws IOException {
File levelDat = new File(folderTo, "level.dat");
if (!levelDat.exists()) {
folderTo.mkdirs();
levelDat.createNewFile();
}
try (LittleEndianDataInputStream ledis = new LittleEndianDataInputStream(new FileInputStream(in))) {
int version = ledis.readInt(); // Ignored
int length = ledis.readInt(); // Ignored
NBTInputStream nis = new NBTInputStream((DataInput) ledis);
NamedTag named = nis.readNamedTag();
com.sk89q.jnbt.CompoundTag tag = (CompoundTag) named.getTag();
Map<String, com.sk89q.jnbt.Tag> map = ReflectionUtils.getMap(tag.getValue());
Map<String, String> gameRules = new HashMap<>();
gameRules.put("firedamage", "firedamage");
gameRules.put("falldamage", "falldamage");
gameRules.put("dofiretick", "doFireTick");
gameRules.put("drowningdamage", "drowningdamage");
gameRules.put("doentitydrops", "doEntityDrops");
gameRules.put("keepinventory", "keepInventory");
gameRules.put("sendcommandfeedback", "sendCommandFeedback");
gameRules.put("dodaylightcycle", "doDaylightCycle");
gameRules.put("commandblockoutput", "commandBlockOutput");
gameRules.put("domobloot", "doMobLoot");
gameRules.put("domobspawning", "doMobSpawning");
gameRules.put("doweathercycle", "doWeatherCycle");
gameRules.put("mobgriefing", "mobGriefing");
gameRules.put("dotiledrops", "doTileDrops");
HashMap<String, com.sk89q.jnbt.Tag> ruleTagValue = new HashMap<>();
for (Map.Entry<String, String> rule : gameRules.entrySet()) {
com.sk89q.jnbt.Tag value = map.remove(rule.getKey());
if (value instanceof ByteTag) {
value = new StringTag((Byte) value.getValue() == 1 ? "true" : "false");
}
if (value != null) {
ruleTagValue.put(rule.getValue(), value);
}
}
HashSet<String> allowed = new HashSet<>(Arrays.asList(
"lightningTime", "pvp", "LevelName", "Difficulty", "GameType", "Generator", "LastPlayed", "RandomSeed", "StorageVersion", "Time", "commandsEnabled", "currentTick", "rainTime", "SpawnX", "SpawnY", "SpawnZ", "SizeOnDisk"
));
Iterator<Map.Entry<String, com.sk89q.jnbt.Tag>> iterator = map.entrySet().iterator();
while (iterator.hasNext()) {
Map.Entry<String, com.sk89q.jnbt.Tag> entry = iterator.next();
if (!allowed.contains(entry.getKey())) {
iterator.remove();
}
}
{
map.put("GameRules", new CompoundTag(ruleTagValue));
map.put("version", new IntTag(19133));
map.put("DataVersion", new IntTag(1343));
map.put("initialized", new ByteTag((byte) 1));
map.putIfAbsent("SizeOnDisk", new LongTag(0));
// generator
int generator = tag.getInt("Generator");
String name;
switch (generator) {
default:
case 1:
name = "default";
break;
case 2:
name = "flat";
break;
}
map.put("generatorName", new StringTag(name));
map.put("generatorOptions", new StringTag(""));
map.put("generatorVersion", new IntTag(1));
map.put("Difficulty", new ByteTag((byte) tag.getInt("Difficulty")));
map.put("DifficultyLocked", new ByteTag((byte) 0));
map.put("MapFeatures", new ByteTag((byte) 1));
map.put("allowCommands", new ByteTag(tag.getByte("commandsEnabled")));
long time = tag.getLong("Time");
if (time == 0) time = tag.getLong("CurrentTick");
map.put("Time", new LongTag(time));
map.put("spawnMobs", new ByteTag((byte) 1));
Long lastPlayed = tag.getLong("LastPlayed");
if (lastPlayed != null && lastPlayed < Integer.MAX_VALUE) {
lastPlayed = lastPlayed * 1000;
map.put("LastPlayed", new LongTag(lastPlayed));
}
HashMap<String, com.sk89q.jnbt.Tag> data = new HashMap<>();
data.put("Data", new CompoundTag(map));
CompoundTag root = new CompoundTag(data);
try (NBTOutputStream nos = new NBTOutputStream(new PGZIPOutputStream(new FileOutputStream(levelDat)))) {
nos.writeNamedTag("level.dat", root);
}
}
}
}
public String decompressXml(InputStream is) throws IOException {
StringBuilder result = new StringBuilder("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
try(LittleEndianDataInputStream dis = new LittleEndianDataInputStream(is)) {
//Getting and checking the marker for a valid XML file
int fileMarker = dis.readInt();
if (fileMarker != PACKED_XML_IDENTIFIER) {
throw new IOException(
String.format(ERROR_INVALID_MAGIC_NUMBER,
PACKED_XML_IDENTIFIER,
fileMarker));
}
dis.skipBytes(4);
List<String> packedStrings = parseStrings(dis);
int ident = 0;
int tag = dis.readShort();
do {
int headerSize = dis.readShort();
int chunkSize = dis.readInt();
switch (tag) {
case RES_XML_FIRST_CHUNK_TYPE: {
dis.skipBytes(chunkSize - 8);
break;
}
case RES_XML_RESOURCE_MAP_TYPE: {
dis.skipBytes(chunkSize - 8);
break;
}
case START_ELEMENT_TAG: {
parseStartTag(result, dis, packedStrings, ident);
ident++;
break;
}
case END_ELEMENT_TAG: {
ident--;
parseEndTag(result, dis, packedStrings, ident);
break;
}
case CDATA_TAG: {
parseCDataTag(result, dis, packedStrings, ident);
break;
}
default:
System.err.println(String.format(ERROR_UNKNOWN_TAG, tag));
}
tag = dis.readShort();
} while (tag != END_DOC_TAG);
return result.toString();
}
}
private static ResourceContainer readResourceContainer(
ZipFile zipFile, boolean includeFileContentsForValidation) throws IOException {
List<ResourceTable> resourceTables = new ArrayList<>();
List<CompiledFileWithData> compiledFiles = new ArrayList<>();
Enumeration<? extends ZipEntry> resourceFiles = zipFile.entries();
while (resourceFiles.hasMoreElements()) {
ZipEntry resourceFile = resourceFiles.nextElement();
String fileZipPath = resourceFile.getName();
if (fileZipPath.endsWith(CompiledResources.ATTRIBUTES_FILE_EXTENSION)) {
continue;
}
try (LittleEndianDataInputStream dataInputStream =
new LittleEndianDataInputStream(zipFile.getInputStream(resourceFile))) {
int magic = dataInputStream.readInt();
verify(
magic == AAPT_CONTAINER_MAGIC,
"Unexpected magic number in %s: %s",
resourceFile,
magic);
int version = dataInputStream.readInt();
verify(
version == AAPT_CONTAINER_VERSION,
"Unexpected version number in %s: %s",
resourceFile,
version);
int numberOfEntries = dataInputStream.readInt();
for (int i = 0; i < numberOfEntries; i++) {
int entryType = dataInputStream.readInt();
verify(
entryType == AAPT_CONTAINER_ENTRY_RES_TABLE
|| entryType == AAPT_CONTAINER_ENTRY_RES_FILE,
"Unexpected entry type in %s: %s",
resourceFile,
entryType);
if (entryType == AAPT_CONTAINER_ENTRY_RES_TABLE) {
long size = dataInputStream.readLong();
verify(size <= Integer.MAX_VALUE);
byte[] tableBytes = readBytesAndSkipPadding(dataInputStream, (int) size);
resourceTables.add(
ResourceTable.parseFrom(tableBytes, ExtensionRegistry.getEmptyRegistry()));
} else {
// useless and wrong "size" data; see
// https://android-review.googlesource.com/c/platform/frameworks/base/+/1161789
ByteStreams.skipFully(dataInputStream, 8);
int headerSize = dataInputStream.readInt();
long payloadSize = dataInputStream.readLong();
byte[] headerBytes = readBytesAndSkipPadding(dataInputStream, headerSize);
CompiledFile compiledFile =
CompiledFile.parseFrom(headerBytes, ExtensionRegistry.getEmptyRegistry());
HashCode fingerprint;
XmlNode rootXmlNode;
if (includeFileContentsForValidation) {
byte[] payloadBytes = readBytesAndSkipPadding(dataInputStream, (int) payloadSize);
fingerprint = Hashing.goodFastHash(/*minimumBits=*/ 64).hashBytes(payloadBytes);
rootXmlNode =
compiledFile.getType() == Resources.FileReference.Type.PROTO_XML
? XmlNode.parseFrom(payloadBytes, ExtensionRegistry.getEmptyRegistry())
: null;
} else {
ByteStreams.skipFully(
dataInputStream, roundUpToNearest(payloadSize, AAPT_FLAT_FILE_ALIGNMENT));
fingerprint = null;
rootXmlNode = null;
}
compiledFiles.add(CompiledFileWithData.create(compiledFile, fingerprint, rootXmlNode));
}
}
}
}
return ResourceContainer.create(resourceTables, compiledFiles);
}
/**
* Creates a new {@code NBTInputStream}, which will source its data
* from the specified input stream.
*
* @param is The input stream.
* @param littleEndian Whether the stream contains little endian data.
*/
public NBTInputStream(InputStream is, boolean littleEndian) {
if (is == null) {
throw new NullPointerException("is");
}
this.is = littleEndian ? new LittleEndianDataInputStream(is) : new DataInputStream(is);
}
/**
* Read two lots of 4 bytes and verify that they satisfy the signature of a
* kdbx file;
* @param ledis an input stream
* @return true if it looks like this is a kdbx file
* @throws IOException on error
*/
private static boolean verifyMagicNumber(LittleEndianDataInputStream ledis) throws IOException {
int sig1 = ledis.readInt();
int sig2 = ledis.readInt();
return sig1 == SIG1 && sig2 == SIG2;
}
/**
* Read 4 bytes and make sure they conform to expectations of file version
* @param ledis an input stream
* @return true if it looks like we understand this file version
* @throws IOException on error
*/
private static boolean verifyFileVersion(LittleEndianDataInputStream ledis) throws IOException {
return ((ledis.readInt() & FILE_VERSION_CRITICAL_MASK) <= (FILE_VERSION_32 & FILE_VERSION_CRITICAL_MASK));
}