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:nl.systemsgenetics.cellTypeSpecificAlleleSpecificExpression.ReadGenoAndAsFromIndividual.java

/**
 *
 * @param individual_names//  ww w .jav  a 2s .c o m
 * @param coupling_location
 * @return
 * @throws FileNotFoundException
 * @throws IOException
 * 
 */

public static HashMap convert_individual_names(String[] individual_names, String coupling_location)
        throws FileNotFoundException, IOException {

    String coupling_loc = coupling_location;

    //This will be filled while reading the file
    ArrayList<String> sample_names_in_file = new ArrayList<String>();
    ArrayList<String> individual_names_in_file = new ArrayList<String>();

    //This will be the one that is later returned
    HashMap ordered_sample_names = new HashMap();

    File coupling_file = new File(coupling_loc);

    FileInputStream fis;
    BufferedInputStream bis;
    DataInputStream dis;

    fis = new FileInputStream(coupling_file);

    // Here BufferedInputStream is added for fast reading.
    bis = new BufferedInputStream(fis);
    dis = new DataInputStream(bis);

    // dis.available() returns 0 if the file does not have more lines.
    int i = 0;
    while (dis.available() != 0) {

        String[] curr_line = dis.readLine().split("\t");
        individual_names_in_file.add(i, curr_line[0]);
        sample_names_in_file.add(i, curr_line[1]);

        //print the individual al line for checking.
        //System.out.println("line: " + " " + curr_line[0] +" " + curr_line[1] );

    }

    // dispose all the resources after using them.
    fis.close();
    bis.close();
    dis.close();

    i = 0;
    for (String i_name : individual_names) {
        int index = individual_names_in_file.indexOf(i_name);

        if (index >= 0) {
            //We find a name in the genotype folder in the individual names stuff.
            ordered_sample_names.put(sample_names_in_file.get(index), i);
        }
        i++;

    }
    return (ordered_sample_names);

}

From source file:org.rifidi.edge.adapter.csl.util.CslRfidTagServer.java

public static void clearReadBuffer(DataInputStream data) {
    byte[] inData = new byte[1024];
    try {/*w ww .  ja  va  2  s . c o m*/
        while (data.available() != 0)
            data.read(inData);
    } catch (IOException e) {
        logger.error("Could not clear buffer: " + e.getMessage());
    }
}

From source file:org.apache.cassandra.db.SystemKeyspace.java

