Example usage for java.io DataInputStream available

List of usage examples for java.io DataInputStream available

Introduction

In this page you can find the example usage for java.io DataInputStream available.

Prototype

public int available() throws IOException 

Source Link

Document

Returns an estimate of the number of bytes that can be read (or skipped over) from this input stream without blocking by the next caller of a method for this input stream.

Usage

From source file:org.apache.hadoop.io.TestBufferedByteInputOutput.java

/**
 * Test reading from closed buffer./*from  w ww  .  j av  a 2s.c  om*/
 */
@Test
public void testCloseInput() throws IOException {
    LOG.info("Running test close input");
    setUp(1000);

    // input is of size 1000, so the ReadThread will attempt to write to
    // the buffer, which will fail, but we should be able to read 100 bytes
    ByteArrayInputStream is = new ByteArrayInputStream(input);
    DataInputStream dis = BufferedByteInputStream.wrapInputStream(is, 100, 10);

    // wait for the thread to read from is and
    // write to the buffer
    while (dis.available() < 100) {
        sleep(10);
    }

    // no more writes to the internal buffer
    dis.close();

    try {
        dis.read(); // read will call DataInputStream fill() which should fail
        fail("Read should fail because we are closed");
    } catch (Exception e) {
        LOG.info("Expected exception " + e.getMessage());
    }

    dis.close(); // can call multiple close()  

    try {
        dis.read(new byte[10], 0, 10);
        fail("Read should fail because we are closed");
    } catch (Exception e) {
        LOG.info("Expected exception " + e.getMessage());
    }

    try {
        dis.available();
        fail("Available should fail because we are closed");
    } catch (Exception e) {
        LOG.info("Expected exception " + e.getMessage());
    }
}

From source file:com.jk.framework.util.FakeRunnable.java

/**
 * Read stream.//  ww  w . j ava  2 s.c o  m
 *
 * @param inStream
 *            InputStream
 * @return String
 * @throws IOException
 *             Signals that an I/O exception has occurred.
 */
public static byte[] readStream(final InputStream inStream) throws IOException {
    DataInputStream in = null;
    try {
        in = new DataInputStream(inStream);
        final int size = in.available();
        final byte arr[] = new byte[size];
        in.readFully(arr);
        return arr;
    } finally {
        if (in != null) {
            in.close();
        }
    }
}

From source file:com.photon.phresco.framework.impl.CIManagerImpl.java

private void setSvnCredential(CIJob job) throws JDOMException, IOException {
    S_LOGGER.debug("Entering Method CIManagerImpl.setSvnCredential");
    try {/*from  w  w w  .j a va 2  s. c  o  m*/
        InputStream credentialXml = PhrescoFrameworkFactory.getServiceManager().getCredentialXml();
        SvnProcessor processor = new SvnProcessor(credentialXml);
        DataInputStream in = new DataInputStream(credentialXml);
        while (in.available() != 0) {
            System.out.println(in.readLine());
        }

        in.close();
        processor.changeNodeValue("credentials/entry//userName", job.getUserName());
        processor.changeNodeValue("credentials/entry//password", job.getPassword());

        processor.writeStream(new File(Utility.getJenkinsHome() + File.separator + job.getName()));

        //jenkins home location
        String jenkinsJobHome = System.getenv(JENKINS_HOME);
        StringBuilder builder = new StringBuilder(jenkinsJobHome);
        builder.append(File.separator);

        processor.writeStream(new File(builder.toString() + CI_CREDENTIAL_XML));
    } catch (Exception e) {
        S_LOGGER.error(
                "Entered into the catch block of CIManagerImpl.setSvnCredential " + e.getLocalizedMessage());
    }
}

From source file:com.photon.phresco.framework.impl.CIManagerImpl.java

public void setMailCredential(CIJob job) {
    S_LOGGER.debug("Entering Method CIManagerImpl.setMailCredential");
    try {/*ww  w  . ja  va 2s  . co m*/
        InputStream credentialXml = PhrescoFrameworkFactory.getServiceManager().getMailerXml();
        SvnProcessor processor = new SvnProcessor(credentialXml);
        DataInputStream in = new DataInputStream(credentialXml);
        while (in.available() != 0) {
            System.out.println(in.readLine());
        }

        in.close();
        // Mail have to with jenkins running email address
        InetAddress ownIP = InetAddress.getLocalHost();
        processor.changeNodeValue(CI_HUDSONURL, HTTP_PROTOCOL + PROTOCOL_POSTFIX + ownIP.getHostAddress()
                + COLON + job.getJenkinsPort() + FORWARD_SLASH + CI + FORWARD_SLASH);
        processor.changeNodeValue("smtpAuthUsername", job.getSenderEmailId());
        processor.changeNodeValue("smtpAuthPassword", job.getSenderEmailPassword());
        processor.changeNodeValue("adminAddress", job.getSenderEmailId());

        //jenkins home location
        String jenkinsJobHome = System.getenv(JENKINS_HOME);
        StringBuilder builder = new StringBuilder(jenkinsJobHome);
        builder.append(File.separator);

        processor.writeStream(new File(builder.toString() + CI_MAILER_XML));
    } catch (Exception e) {
        S_LOGGER.error(
                "Entered into the catch block of CIManagerImpl.setMailCredential " + e.getLocalizedMessage());
    }
}

