List of usage examples for java.io DataInputStream readFully
public final void readFully(byte b[]) throws IOException
From source file:org.mule.transport.tcp.protocols.LengthProtocol.java
public Object read(InputStream is) throws IOException { // original comments indicated that we need to use read(byte[]) rather than readInt() // to avoid socket timeouts - don't understand, but don't want to risk change. // first read the data necessary to know the length of the payload DataInputStream dis = new DataInputStream(is); dis.mark(SIZE_INT);/* w w w.j ava 2s .c o m*/ // this pulls through SIZE_INT bytes if (null == super.read(dis, SIZE_INT)) { return null; // eof } // reset and read the integer dis.reset(); int length = dis.readInt(); if (logger.isDebugEnabled()) { logger.debug("length: " + length); } if (length < 0 || (getMaxMessageLength() > 0 && length > getMaxMessageLength())) { throw new IOException("Length " + length + " exceeds limit: " + getMaxMessageLength()); } // finally read the rest of the data byte[] buffer = new byte[length]; dis.readFully(buffer); if (logger.isDebugEnabled()) { logger.debug("length read: " + buffer.length); } return buffer; }
From source file:com.limegroup.gnutella.metadata.ASFParser.java
/** Parses stream properties to see if we have audio or video data. */ private void parseStreamProperties(DataInputStream ds) throws IOException { LOG.debug("Parsing stream properties"); byte[] streamID = new byte[16]; ds.readFully(streamID); if (Arrays.equals(streamID, IDs.AUDIO_STREAM_ID)) { _hasAudio = true;//from w ww. j a va2s . c om } else if (Arrays.equals(streamID, IDs.VIDEO_STREAM_ID)) { _hasVideo = true; IOUtils.ensureSkip(ds, 38); _width = ByteUtils.leb2int(ds); if (_width < 0) throw new IOException("ASF file corrupt. Video width excessive:" + ByteUtils.uint2long(_width)); _height = ByteUtils.leb2int(ds); if (_height < 0) throw new IOException("ASF file corrupt. Video height excessive:" + ByteUtils.uint2long(_height)); } // we aren't reading everything, but we'll skip over just fine. }
From source file:com.limegroup.gnutella.metadata.ASFParser.java
/** * Parses a ASF input stream's metadata. * This first checks that the marker (16 bytes) is correct, reads the data offset & object count, * and then iterates through the objects, reading them. * Each object is stored in the format:/*from ww w .j av a 2 s. c o m*/ * <xmp> * ObjectID (16 bytes) * Object Size (4 bytes) * Object (Object Size bytes) * </xmp> */ private void parse(InputStream is) throws IOException { CountingInputStream counter = new CountingInputStream(is); DataInputStream ds = new DataInputStream(counter); byte[] marker = new byte[IDs.HEADER_ID.length]; ds.readFully(marker); if (!Arrays.equals(marker, IDs.HEADER_ID)) throw new IOException("not an ASF file"); long dataOffset = ByteUtils.leb2long(ds); int objectCount = ByteUtils.leb2int(ds); IOUtils.ensureSkip(ds, 2); if (LOG.isDebugEnabled()) LOG.debug("Data Offset: " + dataOffset + ", objectCount: " + objectCount); if (dataOffset < 0) throw new IOException("ASF file is corrupt. Data offset negative:" + dataOffset); if (objectCount < 0) throw new IOException( "ASF file is corrupt. Object count unreasonable:" + ByteUtils.uint2long(objectCount)); if (objectCount > 100) throw new IOException("object count very high: " + objectCount); byte[] object = new byte[16]; for (int i = 0; i < objectCount; i++) { if (LOG.isDebugEnabled()) LOG.debug("Parsing object[" + i + "]"); ds.readFully(object); long size = ByteUtils.leb2long(ds) - 24; if (size < 0) throw new IOException("ASF file is corrupt. Object size < 0 :" + size); counter.clearAmountRead(); readObject(ds, object, size); int read = counter.getAmountRead(); if (read > size) throw new IOException("read (" + read + ") more than size (" + size + ")"); else if (read != size) { if (LOG.isDebugEnabled()) LOG.debug("Skipping to next object. Read: " + read + ", size: " + size); IOUtils.ensureSkip(ds, size - read); } } }
From source file:org.kualigan.maven.plugins.api.DefaultPrototypeHelper.java
/** * Puts temporary pom in the system temp directory. prototype-pom.xml is extracted * from the plugin.//from w ww.j av a 2 s. co m */ public void extractTempPom() throws MojoExecutionException { getCaller().getLog().info("Extracting the Temp Pom"); final InputStream pom_is = getClass().getClassLoader().getResourceAsStream("prototype-resources/pom.xml"); byte[] fileBytes = null; try { final DataInputStream dis = new DataInputStream(pom_is); fileBytes = new byte[dis.available()]; dis.readFully(fileBytes); dis.close(); } catch (Exception e) { throw new MojoExecutionException("Wasn't able to read in the prototype pom", e); } finally { try { pom_is.close(); } catch (Exception e) { // Ignore exceptions } } try { final FileOutputStream fos = new FileOutputStream( System.getProperty("java.io.tmpdir") + File.separator + "prototype-pom.xml"); try { fos.write(fileBytes); } finally { fos.close(); } } catch (Exception e) { throw new MojoExecutionException("Could not write temporary pom file", e); } }
From source file:org.apache.jackrabbit.core.persistence.bundle.util.BundleBinding.java
/** * Write a small binary value and return the data. * * @param out the output stream to write * @param blobVal the binary value/*from w ww . j a v a2 s . com*/ * @param state the property state (for error messages) * @param i the index (for error messages) * @return the data * @throws IOException if the data could not be read */ private byte[] writeSmallBinary(DataOutputStream out, InternalValue value, NodePropBundle.PropertyEntry state, int i) throws IOException { try { int size = (int) value.getLength(); out.writeInt(size); byte[] data = new byte[size]; DataInputStream in = new DataInputStream(value.getStream()); try { in.readFully(data); } finally { IOUtils.closeQuietly(in); } out.write(data, 0, data.length); return data; } catch (Exception e) { String msg = "Error while storing blob. id=" + state.getId() + " idx=" + i + " value=" + value; log.error(msg, e); throw new IOException(msg); } }
From source file:org.ow2.proactive.authentication.crypto.Credentials.java
/** * Creates a Credentials given its base64 encoded representation * /*from w w w. j a v a2s .c om*/ * @param base64enc the Credentials representation as a base64 encoded byte array, * as returned by {@link Credentials#getBase64()} * @return the Credentials object corresponding the <code>base64en</code> representation * @throws KeyException */ public static Credentials getCredentialsBase64(byte[] base64enc) throws KeyException { String algo = "", cipher = "", tmp = ""; byte[] data; byte[] aes; int size; byte[] asciiEnc; try { asciiEnc = Base64.decodeBase64(base64enc); } catch (Exception e) { throw new KeyException("Unable to decode base64 credentials", e); } try { DataInputStream in = new DataInputStream(new ByteArrayInputStream(asciiEnc)); int read, tot = 0; while ((read = in.read()) != '\n') { if (read == -1) throw new KeyException("Failed to parse malformed credentials"); algo += (char) read; tot++; } tot++; while ((read = in.read()) != '\n') { if (read == -1) throw new KeyException("Failed to parse malformed credentials"); tmp += (char) read; tot++; } tot++; size = Integer.parseInt(tmp); while ((read = in.read()) != '\n') { if (read == -1) throw new KeyException("Failed to parse malformed credentials"); cipher += (char) read; tot++; } tot++; aes = new byte[size / 8]; for (int i = 0; i < size / 8; i++) { aes[i] = (byte) in.read(); tot++; } data = new byte[asciiEnc.length - tot]; in.readFully(data); } catch (Exception e) { throw new KeyException("Could not decode credentials", e); } return new Credentials(algo, size, cipher, aes, data); }
From source file:org.kawanfw.file.reflection.Reloader.java
private byte[] loadClassData(String className) throws IOException, ClassNotFoundException { DataInputStream dis = null; try {/*from ww w . j av a 2 s . c om*/ debug("className: " + className); /* * get the actual path using the original class loader */ Class<?> clazz = orig.loadClass(className); String simpleName = StringUtils.substringAfterLast(className, "."); debug("clazz : " + clazz); debug("clazz.getSimpleName(): " + simpleName); url = clazz.getResource(simpleName + ".class"); debug("url: " + url); /* * force reload */ File file = new File(url.toURI()); int size = (int) file.length(); byte buff[] = new byte[size]; dis = new DataInputStream(new FileInputStream(file)); dis.readFully(buff); return buff; } catch (ClassNotFoundException ex) { throw ex; } catch (Exception ex) { throw new IOException(ex); } finally { if (dis != null) { dis.close(); } } }
From source file:org.mule.transport.comm.protocols.LengthProtocol.java
public Object read(InputStream is) throws IOException { // original comments indicated that we need to use read(byte[]) rather than readInt() // to avoid socket timeouts - don't understand, but don't want to risk change. // first read the data necessary to know the length of the payload DataInputStream dis = new DataInputStream(is); dis.mark(SIZE_INT);/*w w w .ja v a2s. co m*/ // this pulls through SIZE_INT bytes if (null == super.read(dis, SIZE_INT)) { return null; // eof } // reset and read the integer dis.reset(); int length = dis.readInt(); if (logger.isDebugEnabled()) { logger.debug("length: " + length); } if (length < 0 || (getMaxMessageLength() > 0 && length > getMaxMessageLength())) { // throw new IOException("Length " + length + " exceeds limit: " + getMaxMessageLength()); System.out.println("Length " + length + " exceeds limit: " + getMaxMessageLength()); } // finally read the rest of the data if (length > 0) { byte[] buffer = new byte[0]; dis.readFully(buffer); if (logger.isDebugEnabled()) { logger.debug("length read: " + buffer.length); } return buffer; } else { byte[] buffer = new byte[0]; return buffer; } }
From source file:org.commoncrawl.service.listcrawler.HDFSFileIndex.java
private void loadIndexFromLocalFile() throws IOException { LOG.info("Loading Index from Local File:" + _localIndexFilePath); // now open an input stream to the local file ... FileInputStream fileInputStream = new FileInputStream(_localIndexFilePath); DataInputStream dataStream = new DataInputStream(fileInputStream); try {/*from ww w . ja v a2s . c om*/ // deserialize bloom filter _bloomFilter = BloomFilter.serializer().deserialize(dataStream); _indexHintCount = dataStream.readInt(); int indexHintDataSize = _indexHintCount * INDEX_HINT_SIZE; // and deserialize index hints _indexHints = ByteBuffer.allocate(indexHintDataSize); dataStream.readFully(_indexHints.array()); // load index data buffer size _indexDataSize = dataStream.readInt(); // and capture offset information _indexDataOffset = (int) fileInputStream.getChannel().position(); } finally { if (fileInputStream != null) { fileInputStream.close(); } } LOG.info("Successfully loaded Index"); }
From source file:org.echocat.jomon.net.dns.DnsServer.java
public void TCPclient(Socket s) { try {/*w w w.jav a 2 s . c om*/ final int inLength; final DataInputStream dataIn; final DataOutputStream dataOut; final byte[] in; final InputStream is = s.getInputStream(); dataIn = new DataInputStream(is); inLength = dataIn.readUnsignedShort(); in = new byte[inLength]; dataIn.readFully(in); final Message query; byte[] response; try { query = new Message(in); response = generateReply(query, in, in.length, s); if (response == null) { return; } } catch (final IOException ignored) { response = formerrMessage(in); } dataOut = new DataOutputStream(s.getOutputStream()); dataOut.writeShort(response.length); dataOut.write(response); } catch (final IOException e) { LOG.warn("TCPclient(" + addrport(s.getLocalAddress(), s.getLocalPort()) + ").", e); } finally { try { s.close(); } catch (final IOException ignored) { } } }