private static Pair<ReplayPosition, Long> truncationRecordFromBlob(ByteBuffer bytes) {
    try {/*from   ww w  . j  av a 2  s.c o  m*/
        DataInputStream in = new DataInputStream(ByteBufferUtil.inputStream(bytes));
        return Pair.create(ReplayPosition.serializer.deserialize(in),
                in.available() > 0 ? in.readLong() : Long.MIN_VALUE);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}

From source file:clueweb09.WarcRecord.java

private static String readLineFromInputStream(DataInputStream in) throws IOException {
    StringBuilder retString = new StringBuilder();
    boolean found_cr = false;
    boolean keepReading = true;
    try {//from   www.j  a v a 2 s  .co m
        do {
            char thisChar = 0;
            byte readByte = in.readByte();
            // check to see if it's a multibyte character
            if ((readByte & MASK_THREE_BYTE_CHAR) == MASK_THREE_BYTE_CHAR) {
                found_cr = false;
                // need to read the next 2 bytes
                if (in.available() < 2) {
                    // treat these all as individual characters
                    retString.append((char) readByte);
                    int numAvailable = in.available();
                    for (int i = 0; i < numAvailable; i++) {
                        retString.append((char) (in.readByte()));
                    }
                    continue;
                }
                byte secondByte = in.readByte();
                byte thirdByte = in.readByte();
                // ensure the topmost bit is set
                if (((secondByte & MASK_TOPMOST_BIT) != MASK_TOPMOST_BIT)
                        || ((thirdByte & MASK_TOPMOST_BIT) != MASK_TOPMOST_BIT)) {
                    //treat these as individual characters
                    retString.append((char) readByte);
                    retString.append((char) secondByte);
                    retString.append((char) thirdByte);
                    continue;
                }
                int finalVal = (thirdByte & MASK_BOTTOM_FIVE_BITS) + 64 * (secondByte & MASK_BOTTOM_FIVE_BITS)
                        + 4096 * (readByte & MASK_BOTTOM_FOUR_BITS);
                thisChar = (char) finalVal;
            } else if ((readByte & MASK_TWO_BYTE_CHAR) == MASK_TWO_BYTE_CHAR) {
                found_cr = false;

                // need to read next byte
                if (in.available() < 1) {
                    // treat this as individual characters
                    retString.append((char) readByte);
                    continue;
                }
                byte secondByte = in.readByte();
                if ((secondByte & MASK_TOPMOST_BIT) != MASK_TOPMOST_BIT) {
                    retString.append((char) readByte);
                    retString.append((char) secondByte);
                    continue;
                }
                int finalVal = (secondByte & MASK_BOTTOM_FIVE_BITS) + 64 * (readByte & MASK_BOTTOM_SIX_BITS);
                thisChar = (char) finalVal;
            } else {
                // interpret it as a single byte
                thisChar = (char) readByte;
            }
            // Look for carriage return; if found set a flag
            if (thisChar == '\r') {
                found_cr = true;
            }
            if (thisChar == '\n') {
                // if the linefeed is the next character after the carriage return
                if (found_cr) {
                    LINE_ENDING = CR_NEWLINE;
                } else {
                    LINE_ENDING = NEWLINE;
                }
                keepReading = false;
            } else {
                retString.append(thisChar);
            }
        } while (keepReading);
    } catch (EOFException eofEx) {
        return null;
    }

    if (retString.length() == 0) {
        return "";
    }

    return retString.toString();
}

From source file:cwru.mjq.WarcRecord.java

private static String readLineFromInputStream(DataInputStream in) throws IOException {
    StringBuilder retString = new StringBuilder();
    boolean found_cr = false;
    boolean keepReading = true;
    try {//from www .j  av  a  2 s.com
        do {
            char thisChar;
            byte readByte = in.readByte();
            // check to see if it's a multibyte character
            if ((readByte & MASK_THREE_BYTE_CHAR) == MASK_THREE_BYTE_CHAR) {
                found_cr = false;
                // need to read the next 2 bytes
                if (in.available() < 2) {
                    // treat these all as individual characters
                    retString.append((char) readByte);
                    int numAvailable = in.available();
                    for (int i = 0; i < numAvailable; i++) {
                        retString.append((char) (in.readByte()));
                    }
                    continue;
                }
                byte secondByte = in.readByte();
                byte thirdByte = in.readByte();
                // ensure the topmost bit is set
                if (((secondByte & MASK_TOPMOST_BIT) != MASK_TOPMOST_BIT)
                        || ((thirdByte & MASK_TOPMOST_BIT) != MASK_TOPMOST_BIT)) {
                    // treat these as individual characters
                    retString.append((char) readByte);
                    retString.append((char) secondByte);
                    retString.append((char) thirdByte);
                    continue;
                }
                int finalVal = (thirdByte & MASK_BOTTOM_FIVE_BITS) + 64 * (secondByte & MASK_BOTTOM_FIVE_BITS)
                        + 4096 * (readByte & MASK_BOTTOM_FOUR_BITS);
                thisChar = (char) finalVal;
            } else if ((readByte & MASK_TWO_BYTE_CHAR) == MASK_TWO_BYTE_CHAR) {
                found_cr = false;

                // need to read next byte
                if (in.available() < 1) {
                    // treat this as individual characters
                    retString.append((char) readByte);
                    continue;
                }
                byte secondByte = in.readByte();
                if ((secondByte & MASK_TOPMOST_BIT) != MASK_TOPMOST_BIT) {
                    retString.append((char) readByte);
                    retString.append((char) secondByte);
                    continue;
                }
                int finalVal = (secondByte & MASK_BOTTOM_FIVE_BITS) + 64 * (readByte & MASK_BOTTOM_SIX_BITS);
                thisChar = (char) finalVal;
            } else {
                // interpret it as a single byte
                thisChar = (char) readByte;
            }
            // Look for carriage return; if found set a flag
            if (thisChar == '\r') {
                found_cr = true;
            }
            if (thisChar == '\n') {
                // if the linefeed is the next character after the carriage
                // return
                if (found_cr) {
                    LINE_ENDING = CR_NEWLINE;
                } else {
                    LINE_ENDING = NEWLINE;
                }
                keepReading = false;
            } else {
                retString.append(thisChar);
            }
        } while (keepReading);
    } catch (EOFException eofEx) {
        return null;
    }

    if (retString.length() == 0) {
        return "";
    }

    return retString.toString();
}

From source file:org.kei.android.phone.cellhistory.towers.request.OpenCellIdRequestEntity.java

@Override
public int decode(final String url, final HttpConnection connection, final int timeout) throws Exception {
    int ret = OK;
    final GetMethod getMethod = new GetMethod(url);
    getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER,
            new DefaultHttpMethodRetryHandler(1, false));
    // socket timeout (connection timeout already set in HttpClient)
    getMethod.getParams().setSoTimeout(timeout);
    final int resCode = getMethod.execute(new HttpState(), connection);
    final InputStream response = getMethod.getResponseBodyAsStream();
    final DataInputStream dis = new DataInputStream(response);
    if (resCode == HttpStatus.SC_OK) {
        final int av = dis.available();
        final byte[] json = new byte[av];
        dis.readFully(json);//from  ww w . j av a2  s.  co  m
        final String sjson = new String(json);
        final String ljson = sjson.toLowerCase(Locale.US);
        if (ljson.indexOf("err") == -1) {
            final JSONObject object = new JSONObject(sjson);
            String lat, lng;
            lat = object.getString("lat");
            lng = object.getString("lon");
            ti.setCellLatitude(Double.parseDouble(lat));
            ti.setCellLongitude(Double.parseDouble(lng));
        } else if (ljson.indexOf("not found") != -1)
            ret = NOT_FOUND;
        else
            ret = EXCEPTION;
    } else if (resCode == HttpStatus.SC_NOT_FOUND)
        ret = NOT_FOUND;
    else if (resCode == HttpStatus.SC_INTERNAL_SERVER_ERROR)
        ret = BAD_REQUEST;
    else
        ret = EXCEPTION;
    getMethod.releaseConnection();
    return ret;
}

