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.commoncrawl.service.crawlhistory.CrawlHistoryServer.java

@Override
public void bulkItemQuery(AsyncContext<BulkItemHistoryQuery, BulkItemHistoryQueryResponse> rpcContext)
        throws RPCException {
    LOG.info("Received BulkItemQueryRequest");
    ImmutableBuffer inputBuffer = rpcContext.getInput().getFingerprintList();

    if (inputBuffer.getCount() != 0) {
        try {//from  www. j  a  v a2 s  .co m

            if (_bloomFilter == null) {
                throw new IOException("BloomFilter Not Initilized. Invalid Server State!");
            }

            DataInputStream inputStream = new DataInputStream(
                    new ByteArrayInputStream(inputBuffer.getReadOnlyBytes(), 0, inputBuffer.getCount()));

            BitStream bitStreamOut = new BitStream();

            URLFPV2 fingerprint = new URLFPV2();

            int itemsPresent = 0;
            while (inputStream.available() != 0) {
                fingerprint.setDomainHash(WritableUtils.readVLong(inputStream));
                fingerprint.setUrlHash(WritableUtils.readVLong(inputStream));
                if (_bloomFilter.isPresent(fingerprint)) {
                    bitStreamOut.addbit(1);
                    ++itemsPresent;
                } else {
                    bitStreamOut.addbit(0);
                }
            }

            LOG.info("Received BulkItemQueryRequest Completed with " + itemsPresent + " items found");

            rpcContext.getOutput()
                    .setResponseList(new Buffer(bitStreamOut.bits, 0, (bitStreamOut.nbits + 7) / 8));
        } catch (IOException e) {
            LOG.error(CCStringUtils.stringifyException(e));
            rpcContext.setStatus(Status.Error_RequestFailed);
            rpcContext.setErrorDesc(CCStringUtils.stringifyException(e));
        }
        rpcContext.completeRequest();
    }

}

From source file:com.lines.activitys.SettingsActivity.java

/**
 * Delete the databases that already exist, and populate a new one with the
 * user's selected script/*w  w w . j a va  2 s .c  om*/
 * 
 * @param script
 *            - script file we are populating the database with
 * @throws IOException
 */
private void loadScript(String script) throws IOException {

    // Get adapters
    mDbAdapter = app.getPlayAdapter();
    mNDbAdapter = app.getNoteAdapter();

    // Reset tables for Script and Notes
    mDbAdapter.deleteTable();
    mNDbAdapter.deleteTable();

    String text = "";
    InputStream is;
    BufferedInputStream bis;
    DataInputStream dis;
    String line = null;
    int lineNo = -1;
    int actNo = 0;
    int pageNo = 1;

    // Add file extension back on
    script = script + ".txt";
    File file = new File(Environment.getExternalStorageDirectory() + "/learnyourlines/scripts/" + script);

    // Try to open the file, and alert the user if file doesn't exist
    try {
        is = new FileInputStream(file);
        bis = new BufferedInputStream(is);
        dis = new DataInputStream(bis);
    } catch (IOException e) {
        e.printStackTrace();
        return;
    }

    // Read line-by-line and pick out the relevent information
    while (dis.available() != 0) {

        lineNo++;

        line = dis.readLine();

        // Split line into array of words
        String words[] = line.split("\\s+");
        String firstWord = words[0];

        // Keep a count of which Act we're on
        if (firstWord.equals("FIRST") || firstWord.equals("SECOND") || firstWord.equals("THIRD")) {
            actNo++;
        }

        // Keep count of what page we're on (23 lines/page)
        if ((lineNo % 23) == 0 && lineNo != 0) {
            pageNo++;
        }

        // Check our firstWord is a character name
        if (isCharacter(firstWord)) {
            // If the first word doesn't contain a period (.) then we need
            // to take the second word as well as part of the character
            // name.
            if (!firstWord.contains(".")) {
                firstWord += " " + words[1];

                text = "";
                for (int j = 2; j < words.length; j++) {
                    text += words[j] + " ";
                }
                // If the second word is "and" then it is two characters
                // delievering a line and we need to get the other
                // characters name.
                if (words[1].equals("and")) {
                    firstWord += " " + words[2] + ".";

                    text = "";
                    for (int j = 3; j < words.length; j++) {
                        text += words[j] + " ";
                    }
                    // Handle the rest of the data that hasn't yet been
                    // filtered
                } else if (!words[1].contains(".")) {
                    firstWord = "STAGE.";

                    text = "";
                    for (int j = 0; j < words.length; j++) {
                        text += words[j] + " ";
                    }
                }
            }
            // If the firstWord isn't a character, then it is a stage
            // direction
        } else {
            firstWord = "STAGE.";

            text = "";
            for (int j = 0; j < words.length; j++) {
                text += words[j] + " ";
            }
        }

        // If we didn't manage to populate "text" from the previous if
        // statements, then do it here.
        if (text.equals("")) {
            for (int j = 1; j < words.length; j++) {
                text += words[j] + " ";
            }
        }

        // Once we have all the data picked out from current line in text
        // file, create a new row in the database. Filter out text we don't
        // want in our database.
        if (!isAllUpperCase(words[0])) {
            firstWord = firstWord.substring(0, firstWord.length() - 1);
            mDbAdapter.createPlay(lineNo, firstWord, text, actNo, pageNo, "N", "N", 0, 0, 0);
            // If we're not adding to the database, then we need to reduce
            // the line count.
        } else {
            lineNo--;
        }

        // Clear "text" before we read next line
        text = "";
    }

    // Cleanup
    is.close();
    bis.close();
    dis.close();

    finish();

}

