List of usage examples for java.io DataInputStream readInt
public final int readInt() throws IOException
readInt
method of DataInput
. From source file:ubic.gemma.core.loader.expression.arrayDesign.AffyChipTypeExtractor.java
/** * @return 32 bit signed integral number *//*from w w w. ja va2s .c o m*/ private static int readIntLittleEndian(DataInputStream dis) throws IOException { return dis.readInt(); }
From source file:com.atilika.kuromoji.trie.DoubleArrayTrie.java
/** * Load Stored data//from www.ja v a 2 s.c o m * * @param input input stream to read the double array trie from * @return double array trie, not null * @throws IOException if an IO error occured during reading the double array trie */ public static DoubleArrayTrie read(InputStream input) throws IOException { DoubleArrayTrie trie = new DoubleArrayTrie(); DataInputStream dis = new DataInputStream(new BufferedInputStream(input)); trie.compact = dis.readBoolean(); int baseCheckSize = dis.readInt(); // Read size of baseArr and checkArr int tailSize = dis.readInt(); // Read size of tailArr ReadableByteChannel channel = Channels.newChannel(dis); ByteBuffer tmpBaseBuffer = ByteBuffer.allocate(baseCheckSize * 4); channel.read(tmpBaseBuffer); tmpBaseBuffer.rewind(); trie.baseBuffer = tmpBaseBuffer.asIntBuffer(); ByteBuffer tmpCheckBuffer = ByteBuffer.allocate(baseCheckSize * 4); channel.read(tmpCheckBuffer); tmpCheckBuffer.rewind(); trie.checkBuffer = tmpCheckBuffer.asIntBuffer(); ByteBuffer tmpTailBuffer = ByteBuffer.allocate(tailSize * 2); channel.read(tmpTailBuffer); tmpTailBuffer.rewind(); trie.tailBuffer = tmpTailBuffer.asCharBuffer(); input.close(); return trie; }
From source file:ClassFileUtilities.java
/** * Returns the dependencies of the given class. * @return a list of strings representing the used classes. */// ww w . j a va2 s . c o m public static Set getClassDependencies(InputStream is) throws IOException { DataInputStream dis = new DataInputStream(is); if (dis.readInt() != 0xcafebabe) { throw new IOException("Invalid classfile"); } dis.readInt(); int len = dis.readShort(); String[] strs = new String[len]; Set classes = new HashSet(); Set desc = new HashSet(); for (int i = 1; i < len; i++) { int constCode = dis.readByte() & 0xff; switch (constCode) { case CONSTANT_LONG_INFO: case CONSTANT_DOUBLE_INFO: dis.readLong(); i++; break; case CONSTANT_FIELDREF_INFO: case CONSTANT_METHODREF_INFO: case CONSTANT_INTERFACEMETHODREF_INFO: case CONSTANT_INTEGER_INFO: case CONSTANT_FLOAT_INFO: dis.readInt(); break; case CONSTANT_CLASS_INFO: classes.add(new Integer(dis.readShort() & 0xffff)); break; case CONSTANT_STRING_INFO: dis.readShort(); break; case CONSTANT_NAMEANDTYPE_INFO: dis.readShort(); desc.add(new Integer(dis.readShort() & 0xffff)); break; case CONSTANT_UTF8_INFO: strs[i] = dis.readUTF(); break; default: throw new RuntimeException("unexpected data in constant-pool:" + constCode); } } Set result = new HashSet(); Iterator it = classes.iterator(); while (it.hasNext()) { result.add(strs[((Integer) it.next()).intValue()]); } it = desc.iterator(); while (it.hasNext()) { result.addAll(getDescriptorClasses(strs[((Integer) it.next()).intValue()])); } return result; }
From source file:com.alphabetbloc.accessmrs.utilities.NetworkUtils.java
public static DataInputStream getOdkStream(HttpClient client, String url) throws Exception { // get prefs/* www . ja v a2 s. co m*/ HttpPost request = new HttpPost(url); request.setEntity(new OdkAuthEntity()); HttpResponse response = client.execute(request); response.getStatusLine().getStatusCode(); HttpEntity responseEntity = response.getEntity(); DataInputStream zdis = new DataInputStream(new GZIPInputStream(responseEntity.getContent())); int status = zdis.readInt(); if (status == HttpURLConnection.HTTP_UNAUTHORIZED) { zdis.close(); throw new IOException("Access denied. Check your username and password."); } else if (status <= 0 || status >= HttpURLConnection.HTTP_BAD_REQUEST) { zdis.close(); throw new IOException(App.getApp().getString(R.string.error_connection)); } else { assert (status == HttpURLConnection.HTTP_OK); // success return zdis; } }
From source file:org.gitools.matrix.format.CmatrixMatrixFormat.java
public static String[] readHeader(File file) { try {/*from w w w . jav a 2 s . c om*/ DataInputStream in; if (file.getName().endsWith(".gz")) { in = new DataInputStream(new GZIPInputStream(new FileInputStream(file))); } else { in = new DataInputStream(new FileInputStream(file)); } // Format version int version = in.readInt(); // Dictionary byte[] dictionary = readBuffer(in); // Columns String[] columns = splitBuffer(readBuffer(in)); // Rows String[] rows = splitBuffer(readBuffer(in)); // Headers String[] headers = splitBuffer(readBuffer(in)); in.close(); return headers; } catch (IOException e) { throw new PersistenceException(e); } }
From source file:com.linkedin.pinot.core.common.datatable.DataTableImplV2.java
private static String decodeString(DataInputStream dataInputStream) throws IOException { int length = dataInputStream.readInt(); if (length == 0) { return StringUtils.EMPTY; } else {// ww w . j av a 2 s. com byte[] buffer = new byte[length]; int numBytesRead = dataInputStream.read(buffer); assert numBytesRead == length; return new String(buffer, UTF_8); } }
From source file:org.apache.hadoop.hdfs.server.namenode.EditLogFileInputStream.java
/** * Read the header of fsedit log/*ww w . java 2 s.c o m*/ * @param in fsedit stream * @return the edit log version number * @throws IOException if error occurs */ @VisibleForTesting static int readLogVersion(DataInputStream in, boolean verifyLayoutVersion) throws IOException, LogHeaderCorruptException { int logVersion; try { logVersion = in.readInt(); } catch (EOFException eofe) { throw new LogHeaderCorruptException("Reached EOF when reading log header"); } if (verifyLayoutVersion && (logVersion < HdfsServerConstants.NAMENODE_LAYOUT_VERSION || // future version logVersion > Storage.LAST_UPGRADABLE_LAYOUT_VERSION)) { // unsupported throw new LogHeaderCorruptException("Unexpected version of the file system log file: " + logVersion + ". Current version = " + HdfsServerConstants.NAMENODE_LAYOUT_VERSION + "."); } return logVersion; }
From source file:r.base.Connections.java
public static byte[] decompress1(byte buffer[]) throws IOException, DataFormatException { DataInputStream in = new DataInputStream(new ByteArrayInputStream(buffer)); int outLength = in.readInt(); Inflater inflater = new Inflater(); inflater.setInput(buffer, 4, buffer.length - 4); byte[] result = new byte[outLength]; inflater.inflate(result);// ww w. j a v a 2 s . c o m inflater.end(); return result; }
From source file:net.sf.keystore_explorer.crypto.x509.X509ExtensionSet.java
/** * Load X.509 extension set./*w ww.j a va 2 s . c o m*/ * * @param is * Stream to load from * @return X.509 extension set * @throws X509ExtensionSetLoadException * If stream contents are not a valid X.509 extension set * @throws IOException * If an I/O problem occurred */ public static X509ExtensionSet load(InputStream is) throws X509ExtensionSetLoadException, IOException { DataInputStream dis = null; try { dis = new DataInputStream(is); long magicNumber = dis.readLong(); if (magicNumber != FILE_MAGIC_NUMBER) { throw new X509ExtensionSetLoadException( res.getString("NoLoadX509ExtensionSet.BadMagicNumber.exception.message")); } int version = dis.readInt(); if (version != FILE_VERSION) { throw new X509ExtensionSetLoadException( res.getString("NoLoadX509ExtensionSet.WrongVersion.exception.message")); } X509ExtensionSet x509ExtensionSet = new X509ExtensionSet(); x509ExtensionSet.criticalExtensions = loadExtensions(dis); x509ExtensionSet.nonCriticalExtensions = loadExtensions(dis); return x509ExtensionSet; } catch (EOFException ex) { throw new X509ExtensionSetLoadException( res.getString("NoLoadX509ExtensionSet.NotEnoughBytes.exception.message")); } finally { IOUtils.closeQuietly(dis); } }
From source file:org.kse.crypto.filetype.CryptoFileUtil.java
/** * Detect the KeyStore type contained in the supplied file. * * @param is//from www.j ava 2 s.co m * Input stream to detect type for * @return KeyStore type or null if none matched * @throws IOException * If an I/O problem occurred */ public static KeyStoreType detectKeyStoreType(InputStream is) throws IOException { byte[] contents = ReadUtil.readFully(is); DataInputStream dis = null; try { dis = new DataInputStream(new ByteArrayInputStream(contents)); // If less than 4 bytes are available it isn't a KeyStore if (dis.available() < 4) { return null; } // Read first integer (4 bytes) int i1 = dis.readInt(); // Test for JKS - starts with appropriate magic number if (i1 == JKS_MAGIC_NUMBER) { return JKS; } // Test for JCEKS - starts with appropriate magic number if (i1 == JCEKS_MAGIC_NUMBER) { return JCEKS; } // Test for BKS and UBER // Both start with a version number of 0, 1 or 2 if ((i1 == 0) || (i1 == 1) || (i1 == 2)) { /* * For BKS and UBER the last 20 bytes of the file are the SHA-1 * Hash while the byte before that is a ASN1Null (0) indicating * the end of the store. UBER, however, encrypts the store * content making it highly unlikely that the ASN1Null end byte * will be preserved. Therefore if the 21st byte from the end of * the file is a ASN1Null then the KeyStore is BKS */ if (contents.length < 26) { // Insufficient bytes to be BKS or UBER return null; } // Skip to 21st from last byte (file length minus 21 and the 4 bytes already read) dis.skip(contents.length - 25); // Read what may be the null byte if (dis.readByte() == 0) { // Found null byte - BKS/BKS-V1 if (i1 == 1) { return BKS_V1; } else { return BKS; } } else { // No null byte - UBER return UBER; } } } finally { IOUtils.closeQuietly(dis); } // @formatter:off /* * Test for PKCS #12. ASN.1 should look like this: * * PFX ::= ASN1Sequence { version ASN1Integer {v3(3)}(v3,...), authSafe * ContentInfo, macData MacData OPTIONAL */ // @formatter:on ASN1Primitive pfx = null; try { pfx = ASN1Primitive.fromByteArray(contents); } catch (IOException e) { // if it cannot be parsed as ASN1, it is certainly not a pfx key store return null; } // Is a sequence... if ((pfx != null) && (pfx instanceof ASN1Sequence)) { // Has two or three components... ASN1Sequence sequence = (ASN1Sequence) pfx; if ((sequence.size() == 2) || (sequence.size() == 3)) { // ...the first of which is a version of 3 ASN1Encodable firstComponent = sequence.getObjectAt(0); if (firstComponent instanceof ASN1Integer) { ASN1Integer version = (ASN1Integer) firstComponent; if (version.getValue().intValue() == 3) { return PKCS12; } } } } // KeyStore type not recognised return null; }