From source file:com.vimukti.accounter.license.LicensePair.java

public LicensePair(String contactLicenseText) throws LicenseException {
    this.originalLicenseString = contactLicenseText;
    if (!new LicenseManager().canDecode(contactLicenseText)) {
        return;//from  w  w  w  .j a v a2s.c om
    }

    try {
        byte[] decodedBytes = Base64.decodeBase64(contactLicenseText.getBytes());

        ByteArrayInputStream in = new ByteArrayInputStream(decodedBytes);
        DataInputStream dIn = new DataInputStream(in);

        int textLength = dIn.readInt();
        this.licenseText = new byte[textLength];
        dIn.read(licenseText);

        this.hash = new byte[dIn.available()];
        dIn.read(hash);
    } catch (IOException e) {
        throw new LicenseException(e);
    }

}

From source file:org.wyona.yanel.impl.resources.updatefinder.utils.TomcatContextHandler.java

/**
* @param ArrayList contexts/* w w w . j av  a  2  s .co  m*/
* @return ArrayList with all contexts which pionts to the webapp or null if webapp not exists or no conext points to this webapp (will never return ROOT as context)
*/
public List<String> getContextsOfWebapp(String webapp) throws FileNotFoundException, IOException {
    List<String> contexts = new ArrayList<String>();
    for (int i = 0; i < this.contextConfDirectory.listFiles().length; i++) {
        String line = "";
        FileInputStream fis = new FileInputStream(contextConfDirectory.listFiles()[i]);
        BufferedInputStream bis = new BufferedInputStream(fis);
        DataInputStream dis = new DataInputStream(bis);
        while (dis.available() != 0) {
            line = line + dis.readLine();
        }
        fis.close();
        bis.close();
        dis.close();
        if (line.indexOf(webapp) > 0) {
            contexts.add(contextConfDirectory.listFiles()[i].getName().replaceAll(".xml", ""));
        }
    }
    if (contexts.size() < 1) {
        return null;
    }
    return contexts;
}

From source file:net.mms_projects.copy_it.server.push.android.GCMRunnable.java

public void run() {
    try {/*  w w w . ja  v a2 s . co m*/
        URL url = new URL(GCM_URL);
        HttpsURLConnection conn = (HttpsURLConnection) url.openConnection();
        conn.setRequestMethod(POST);
        conn.setRequestProperty(CONTENT_TYPE, Page.ContentTypes.JSON_TYPE);
        conn.setRequestProperty(AUTHORIZATION, KEY);
        final String output_json = full.toString();
        System.err.println("Input json: " + output_json);
        conn.setRequestProperty(CONTENT_LENGTH, String.valueOf(output_json.length()));
        conn.setDoOutput(true);
        conn.setDoInput(true);
        DataOutputStream outputstream = new DataOutputStream(conn.getOutputStream());
        outputstream.writeBytes(output_json);
        outputstream.close();
        DataInputStream input = new DataInputStream(conn.getInputStream());
        StringBuilder builder = new StringBuilder(input.available());
        for (int c = input.read(); c != -1; c = input.read())
            builder.append((char) c);
        input.close();
        output = new JSONObject(builder.toString());
        System.err.println("Output json: " + output.toString());
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:org.wyona.yanel.impl.resources.updatefinder.utils.TomcatContextHandler.java

/**
 * @param String context//from ww w.j  a  va2s. c o  m
 * @return String webapp or null if webapp not exists or null if context points to webapp which does not exist
 */
public String getWebappOfContext(String context) throws FileNotFoundException, IOException {
    File file = new File(contextConfPath + context + ".xml");
    if (!file.exists()) {
        return null;
    }
    String line = "";
    String webapp = "";

    FileInputStream fis = new FileInputStream(file);
    BufferedInputStream bis = new BufferedInputStream(fis);
    DataInputStream dis = new DataInputStream(bis);
    while (dis.available() != 0) {
        line = line + dis.readLine();
    }
    fis.close();
    bis.close();
    dis.close();
    if (line.indexOf("yanel-webapps") <= 0) {
        return "";
    }
    line = line.replaceAll("[ ]+", " ");
    line = line.replaceAll("\"/>", "");
    webapp = line.split("/")[line.split("/").length - 1];

    if (!new File(webappsDirectoryPath + webapp).exists()) {
        return null;
    }
    return webapp;
}