From source file:org.apache.hadoop.hive.sql.QTestUtil.java

public void addFile(File qf) throws Exception {

    FileInputStream fis = new FileInputStream(qf);
    BufferedInputStream bis = new BufferedInputStream(fis);
    DataInputStream dis = new DataInputStream(bis);
    StringBuilder qsb = new StringBuilder();

    // Look for a hint to not run a test on some Hadoop versions
    Pattern pattern = Pattern.compile("-- EXCLUDE_HADOOP_MAJOR_VERSIONS(.*)");

    // Read the entire query
    boolean excludeQuery = false;
    String hadoopVer = ShimLoader.getMajorVersion();
    while (dis.available() != 0) {
        String line = dis.readLine();

        // While we are reading the lines, detect whether this query wants to be
        // excluded from running because the Hadoop version is incorrect
        Matcher matcher = pattern.matcher(line);
        if (matcher.find()) {
            String group = matcher.group();
            int start = group.indexOf('(');
            int end = group.indexOf(')');
            assert end > start;
            // versions might be something like '0.17, 0.19'
            String versions = group.substring(start + 1, end);

            Set<String> excludedVersionSet = new HashSet<String>();
            for (String s : versions.split("\\,")) {
                s = s.trim();//from  w  ww  .  ja va2s  . c  o  m
                excludedVersionSet.add(s);
            }
            if (excludedVersionSet.contains(hadoopVer)) {
                excludeQuery = true;
            }
        }
        qsb.append(line + "\n");
    }
    qMap.put(qf.getName(), qsb.toString());
    if (excludeQuery) {
        System.out.println("Due to the Hadoop Version (" + hadoopVer + "), " + "adding query " + qf.getName()
                + " to the set of tests to skip");
        qSkipSet.add(qf.getName());
    }
    dis.close();
}

From source file:org.apache.hadoop.hive.ql.QTestUtil2.java

