List of usage examples for com.google.common.io LittleEndianDataInputStream readInt
@Override public int readInt() throws IOException
From source file:pw.simplyintricate.bitcoin.models.datastructures.InventoryVector.java
public static InventoryVector fromInputStream(LittleEndianDataInputStream reader) throws IOException { int type = reader.readInt(); byte[] hash = new byte[32]; reader.read(hash, 0, hash.length);// w ww . j a v a2 s .c o m return new InventoryVector(UnsignedInteger.fromIntBits(type), hash); }
From source file:pw.simplyintricate.bitcoin.models.datastructures.BlockVersion.java
public static BlockVersion fromInputStream(LittleEndianDataInputStream reader) throws IOException { int version = reader.readInt(); long services = reader.readLong(); long timestamp = reader.readLong(); NetworkAddress receivingNetworkAddress = NetworkAddress.fromInputStream(reader); NetworkAddress emittingNetworkAddress = NetworkAddress.fromInputStream(reader); byte[] nonce = new byte[8]; reader.read(nonce, 0, nonce.length); VariableString userAgent = VariableString.fromInputStream(reader); int startingHeight = reader.readInt(); BlockVersion blockVersion = new BlockVersion(version, UnsignedInteger.valueOf(services), timestamp, receivingNetworkAddress, emittingNetworkAddress, nonce, userAgent, startingHeight); return blockVersion; }
From source file:pw.simplyintricate.bitcoin.models.datastructures.NetworkAddress.java
public static NetworkAddress fromInputStream(LittleEndianDataInputStream reader, boolean readTime) throws IOException { int time = -1; if (readTime) { time = reader.readInt(); }//from w w w. jav a 2 s. c o m long services = reader.readLong(); reader.skip(12); // skip ipv6 support :( byte[] ipAddress = new byte[4]; reader.read(ipAddress, 0, 4); short port = EndianUtils.swapShort(((short) reader.readUnsignedShort())); NetworkAddress networkAddress; if (readTime) { networkAddress = new NetworkAddress(UnsignedInteger.valueOf(services), ipAddress, port, UnsignedInteger.fromIntBits(time)); } else { networkAddress = new NetworkAddress(UnsignedInteger.valueOf(services), ipAddress, port); } return networkAddress; }
From source file:org.linguafranca.pwdb.kdbx.stream_3_1.KdbxSerializer.java
/** * Read 4 bytes and make sure they conform to expectations of file version * @param ledis an input stream//from w w w . ja v a 2 s .c om * @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)); }
From source file:org.linguafranca.pwdb.kdbx.stream_3_1.KdbxSerializer.java
/** * Read two lots of 4 bytes and verify that they satisfy the signature of a * kdbx file;/*from ww w.j a va 2s . c o m*/ * @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; }
From source file:pw.simplyintricate.bitcoin.models.datastructures.VariableInteger.java
public static VariableInteger fromInputStream(LittleEndianDataInputStream reader) throws IOException { byte determiningSize = reader.readByte(); UnsignedInteger value;//w w w. j a va2 s . c o m if (determiningSize < 0xfd) { value = UnsignedInteger.fromIntBits(determiningSize); } else if (determiningSize <= 0xffff) { int followUpValue = reader.readUnsignedShort(); value = UnsignedInteger.fromIntBits(followUpValue); } else if (determiningSize <= 0xffffffff) { int followUpValue = reader.readInt(); value = UnsignedInteger.valueOf(followUpValue); } else { long followUpValue = reader.readLong(); value = UnsignedInteger.valueOf(followUpValue); } VariableInteger variableInteger = new VariableInteger(value); return variableInteger; }
From source file:org.linguafranca.pwdb.kdbx.stream_3_1.KdbxSerializer.java
private static int getInt(LittleEndianDataInputStream ledis) throws IOException { short fieldLength = ledis.readShort(); if (fieldLength != 4) { throw new IllegalStateException("Int required but length was " + fieldLength); }/*from ww w .ja v a 2 s. co m*/ return ledis.readInt(); }
From source file:garmintools.adapters.garmin.TableOfContentsGarminAdapter.java
private TableOfContentsEntry readTableOfContentsEntry(LittleEndianDataInputStream inputStream) throws IOException { TableOfContentsEntry.Builder builder = TableOfContentsEntry.newBuilder(); int data = inputStream.readInt(); // 00ffffff -> file offset // ff000000 -> high item length builder.setFileOffset(data & 0xffffff); int itemLength = (data >> 16) & 0xff00; data = inputStream.readInt();//from w ww. j av a2s . c om // 000000ff -> low item length // ffffff00 -> item quantity builder.setItemLength(itemLength | (data & 0xff)).setItemQuantity(data >> 8); return builder.build(); }
From source file:com.google.devtools.build.android.AndroidCompiledDataDeserializer.java
@Override public void read(Path inPath, KeyValueConsumers consumers) { Stopwatch timer = Stopwatch.createStarted(); try (ZipFile zipFile = new ZipFile(inPath.toFile())) { Enumeration<? extends ZipEntry> resourceFiles = zipFile.entries(); while (resourceFiles.hasMoreElements()) { ZipEntry resourceFile = resourceFiles.nextElement(); String fileZipPath = resourceFile.getName(); int resourceSubdirectoryIndex = fileZipPath.indexOf('_', fileZipPath.lastIndexOf('/')); Path filePath = Paths .get(String.format("%s%c%s", fileZipPath.substring(0, resourceSubdirectoryIndex), '/', fileZipPath.substring(resourceSubdirectoryIndex + 1))); String shortPath = filePath.getParent().getFileName() + "/" + filePath.getFileName(); if (filteredResources.contains(shortPath) && !Files.exists(filePath)) { // Skip files that were filtered out during analysis. // TODO(asteinb): Properly filter out these files from android_library symbol files during // analysis instead, and remove this list. continue; }/*from w w w . j a v a 2 s .co m*/ try (InputStream resourceFileStream = zipFile.getInputStream(resourceFile)) { final String[] dirNameAndQualifiers = filePath.getParent().getFileName().toString() .split(SdkConstants.RES_QUALIFIER_SEP); Factory fqnFactory = Factory.fromDirectoryName(dirNameAndQualifiers); if (fileZipPath.endsWith(".attributes")) { readAttributesFile(resourceFileStream, inPath.getFileSystem(), consumers); } else { LittleEndianDataInputStream dataInputStream = new LittleEndianDataInputStream( resourceFileStream); int magicNumber = dataInputStream.readInt(); int formatVersion = dataInputStream.readInt(); int numberOfEntries = dataInputStream.readInt(); int resourceType = dataInputStream.readInt(); if (resourceType == 0) { // 0 is a resource table readResourceTable(dataInputStream, consumers); } else if (resourceType == 1) { // 1 is a resource file readCompiledFile(dataInputStream, consumers, fqnFactory); } else { throw new DeserializationException("aapt2 version mismatch.", new DeserializationException(String.format( "Unexpected tag for resourceType %s expected 0 or 1 in %s." + "\n Last known good values:" + "\n\tmagicNumber 1414545729 (is %s)" + "\n\tformatVersion 1 (is %s)" + "\n\tnumberOfEntries 1 (is %s)", resourceType, fileZipPath, magicNumber, formatVersion, numberOfEntries))); } } } } } catch (IOException e) { throw new DeserializationException("Error deserializing " + inPath, e); } finally { logger.fine( String.format("Deserialized in compiled merged in %sms", timer.elapsed(TimeUnit.MILLISECONDS))); } }
From source file:com.google.devtools.build.android.AndroidCompiledDataDeserializer.java
/** * Reads compiled resource data files and adds them to consumers * * @param compiledFileStream First byte is number of compiled files represented in this file. Next * 8 bytes is a long indicating the length of the metadata describing the compiled file. Next * N bytes is the metadata describing the compiled file. The remaining bytes are the actual * original file.//www .j a v a 2s . c o m * @param consumers * @param fqnFactory * @throws IOException */ private void readCompiledFile(LittleEndianDataInputStream compiledFileStream, KeyValueConsumers consumers, Factory fqnFactory) throws IOException { // Skip aligned size. We don't need it here. Preconditions.checkArgument(compiledFileStream.skipBytes(8) == 8); int resFileHeaderSize = compiledFileStream.readInt(); // Skip data payload size. We don't need it here. Preconditions.checkArgument(compiledFileStream.skipBytes(8) == 8); byte[] file = new byte[resFileHeaderSize]; compiledFileStream.read(file, 0, resFileHeaderSize); CompiledFile compiledFile = CompiledFile.parseFrom(file); Path sourcePath = Paths.get(compiledFile.getSourcePath()); FullyQualifiedName fqn = fqnFactory.parse(sourcePath); DataSource dataSource = DataSource.of(sourcePath); if (consumers != null) { consumers.overwritingConsumer.accept(fqn, DataValueFile.of(dataSource)); } for (CompiledFile.Symbol exportedSymbol : compiledFile.getExportedSymbolList()) { if (!exportedSymbol.getResourceName().startsWith("android:")) { // Skip writing resource xml's for resources in the sdk FullyQualifiedName symbolFqn = fqnFactory.create(ResourceType.ID, exportedSymbol.getResourceName().replaceFirst("id/", "")); DataResourceXml dataResourceXml = DataResourceXml.from(null, dataSource, ResourceType.ID, null); consumers.combiningConsumer.accept(symbolFqn, dataResourceXml); } } }