From source file:view.CertificateManagementDialog.java

private InputStream fullStream(String fname) throws IOException {
    FileInputStream fis = new FileInputStream(fname);
    DataInputStream dis = new DataInputStream(fis);
    byte[] bytes = new byte[dis.available()];
    dis.readFully(bytes);//from  www.ja  va2  s.c  o m
    ByteArrayInputStream bais = new ByteArrayInputStream(bytes);
    return bais;
}

From source file:ubic.basecode.io.ByteArrayConverter.java

/**
 * @param barray// ww w.java2s.co m
 * @return boolean[]
 */
public boolean[] byteArrayToBooleans(byte[] barray) {
    if (barray == null)
        return null;
    ByteArrayInputStream bis = new ByteArrayInputStream(barray);
    DataInputStream dis = new DataInputStream(bis);
    boolean[] iarray = new boolean[barray.length / BOOL_SIZE];
    int i = 0;

    try {
        while (dis.available() > 0) {
            iarray[i] = dis.readBoolean();
            i++;
        }
        return iarray;

    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        try {
            dis.close();
            bis.close();

        } catch (IOException e) {
            throw new RuntimeException(e);

        }
    }

}

From source file:com.hadoopvietnam.cache.memcached.MemcachedCache.java

/**
 * Convert the memcached object into a List&lt;Long&gt;.
 *
 * @param inBytesOfLongs the byte[] to convert
 * @return the byte[] as List&lt;Long&gt;, null if not valid or empty bytes
 * @throws IOException thrown if any errors
 *///from   w w w  . j  a v  a2s  . c  om
protected ArrayList<Long> getListFromBytes(final Object inBytesOfLongs) throws IOException {
    if (inBytesOfLongs == null) {
        return null;
    }

    ArrayList<Long> toReturn = new ArrayList<Long>();

    ByteArrayInputStream bytes = new ByteArrayInputStream((byte[]) inBytesOfLongs);
    DataInputStream input = new DataInputStream(bytes);
    try {
        while (input.available() > 0) {
            toReturn.add(input.readLong());
        }
    } finally {
        input.close();
    }

    return toReturn;
}

From source file:netscape.security.pkcs.PKCS7.java

/**
 * Unmarshals a PKCS7 block from its encoded form, parsing the
 * encoded bytes from the InputStream.//from  w w  w .  j  av  a  2  s . com
 *
 * @param in an input stream holding at least one PKCS7 block.
 * @exception ParsingException on parsing errors.
 * @exception IOException on other errors.
 */
public PKCS7(InputStream in) throws ParsingException, IOException {
    DataInputStream dis = new DataInputStream(in);

    int len = 0;
    byte[] newbuf = new byte[len];
    byte[] oldbuf = new byte[len];
    byte[] data = new byte[len];

    do {
        newbuf = new byte[dis.available()];
        len += dis.available();
        dis.readFully(newbuf);
        data = new byte[len];

        System.arraycopy(oldbuf, 0, data, 0, oldbuf.length);
        System.arraycopy(newbuf, 0, data, oldbuf.length, newbuf.length);
        oldbuf = new byte[len];
        System.arraycopy(data, 0, oldbuf, 0, data.length);

    } while (dis.available() > 0);

    parse(new DerInputStream(data));
}

From source file:org.pentaho.di.job.entries.filecompare.JobEntryFileCompare.java

/**
 * Check whether 2 files have the same contents.
 *
 * @param file1/*w  ww .j a  va 2 s.c o m*/
 *          first file to compare
 * @param file2
 *          second file to compare
 * @return true if files are equal, false if they are not
 *
 * @throws IOException
 *           upon IO problems
 */
protected boolean equalFileContents(FileObject file1, FileObject file2) throws KettleFileException {
    // Really read the contents and do comparisons
    DataInputStream in1 = null;
    DataInputStream in2 = null;
    try {
        in1 = new DataInputStream(
                new BufferedInputStream(KettleVFS.getInputStream(KettleVFS.getFilename(file1), this)));
        in2 = new DataInputStream(
                new BufferedInputStream(KettleVFS.getInputStream(KettleVFS.getFilename(file2), this)));

        char ch1, ch2;
        while (in1.available() != 0 && in2.available() != 0) {
            ch1 = (char) in1.readByte();
            ch2 = (char) in2.readByte();
            if (ch1 != ch2) {
                return false;
            }
        }
        if (in1.available() != in2.available()) {
            return false;
        } else {
            return true;
        }
    } catch (IOException e) {
        throw new KettleFileException(e);
    } finally {
        if (in1 != null) {
            try {
                in1.close();
            } catch (IOException ignored) {
                // Nothing to do here
            }
        }
        if (in2 != null) {
            try {
                in2.close();
            } catch (IOException ignored) {
                // Nothing to see here...
            }
        }
    }
}

From source file:ee.sk.digidoc.SignedDoc.java

/**
 * Reads in data file//www  .  java2s  .c  o  m
 * @param inFile input file
 */
public static byte[] readFile(File inFile) throws IOException, FileNotFoundException {
    byte[] data = null;
    FileInputStream is = new FileInputStream(inFile);
    DataInputStream dis = new DataInputStream(is);
    data = new byte[dis.available()];
    dis.readFully(data);
    dis.close();
    is.close();
    return data;
}