public void addFile(File qf) throws Exception {

    FileInputStream fis = new FileInputStream(qf);
    BufferedInputStream bis = new BufferedInputStream(fis);
    DataInputStream dis = new DataInputStream(bis);
    StringBuilder qsb = new StringBuilder();

    // Look for a hint to not run a test on some Hadoop versions
    Pattern pattern = Pattern.compile("-- EXCLUDE_HADOOP_MAJOR_VERSIONS(.*)");

    // Read the entire query
    boolean excludeQuery = false;
    String hadoopVer = ShimLoader.getMajorVersion();
    while (dis.available() != 0) {
        String line = dis.readLine();

        // While we are reading the lines, detect whether this query wants
        // to be/* w  ww. j av  a  2 s.co m*/
        // excluded from running because the Hadoop version is incorrect
        Matcher matcher = pattern.matcher(line);
        if (matcher.find()) {
            String group = matcher.group();
            int start = group.indexOf('(');
            int end = group.indexOf(')');
            assert end > start;
            // versions might be something like '0.17, 0.19'
            String versions = group.substring(start + 1, end);

            Set<String> excludedVersionSet = new HashSet<String>();
            for (String s : versions.split("\\,")) {
                s = s.trim();
                excludedVersionSet.add(s);
            }
            if (excludedVersionSet.contains(hadoopVer)) {
                excludeQuery = true;
            }
        }
        qsb.append(line + "\n");
    }
    qMap.put(qf.getName(), qsb.toString());
    if (excludeQuery) {
        System.out.println("Due to the Hadoop Version (" + hadoopVer + "), " + "adding query " + qf.getName()
                + " to the set of tests to skip");
        qSkipSet.add(qf.getName());
    }
    dis.close();
}

From source file:org.mrgeo.vector.mrsvector.VectorTile.java

protected void readOSMData(final InputStream stream) throws IOException {
    DataInputStream dis;
    if (stream instanceof DataInputStream) {
        dis = (DataInputStream) stream;
    } else {/*from   ww  w .  j  a v  a  2  s. co  m*/
        dis = new DataInputStream(stream);
    }

    while (dis.available() > 0) {
        final BlobHeader header = parseHeader(dis);

        if (header.getType().equals(OSMDATA)) {
            final CodedInputStream blob = CodedInputStream.newInstance(parseBlob(dis, header));

            // for speed, we make _large_ tiles, we don't want protobuf to complain...
            blob.setSizeLimit(MAX_STREAM_SIZE);

            parseOSM(blob);
        }
    }
}

From source file:org.ejbca.core.protocol.cmp.CmpTestCase.java

/**
 * /*from  ww  w.  ja va  2  s .c  o  m*/
 * @param message
 * @param type set to 5 when sending a PKI request, 3 when sending a PKIConf
 * @return
 * @throws IOException
 * @throws NoSuchProviderException
 */
protected byte[] sendCmpTcp(byte[] message, int type) throws IOException, NoSuchProviderException {
    final String host = getProperty("tcpCmpProxyIP", this.CMP_HOST);
    final int port = getProperty("tcpCmpProxyPort", PORT_NUMBER);
    try {
        final Socket socket = new Socket(host, port);

        final byte[] msg = createTcpMessage(message);
        try {
            final BufferedOutputStream os = new BufferedOutputStream(socket.getOutputStream());
            os.write(msg);
            os.flush();

            DataInputStream dis = new DataInputStream(socket.getInputStream());

            // Read the length, 32 bits
            final int len = dis.readInt();
            log.info("Got a message claiming to be of length: " + len);
            // Read the version, 8 bits. Version should be 10 (protocol draft nr
            // 5)
            final int ver = dis.readByte();
            log.info("Got a message with version: " + ver);
            assertEquals(ver, 10);

            // Read flags, 8 bits for version 10
            final byte flags = dis.readByte();
            log.info("Got a message with flags (1 means close): " + flags);
            // Check if the client wants us to close the connection (LSB is 1 in
            // that case according to spec)

            // Read message type, 8 bits
            final int msgType = dis.readByte();
            log.info("Got a message of type: " + msgType);
            assertEquals(msgType, type);

            // Read message
            final ByteArrayOutputStream baos = new ByteArrayOutputStream(3072);
            while (dis.available() > 0) {
                baos.write(dis.read());
            }

            log.info("Read " + baos.size() + " bytes");
            final byte[] respBytes = baos.toByteArray();
            assertNotNull(respBytes);
            assertTrue(respBytes.length > 0);
            return respBytes;
        } finally {
            socket.close();
        }
    } catch (ConnectException e) {
        assertTrue("This test requires a CMP TCP listener to be configured on " + host + ":" + port
                + ". Edit conf/cmptcp.properties and redeploy.", false);
    } catch (EOFException e) {
        assertTrue("Response was malformed.", false);
    } catch (Exception e) {
        e.printStackTrace();
        assertTrue(false);
    }
    return null;
}

From source file:com.isecpartners.gizmo.HttpResponse.java

public void processResponse(InputStream in) throws FailedRequestException {
    StringBuffer content = new StringBuffer();
    DataInputStream inputStream = new DataInputStream(in);
    ArrayByteList blist = new ArrayByteList();
    String header = null;/*from  ww w. j  a  v  a2 s .c  o m*/
    int contentLength = 0;
    boolean isChunked = false;
    String line;
    try {
        line = readline(inputStream);
        while (line != null && !line.equals(ENDL)) {
            content.append(line);
            if (line.toUpperCase().contains(CONTENT_LENGTH)
                    && line.toUpperCase().indexOf(CONTENT_LENGTH) == 0) {
                String value = line.substring(line.indexOf(CONTENT_LENGTH) + CONTENT_LENGTH.length() + 2,
                        line.indexOf('\r'));
                contentLength = Integer.parseInt(value.trim());
            } else if (line.toUpperCase().contains(TRANSFER_ENCODING)) {
                if (line.toUpperCase()
                        .substring(
                                line.toUpperCase().indexOf(TRANSFER_ENCODING) + "Transfer-Encoding:".length())
                        .contains("CHUNKED")) {
                    isChunked = true;
                }
            } else if (line.toUpperCase().contains(CONTENT_ENCODING)) {
                String value = line.substring(line.indexOf(CONTENT_ENCODING) + CONTENT_ENCODING.length() + 2,
                        line.indexOf('\r'));
                value = value.trim();
                if (value.toUpperCase().equals("GZIP")) {
                    this.gzip = true;
                } else if (value.toUpperCase().equals("DEFLATE")) {
                    this.deflate = true;
                }
            }
            line = readline(inputStream);
        }
        if (line == null) {
            GizmoView.log(content.toString());
            throw new FailedRequestException();
        }

        content.append("\r\n");
        header = content.substring(0, content.indexOf("\r\n"));
        append(blist, content);

        if (contentLength != 0) {
            for (int ii = 0; ii < contentLength; ii++) {
                blist.add(inputStream.readByte());
            }
        }

        if (isChunked) {
            boolean isDone = false;
            while (!isDone) {
                byte current = inputStream.readByte();
                blist.add(current);

                int size = 0;
                while (current != '\n') {
                    if (current != '\r') {
                        size *= 16;
                        if (Character.isLetter((char) current)) {
                            current = (byte) Character.toLowerCase((char) current);
                        }
                        if ((current >= '0') && (current <= '9')) {
                            size += (current - 48);
                        } else if ((current >= 'a') && (current <= 'f')) {
                            size += (10 + current - 97);
                        }
                    }
                    current = inputStream.readByte();

                    while ((char) current == ' ') {
                        current = inputStream.readByte();
                    }
                    blist.add(current);
                }

                if (size != 0) {
                    for (int ii = 0; ii < size; ii++) {
                        int byte1 = inputStream.readByte();
                        byte blah = (byte) byte1;
                        blist.add(blah);
                    }
                    blist.add(inputStream.readByte());
                    blist.add(inputStream.readByte());
                } else {
                    byte ch = (byte) inputStream.read();
                    StringBuffer endstuff = new StringBuffer();
                    blist.add(ch);
                    endstuff.append((char) ch);
                    while (ch != '\n') {
                        ch = inputStream.readByte();
                        endstuff.append((char) ch);
                        blist.add(ch);
                    }

                    isDone = true;
                }

            }
        }

        if (inputStream.available() > 0) {
            try {
                while (true) {
                    blist.add(inputStream.readByte());
                }
            } catch (EOFException e) {
                System.out.println(e);
            }
        }
    } catch (IOException ex) {
        Logger.getLogger(HttpResponse.class.getName()).log(Level.SEVERE, null, ex);
    }

    setBlist(blist);
    setHeader(header);
    if (this.gzip) {
        addContents(unzipData(blist.toArray()));
    } else if (this.deflate) {
        addContents(deflateData(blist.toArray()));
    } else {
        addContents(content.toString());
    }
}