Example usage for java.io BufferedReader reset

List of usage examples for java.io BufferedReader reset

Introduction

In this page you can find the example usage for java.io BufferedReader reset.

Prototype

public void reset() throws IOException 

Source Link

Document

Resets the stream to the most recent mark.

Usage

From source file:com.googlecode.erca.framework.io.in.RcftParser.java

private void parseFormalContext(BufferedReader input, String desc) throws IOException {

    String fcName = desc.split("\\ ")[1];

    FormalContext fc = RcfFactory.eINSTANCE.createFormalContext();
    fc.setName(fcName);/* ww w  . ja va2s  .  co  m*/

    logger.info("Parsing formal context " + fcName);

    input.mark(0);
    String line = input.readLine();
    int currentRow = 0;
    Map<Integer, Attribute> attrs = new HashMap<Integer, Attribute>();

    while (line != null) {

        String tline = line.trim();
        if (tline.startsWith("FormalContext"))
            break;
        else if (tline.startsWith("RelationalContext"))
            break;
        else if (tline.equals(""))
            break;

        String[] tokens = line.split("\\|");
        int len = tokens.length;
        if (currentRow == 0) {

            for (int i = 2; i < len; i++) {
                String attrDesc = tokens[i].trim();
                Attribute attr = RcaParsingUtils.attributeFromText(attrDesc);
                attrs.put(i, attr);
                fc.getAttributes().add(attr);

                logger.debug("Attribute " + attr.getDescription() + " created. Row: " + i);
            }
        } else {
            String name = tokens[1].trim();
            Entity ent = ErcaFactory.eINSTANCE.createEntity();

            logger.debug("Entity " + name + " created. Line: " + currentRow);

            ent.setName(name);
            fc.getEntities().add(ent);
            for (int i = 2; i < len; i++) {
                String cell = tokens[i].trim().toLowerCase();
                if ("x".equals(cell)) {
                    Attribute attr = attrs.get(i);
                    Pair pair = RcfFactory.eINSTANCE.createPair();
                    pair.setSource(ent);
                    pair.setTarget(attr);
                    fc.getRelation().add(pair);

                    logger.debug("Pair between " + ent.getName() + " and " + attr.getDescription()
                            + " created. Line: " + currentRow);
                }
            }
        }

        currentRow++;

        input.mark(0);

        line = input.readLine();
    }

    rcf.getFormalContexts().add(fc);

    if (line != null)
        input.reset();

}

From source file:com.googlecode.erca.framework.io.in.RcftParser.java

private void parseRelationalContext(BufferedReader input, String desc) throws IOException {

    RelationalContext rc = RcfFactory.eINSTANCE.createRelationalContext();
    String rcName = desc.split("\\ ")[1];

    logger.info("Parsing relational context " + rcName);

    rc.setName(rcName);// www .j av  a2 s .c  o m

    String source = input.readLine().trim().split("\\ ")[1];
    String target = input.readLine().trim().split("\\ ")[1];
    String sclOp = input.readLine().trim().split("\\ ")[1];
    FormalContext sourceFc = rcf.getFormalContext(source);
    FormalContext targetFc = rcf.getFormalContext(target);
    rc.setSourceContext(sourceFc);
    rc.setTargetContext(targetFc);
    rc.setScalingOperator(sclOp);

    input.mark(0);

    String line = input.readLine();
    int currentRow = 0;
    Map<Integer, Entity> tgtEnts = new HashMap<Integer, Entity>();

    while (line != null) {

        String tline = line.trim();
        if (tline.startsWith("RelationalContext"))
            break;
        else if (tline.startsWith("FormalContext"))
            break;
        else if (tline.equals(""))
            break;

        String[] tokens = line.split("\\|");
        int len = tokens.length;
        if (currentRow == 0) {

            for (int i = 2; i < len; i++) {
                String name = tokens[i].trim();
                Entity ent = targetFc.getEntity(name);
                tgtEnts.put(i, ent);
            }

        } else {
            String name = tokens[1].trim();
            Entity srcEnt = sourceFc.getEntity(name);

            for (int i = 2; i < len; i++) {
                String cell = tokens[i].trim().toLowerCase();
                if ("x".equals(cell)) {
                    Entity tgtEnt = tgtEnts.get(i);
                    Pair pair = RcfFactory.eINSTANCE.createPair();
                    pair.setSource(srcEnt);
                    pair.setTarget(tgtEnt);
                    rc.getRelation().add(pair);

                    logger.debug("Creates pair between " + srcEnt.getName() + " and " + tgtEnt.getName()
                            + ". Line: " + currentRow);
                }
            }
        }

        currentRow++;

        input.mark(0);

        line = input.readLine();
    }

    rcf.getRelationalContexts().add(rc);

    if (line != null)
        input.reset();

}

From source file:org.intermine.web.struts.BuildBagAction.java

/**
 * Action for creating a bag of InterMineObjects or Strings from identifiers in text field.
 *
 * @param mapping The ActionMapping used to select this instance
 * @param form The optional ActionForm bean for this request (if any)
 * @param request The HTTP request we are processing
 * @param response The HTTP response we are creating
 * @return an ActionForward object defining where control goes next
 * @exception Exception if the application business logic throws
 *  an exception/*w  w w.j  a va2  s . com*/
 */
@Override
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    HttpSession session = request.getSession();
    final InterMineAPI im = SessionMethods.getInterMineAPI(session);
    ServletContext servletContext = request.getSession().getServletContext();
    Properties webProperties = (Properties) servletContext.getAttribute(Constants.WEB_PROPERTIES);
    BuildBagForm buildBagForm = (BuildBagForm) form;

    String type = buildBagForm.getType();

    if (StringUtils.isEmpty(type)) {
        recordError(new ActionMessage("bagBuild.typeNotSet"), request);
        return mapping.findForward("bags");
    }

    BagQueryRunner bagRunner = im.getBagQueryRunner();

    int maxBagSize = WebUtil.getIntSessionProperty(session, "max.bag.size", 100000);
    Profile profile = SessionMethods.getProfile(session);
    if (profile == null || profile.getUsername() == null) {
        int defaultMaxNotLoggedSize = 3;
        maxBagSize = WebUtil.getIntSessionProperty(session, "max.bag.size.notloggedin",
                defaultMaxNotLoggedSize);
    }
    BufferedReader reader = null;
    FormFile formFile = buildBagForm.getFormFile();

    /*
     * FormFile used from Struts works a bit strangely.
     * 1. Although the file does't exist formFile.getInputStream() doesn't
     * throw FileNotFoundException.
     * 2. When user specified empty file path or very invalid file path,
     * like file path not starting at '/' then formFile.getFileName() returns empty string.
     */
    if (formFile != null && formFile.getFileName() != null && formFile.getFileName().length() > 0) {
        // attach file name as the name of the bag
        String fileName = formFile.getFileName();
        // strip suffix
        Integer lastPos = new Integer(fileName.lastIndexOf('.'));
        if (lastPos.intValue() > 0) {
            fileName = fileName.substring(0, lastPos.intValue());
        }
        // replace underscores
        fileName = fileName.replaceAll("_", " ");
        // attach
        request.setAttribute("bagName", fileName);

        String mimetype = formFile.getContentType();
        if (!"application/octet-stream".equals(mimetype) && !mimetype.startsWith("text")) {
            recordError(new ActionMessage("bagBuild.notText", mimetype), request);
            return mapping.findForward("bags");
        }
        if (formFile.getFileSize() == 0) {
            recordError(new ActionMessage("bagBuild.noBagFileOrEmpty"), request);
            return mapping.findForward("bags");
        }
        reader = new BufferedReader(new InputStreamReader(formFile.getInputStream()));
    } else if (buildBagForm.getText() != null && buildBagForm.getText().length() != 0) {
        String trimmedText = buildBagForm.getText().trim();
        if (trimmedText.length() == 0) {
            recordError(new ActionMessage("bagBuild.noBagPaste"), request);
            return mapping.findForward("bags");
        }
        reader = new BufferedReader(new StringReader(trimmedText));
    } else {
        recordError(new ActionMessage("bagBuild.noBagFile"), request);
        return mapping.findForward("bags");
    }

    reader.mark(READ_AHEAD_CHARS);

    char[] buf = new char[READ_AHEAD_CHARS];

    int read = reader.read(buf, 0, READ_AHEAD_CHARS);

    for (int i = 0; i < read; i++) {
        if (buf[i] == 0) {
            recordError(new ActionMessage("bagBuild.notText", "binary"), request);
            return mapping.findForward("bags");
        }
    }

    reader.reset();

    String thisLine;
    List<String> list = new ArrayList<String>();
    int elementCount = 0;
    while ((thisLine = reader.readLine()) != null) {
        // append whitespace to valid delimiters
        String bagUploadDelims = (String) webProperties.get("list.upload.delimiters") + " ";
        StrMatcher matcher = StrMatcher.charSetMatcher(bagUploadDelims);
        StrTokenizer st = new StrTokenizer(thisLine, matcher, StrMatcher.doubleQuoteMatcher());
        while (st.hasNext()) {
            String token = st.nextToken();
            list.add(token);
            elementCount++;
            if (elementCount > maxBagSize) {
                ActionMessage actionMessage = null;
                if (profile == null || profile.getUsername() == null) {
                    actionMessage = new ActionMessage("bag.bigNotLoggedIn", new Integer(maxBagSize));
                } else {
                    actionMessage = new ActionMessage("bag.tooBig", new Integer(maxBagSize));
                }
                recordError(actionMessage, request);
                return mapping.findForward("bags");
            }
        }
    }
    BagQueryResult bagQueryResult = bagRunner.search(type, list, buildBagForm.getExtraFieldValue(), false,
            buildBagForm.getCaseSensitive());
    session.setAttribute("bagQueryResult", bagQueryResult);
    request.setAttribute("bagType", type);
    request.setAttribute("bagExtraFilter", buildBagForm.getExtraFieldValue());
    //buildNewBag used by jsp to set editable the bag name field
    request.setAttribute("buildNewBag", "true");
    return mapping.findForward("bagUploadConfirm");
}

From source file:ffx.xray.CIFFilter.java

/**
 * {@inheritDoc}/* w w w . j av  a 2s .co  m*/
 */
@Override
public boolean readFile(File cifFile, ReflectionList reflectionlist, DiffractionRefinementData refinementdata,
        CompositeConfiguration properties) {
    int nread, nnan, nres, nignore, ncifignore, nfriedel, ncut;
    boolean transpose = false;
    boolean intensitiesToAmplitudes = false;

    StringBuilder sb = new StringBuilder();
    sb.append(String.format(" Opening %s\n", cifFile.getName()));
    if (refinementdata.rfreeflag < 0) {
        refinementdata.setFreeRFlag(1);
        sb.append(String.format(" Setting R free flag to CIF default: %d\n", refinementdata.rfreeflag));
    }

    try {
        BufferedReader br = new BufferedReader(new FileReader(cifFile));

        String str;
        int ncol = 0;
        boolean inhkl = false;
        while ((str = br.readLine()) != null) {
            String strarray[] = str.split("\\s+");

            if (strarray[0].startsWith("_refln.")) {
                inhkl = true;
                br.mark(0);
                String cifarray[] = strarray[0].split("\\.+");
                switch (Header.toHeader(cifarray[1])) {
                case index_h:
                    h = ncol;
                    break;
                case index_k:
                    k = ncol;
                    break;
                case index_l:
                    l = ncol;
                    break;
                case F_meas:
                case F_meas_au:
                    fo = ncol;
                    break;
                case F_meas_sigma:
                case F_meas_sigma_au:
                    sigfo = ncol;
                    break;
                case intensity_meas:
                    io = ncol;
                    break;
                case intensity_sigma:
                    sigio = ncol;
                    break;
                case status:
                    rfree = ncol;
                    break;
                }

                ncol++;
            } else if (inhkl) {
                if (h < 0 || k < 0 || l < 0) {
                    String message = "Fatal error in CIF file - no H K L indexes?\n";
                    logger.log(Level.SEVERE, message);
                    return false;
                }
                break;
            }
        }

        if (fo < 0 && sigfo < 0 && io > 0 && sigio > 0) {
            intensitiesToAmplitudes = true;
        }

        if (fo < 0 && io < 0) {
            logger.severe("Reflection data (I/F) not found in CIF file!");
        }

        // go back to where the reflections start
        br.reset();

        // check if HKLs need to be transposed or not
        HKL mate = new HKL();
        int nposignore = 0;
        int ntransignore = 0;
        while ((str = br.readLine()) != null) {
            // reached end, break
            if (str.trim().startsWith("#END")) {
                break;
            } else if (str.trim().startsWith("data")) {
                break;
            } else if (str.trim().startsWith("#")) {
                continue;
            }

            String strarray[] = str.trim().split("\\s+");
            // some files split data on to multiple lines
            while (strarray.length < ncol) {
                str = str + " " + br.readLine();
                strarray = str.trim().split("\\s+");
            }

            if (rfree > 0) {
                // ignored cases
                if (strarray[rfree].charAt(0) == 'x' || strarray[rfree].charAt(0) == '<'
                        || strarray[rfree].charAt(0) == '-' || strarray[rfree].charAt(0) == 'h'
                        || strarray[rfree].charAt(0) == 'l') {
                    continue;
                }
            }
            int ih = Integer.parseInt(strarray[h]);
            int ik = Integer.parseInt(strarray[k]);
            int il = Integer.parseInt(strarray[l]);
            boolean friedel = reflectionlist.findSymHKL(ih, ik, il, mate, false);
            HKL hklpos = reflectionlist.getHKL(mate);
            if (hklpos == null) {
                nposignore++;
            }

            friedel = reflectionlist.findSymHKL(ih, ik, il, mate, true);
            HKL hkltrans = reflectionlist.getHKL(mate);
            if (hkltrans == null) {
                ntransignore++;
            }
        }
        if (nposignore > ntransignore) {
            transpose = true;
        }

        // reopen to start at beginning
        br = new BufferedReader(new FileReader(cifFile));
        inhkl = false;
        while ((str = br.readLine()) != null) {
            String strarray[] = str.split("\\s+");

            if (strarray[0].startsWith("_refln.")) {
                br.mark(0);
                inhkl = true;
            } else if (inhkl) {
                break;
            }
        }

        // go back to where the reflections start
        br.reset();

        // read in data
        double anofsigf[][] = new double[refinementdata.n][4];
        for (int i = 0; i < refinementdata.n; i++) {
            anofsigf[i][0] = anofsigf[i][1] = anofsigf[i][2] = anofsigf[i][3] = Double.NaN;
        }
        nread = nnan = nres = nignore = ncifignore = nfriedel = ncut = 0;
        while ((str = br.readLine()) != null) {
            // reached end, break
            if (str.trim().startsWith("#END")) {
                break;
            } else if (str.trim().startsWith("data")) {
                break;
            } else if (str.trim().startsWith("#")) {
                continue;
            }

            String strarray[] = str.trim().split("\\s+");
            // some files split data on to multiple lines
            while (strarray.length < ncol) {
                str = str + " " + br.readLine();
                strarray = str.trim().split("\\s+");
            }

            int ih = Integer.parseInt(strarray[h]);
            int ik = Integer.parseInt(strarray[k]);
            int il = Integer.parseInt(strarray[l]);
            boolean friedel = reflectionlist.findSymHKL(ih, ik, il, mate, transpose);
            HKL hkl = reflectionlist.getHKL(mate);

            if (hkl != null) {
                boolean isnull = false;

                if (rfree > 0) {
                    if (strarray[rfree].charAt(0) == 'o') {
                        refinementdata.setFreeR(hkl.index(), 0);
                    } else if (strarray[rfree].charAt(0) == 'f') {
                        refinementdata.setFreeR(hkl.index(), 1);
                    } else if (strarray[rfree].charAt(0) == 'x') {
                        isnull = true;
                        nnan++;
                    } else if (strarray[rfree].charAt(0) == '<' || strarray[rfree].charAt(0) == '-'
                            || strarray[rfree].charAt(0) == 'h' || strarray[rfree].charAt(0) == 'l') {
                        isnull = true;
                        ncifignore++;
                    } else {
                        refinementdata.setFreeR(hkl.index(), Integer.parseInt(strarray[rfree]));
                    }
                }

                if (!intensitiesToAmplitudes && !isnull) {
                    if (strarray[fo].charAt(0) == '?' || strarray[sigfo].charAt(0) == '?') {
                        isnull = true;
                        nnan++;
                        continue;
                    }

                    if (refinementdata.fsigfcutoff > 0.0) {
                        double f1 = Double.parseDouble(strarray[fo]);
                        double sigf1 = Double.parseDouble(strarray[sigfo]);
                        if ((f1 / sigf1) < refinementdata.fsigfcutoff) {
                            ncut++;
                            continue;
                        }
                    }

                    if (friedel) {
                        anofsigf[hkl.index()][2] = Double.parseDouble(strarray[fo]);
                        anofsigf[hkl.index()][3] = Double.parseDouble(strarray[sigfo]);
                        nfriedel++;
                    } else {
                        anofsigf[hkl.index()][0] = Double.parseDouble(strarray[fo]);
                        anofsigf[hkl.index()][1] = Double.parseDouble(strarray[sigfo]);
                    }
                }

                if (intensitiesToAmplitudes && !isnull) {
                    if (strarray[io].charAt(0) == '?' || strarray[sigio].charAt(0) == '?') {
                        isnull = true;
                        nnan++;
                        continue;
                    }

                    if (friedel) {
                        anofsigf[hkl.index()][2] = Double.parseDouble(strarray[io]);
                        anofsigf[hkl.index()][3] = Double.parseDouble(strarray[sigio]);
                        nfriedel++;
                    } else {
                        anofsigf[hkl.index()][0] = Double.parseDouble(strarray[io]);
                        anofsigf[hkl.index()][1] = Double.parseDouble(strarray[sigio]);
                    }
                }

                nread++;
            } else {
                HKL tmp = new HKL(ih, ik, il);
                if (!reflectionlist.resolution
                        .inInverseResSqRange(Crystal.invressq(reflectionlist.crystal, tmp))) {
                    nres++;
                } else {
                    nignore++;
                }
            }
        }

        br.close();

        // set up fsigf from F+ and F-
        refinementdata.generate_fsigf_from_anofsigf(anofsigf);

        if (intensitiesToAmplitudes) {
            refinementdata.intensities_to_amplitudes();
        }
    } catch (IOException ioe) {
        System.out.println("IO Exception: " + ioe.getMessage());
        return false;
    }

    sb.append(String.format(" HKL data is %s\n", transpose ? "transposed" : "not transposed"));
    sb.append(String.format(" HKL read in:                             %d\n", nread));
    sb.append(String.format(" HKL read as friedel mates:               %d\n", nfriedel));
    sb.append(String.format(" HKL with NaN (ignored):                  %d\n", nnan));
    sb.append(String.format(" HKL NOT read in (status <, -, h or l):   %d\n", ncifignore));
    sb.append(String.format(" HKL NOT read in (too high resolution):   %d\n", nres));
    sb.append(String.format(" HKL NOT read in (not in internal list?): %d\n", nignore));
    sb.append(String.format(" HKL NOT read in (F/sigF cutoff):         %d\n", ncut));
    sb.append(String.format(" HKL in internal list:                    %d\n", reflectionlist.hkllist.size()));
    if (logger.isLoggable(Level.INFO)) {
        logger.info(sb.toString());
    }

    if (rfree < 0) {
        refinementdata.generateRFree();
    }

    return true;
}

From source file:org.geoserver.ows.Dispatcher.java

Object parseRequestXML(Object requestBean, BufferedReader input, Request request) throws Exception {
    //check for an empty input stream
    //if (input.available() == 0) {
    if (!input.ready()) {
        return null;
    }/*www. j a  v  a 2 s . c om*/

    //create stream parser
    XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
    factory.setNamespaceAware(true);
    factory.setValidating(false);

    //parse root element
    XmlPullParser parser = factory.newPullParser();
    //parser.setInput(input, "UTF-8");
    parser.setInput(input);
    parser.nextTag();

    String namespace = (parser.getNamespace() != null) ? parser.getNamespace() : "";
    String element = parser.getName();
    String version = null;
    String service = null;

    for (int i = 0; i < parser.getAttributeCount(); i++) {
        if ("version".equals(parser.getAttributeName(i))) {
            version = parser.getAttributeValue(i);
        }
        if ("service".equals(parser.getAttributeName(i))) {
            service = parser.getAttributeValue(i);
        }
    }

    parser.setInput(null);

    //reset input stream
    input.reset();

    XmlRequestReader xmlReader = findXmlReader(namespace, element, service, version);
    if (xmlReader == null) {
        //no xml reader, just return object passed in
        return requestBean;
    }

    if (xmlReader instanceof HttpServletRequestAware) {
        ((HttpServletRequestAware) xmlReader).setHttpRequest(request.getHttpRequest());
    }

    //return xmlReader.read(input);
    return xmlReader.read(requestBean, input, request.getKvp());
}

From source file:ffx.xray.parsers.CIFFilter.java

/**
 * {@inheritDoc}/*from  w ww . ja va2s  .co  m*/
 */
@Override
public boolean readFile(File cifFile, ReflectionList reflectionList, DiffractionRefinementData refinementData,
        CompositeConfiguration properties) {

    int nRead, nNAN, nRes;
    int nIgnore, nCIFIgnore;
    int nFriedel, nCut;
    boolean transpose = false;
    boolean intensitiesToAmplitudes = false;

    StringBuilder sb = new StringBuilder();
    sb.append(String.format(" Opening %s\n", cifFile.getName()));
    if (refinementData.rfreeflag < 0) {
        refinementData.setFreeRFlag(1);
        sb.append(format(" Setting R free flag to CIF default: %d\n", refinementData.rfreeflag));
    }

    try {
        BufferedReader br = new BufferedReader(new FileReader(cifFile));

        String string;
        int nCol = 0;
        boolean inHKL = false;
        while ((string = br.readLine()) != null) {
            String stringArray[] = string.split("\\s+");
            if (stringArray[0].startsWith("_refln.")) {
                inHKL = true;
                br.mark(0);
                String cifArray[] = stringArray[0].split("\\.+");
                switch (Header.toHeader(cifArray[1])) {
                case index_h:
                    h = nCol;
                    break;
                case index_k:
                    k = nCol;
                    break;
                case index_l:
                    l = nCol;
                    break;
                case F_meas:
                case F_meas_au:
                    fo = nCol;
                    break;
                case F_meas_sigma:
                case F_meas_sigma_au:
                    sigFo = nCol;
                    break;
                case intensity_meas:
                    io = nCol;
                    break;
                case intensity_sigma:
                    sigIo = nCol;
                    break;
                case status:
                    rFree = nCol;
                    break;
                }
                nCol++;
            } else if (inHKL) {
                if (h < 0 || k < 0 || l < 0) {
                    String message = " Fatal error in CIF file - no H K L indexes?\n";
                    logger.log(Level.SEVERE, message);
                    return false;
                }
                break;
            }
        }

        if (fo < 0 && sigFo < 0 && io > 0 && sigIo > 0) {
            intensitiesToAmplitudes = true;
        }

        if (fo < 0 && io < 0) {
            logger.severe(" Reflection data (I/F) not found in CIF file!");
        }

        // Go back to where the reflections start.
        br.reset();

        // Check if HKLs need to be transposed or not.
        HKL mate = new HKL();
        int nPosIgnore = 0;
        int nTransIgnore = 0;
        while ((string = br.readLine()) != null) {

            if (string.trim().startsWith("#END")) {
                // Reached end, break.
                break;
            } else if (string.trim().startsWith("data")) {
                break;
            } else if (string.trim().startsWith("#")) {
                continue;
            }

            // Some files split data on to multiple lines.
            String strArray[] = string.trim().split("\\s+");
            while (strArray.length < nCol) {
                string = string + " " + br.readLine();
                strArray = string.trim().split("\\s+");
            }

            if (rFree > 0) {
                // Ignored cases.
                if (strArray[rFree].charAt(0) == 'x' || strArray[rFree].charAt(0) == '<'
                        || strArray[rFree].charAt(0) == '-' || strArray[rFree].charAt(0) == 'h'
                        || strArray[rFree].charAt(0) == 'l') {
                    continue;
                }
            }

            int ih = Integer.parseInt(strArray[h]);
            int ik = Integer.parseInt(strArray[k]);
            int il = Integer.parseInt(strArray[l]);

            boolean friedel = reflectionList.findSymHKL(ih, ik, il, mate, false);
            HKL hklPos = reflectionList.getHKL(mate);
            if (hklPos == null) {
                nPosIgnore++;
            }

            friedel = reflectionList.findSymHKL(ih, ik, il, mate, true);
            HKL hklTrans = reflectionList.getHKL(mate);
            if (hklTrans == null) {
                nTransIgnore++;
            }
        }
        if (nPosIgnore > nTransIgnore) {
            transpose = true;
        }

        // Re-open to start at beginning.
        br = new BufferedReader(new FileReader(cifFile));
        inHKL = false;
        while ((string = br.readLine()) != null) {
            String strArray[] = string.split("\\s+");
            if (strArray[0].startsWith("_refln.")) {
                br.mark(0);
                inHKL = true;
            } else if (inHKL) {
                break;
            }
        }

        // Go back to where the reflections start.
        br.reset();

        // Read in data.
        double anofSigF[][] = new double[refinementData.n][4];
        for (int i = 0; i < refinementData.n; i++) {
            anofSigF[i][0] = anofSigF[i][1] = anofSigF[i][2] = anofSigF[i][3] = Double.NaN;
        }
        nRead = nNAN = nRes = nIgnore = nCIFIgnore = nFriedel = nCut = 0;
        while ((string = br.readLine()) != null) {

            // Reached end, break.
            if (string.trim().startsWith("#END")) {
                break;
            } else if (string.trim().startsWith("data")) {
                break;
            } else if (string.trim().startsWith("#")) {
                continue;
            }

            // Some files split data on to multiple lines.
            String strArray[] = string.trim().split("\\s+");
            while (strArray.length < nCol) {
                string = string + " " + br.readLine();
                strArray = string.trim().split("\\s+");
            }

            int ih = Integer.parseInt(strArray[h]);
            int ik = Integer.parseInt(strArray[k]);
            int il = Integer.parseInt(strArray[l]);

            boolean friedel = reflectionList.findSymHKL(ih, ik, il, mate, transpose);
            HKL hkl = reflectionList.getHKL(mate);
            if (hkl != null) {
                boolean isnull = false;
                if (rFree > 0) {
                    if (strArray[rFree].charAt(0) == 'o') {
                        refinementData.setFreeR(hkl.index(), 0);
                    } else if (strArray[rFree].charAt(0) == 'f') {
                        refinementData.setFreeR(hkl.index(), 1);
                    } else if (strArray[rFree].charAt(0) == 'x') {
                        isnull = true;
                        nNAN++;
                    } else if (strArray[rFree].charAt(0) == '<' || strArray[rFree].charAt(0) == '-'
                            || strArray[rFree].charAt(0) == 'h' || strArray[rFree].charAt(0) == 'l') {
                        isnull = true;
                        nCIFIgnore++;
                    } else {
                        refinementData.setFreeR(hkl.index(), Integer.parseInt(strArray[rFree]));
                    }
                }

                if (!intensitiesToAmplitudes && !isnull) {
                    if (strArray[fo].charAt(0) == '?' || strArray[sigFo].charAt(0) == '?') {
                        isnull = true;
                        nNAN++;
                        continue;
                    }

                    if (refinementData.fsigfcutoff > 0.0) {
                        double f1 = Double.parseDouble(strArray[fo]);
                        double sigf1 = Double.parseDouble(strArray[sigFo]);
                        if ((f1 / sigf1) < refinementData.fsigfcutoff) {
                            nCut++;
                            continue;
                        }
                    }

                    if (friedel) {
                        anofSigF[hkl.index()][2] = Double.parseDouble(strArray[fo]);
                        anofSigF[hkl.index()][3] = Double.parseDouble(strArray[sigFo]);
                        nFriedel++;
                    } else {
                        anofSigF[hkl.index()][0] = Double.parseDouble(strArray[fo]);
                        anofSigF[hkl.index()][1] = Double.parseDouble(strArray[sigFo]);
                    }
                }

                if (intensitiesToAmplitudes && !isnull) {
                    if (strArray[io].charAt(0) == '?' || strArray[sigIo].charAt(0) == '?') {
                        isnull = true;
                        nNAN++;
                        continue;
                    }

                    if (friedel) {
                        anofSigF[hkl.index()][2] = Double.parseDouble(strArray[io]);
                        anofSigF[hkl.index()][3] = Double.parseDouble(strArray[sigIo]);
                        nFriedel++;
                    } else {
                        anofSigF[hkl.index()][0] = Double.parseDouble(strArray[io]);
                        anofSigF[hkl.index()][1] = Double.parseDouble(strArray[sigIo]);
                    }
                }

                nRead++;
            } else {
                HKL tmp = new HKL(ih, ik, il);
                if (!reflectionList.resolution
                        .inInverseResSqRange(Crystal.invressq(reflectionList.crystal, tmp))) {
                    nRes++;
                } else {
                    nIgnore++;
                }
            }
        }
        br.close();

        // Set up fsigf from F+ and F-.
        refinementData.generate_fsigf_from_anofsigf(anofSigF);
        if (intensitiesToAmplitudes) {
            refinementData.intensities_to_amplitudes();
        }
    } catch (IOException ioe) {
        System.out.println("IO Exception: " + ioe.getMessage());
        return false;
    }

    sb.append(String.format(" HKL data is %s\n", transpose ? "transposed" : "not transposed"));
    sb.append(String.format(" HKL read in:                             %d\n", nRead));
    sb.append(String.format(" HKL read as friedel mates:               %d\n", nFriedel));
    sb.append(String.format(" HKL with NaN (ignored):                  %d\n", nNAN));
    sb.append(String.format(" HKL NOT read in (status <, -, h or l):   %d\n", nCIFIgnore));
    sb.append(String.format(" HKL NOT read in (too high resolution):   %d\n", nRes));
    sb.append(String.format(" HKL NOT read in (not in internal list?): %d\n", nIgnore));
    sb.append(String.format(" HKL NOT read in (F/sigF cutoff):         %d\n", nCut));
    sb.append(String.format(" HKL in internal list:                    %d\n", reflectionList.hkllist.size()));

    if (logger.isLoggable(Level.INFO)) {
        logger.info(sb.toString());
    }

    if (rFree < 0) {
        refinementData.generateRFree();
    }
    return true;
}

From source file:edu.harvard.iq.dvn.ingest.statdataio.impl.plugins.por.PORFileReader.java

private void decodeSec2(BufferedReader reader) throws IOException {
    dbgLog.fine("***** decodeSec2(): start *****");
    if (reader == null) {
        throw new IllegalArgumentException("decodeSec2: stream == null!");
    }//from   www . j a  v  a2s.  c  o  m

    // Because a 64-bit machine may not save the first 40
    // bytes of a POR file in a way as a 32-bit machine does,
    // the first 5 lines of a POR file is excluded from the read-back
    // file and the new 1st line contains the format mark "SPSSPORT"
    // somewhere in it.

    // mark the start position for the later rewind
    if (reader.markSupported()) {
        reader.mark(100000);
    }

    char[] sixthLineCharArray = new char[80];
    int nbytes_sixthLine = reader.read(sixthLineCharArray);

    String sixthLine = new String(sixthLineCharArray);
    dbgLog.info("sixthLineCharArray=" + Arrays.deepToString(ArrayUtils.toObject(sixthLineCharArray)));
    int signatureLocation = sixthLine.indexOf(POR_MARK);

    if (signatureLocation >= 0) {
        dbgLog.info("format signature was found at:" + signatureLocation);
    } else {
        dbgLog.severe("signature string was not found");
        throw new IOException("signature string was not found");
    }

    // rewind the position to the beginning
    reader.reset();

    // skip bytes up to the signature string
    long skippedBytes = reader.skip(signatureLocation);

    char[] sec2_leader = new char[POR_MARK.length()];
    int nbytes_sec2_leader = reader.read(sec2_leader);

    String leader_string = new String(sec2_leader);

    dbgLog.info("format signature [SPSSPORT] detected=" + leader_string);

    if (leader_string.equals("SPSSPORT")) {
        dbgLog.info("signature was correctly detected");

    } else {
        dbgLog.severe("the format signature is not found at the previously located column");
        throw new IOException("decodeSec2: failed to find the signature string");
    }

    int length_section_2 = LENGTH_SECTION_2;

    char[] Sec2_bytes = new char[length_section_2];

    int nbytes_sec2 = reader.read(Sec2_bytes);

    if (nbytes_sec2 == 0) {
        dbgLog.severe("decodeSec2: reading error");
        throw new IOException("decodeSec2: reading error");
    } else {
        dbgLog.fine("bytes read=" + nbytes_sec2);
    }

    String sec2 = new String(Sec2_bytes);
    dbgLog.fine("sec2[creation date/time]=" + sec2);

    // sec2
    //       0123456789012345678
    //       A8/YYYYMMDD6/HHMMSS
    // thus
    // section2 should has 3 elements

    String[] section2 = StringUtils.split(sec2, '/');

    dbgLog.fine("section2=" + StringUtils.join(section2, "|"));

    String fileCreationDate = null;
    String fileCreationTime = null;
    if ((section2.length == 3) && (section2[0].startsWith("A"))) {
        fileCreationDate = section2[1].substring(0, 7);
        fileCreationTime = section2[2];
    } else {
        dbgLog.severe("decodeSec2: file creation date/time were not correctly detected");
        throw new IOException("decodeSec2: file creation date/time were not correctly detected");
    }
    dbgLog.fine("fileCreationDate=" + fileCreationDate);
    dbgLog.fine("fileCreationTime=" + fileCreationTime);
    smd.getFileInformation().put("fileCreationDate", fileCreationDate);
    smd.getFileInformation().put("fileCreationTime", fileCreationTime);
    smd.getFileInformation().put("varFormat_schema", "SPSS");
}

From source file:org.orekit.files.ccsds.OEMParser.java

/**
 * Parse an ephemeris data line and add its content to the ephemerides
 * block.//from  w  w w.ja  v a  2 s.  co  m
 *
 * @param reader the reader
 * @param pi the parser info
 * @exception IOException if an error occurs while reading from the stream
 * @exception OrekitException if a date cannot be parsed
 */
private void parseEphemeridesDataLines(final BufferedReader reader, final ParseInfo pi)
        throws OrekitException, IOException {

    for (String line = reader.readLine(); line != null; line = reader.readLine()) {

        ++pi.lineNumber;
        if (line.trim().length() > 0) {
            pi.keyValue = new KeyValue(line, pi.lineNumber, pi.fileName);
            if (pi.keyValue.getKeyword() == null) {
                Scanner sc = null;
                try {
                    sc = new Scanner(line);
                    final AbsoluteDate date = parseDate(sc.next(),
                            pi.lastEphemeridesBlock.getMetaData().getTimeSystem());
                    final Vector3D position = new Vector3D(Double.parseDouble(sc.next()) * 1000,
                            Double.parseDouble(sc.next()) * 1000, Double.parseDouble(sc.next()) * 1000);
                    final Vector3D velocity = new Vector3D(Double.parseDouble(sc.next()) * 1000,
                            Double.parseDouble(sc.next()) * 1000, Double.parseDouble(sc.next()) * 1000);
                    final CartesianOrbit orbit = new CartesianOrbit(new PVCoordinates(position, velocity),
                            pi.lastEphemeridesBlock.getMetaData().getFrame(), date, pi.file.getMuUsed());
                    Vector3D acceleration = null;
                    if (sc.hasNext()) {
                        acceleration = new Vector3D(Double.parseDouble(sc.next()) * 1000,
                                Double.parseDouble(sc.next()) * 1000, Double.parseDouble(sc.next()) * 1000);
                    }
                    final OEMFile.EphemeridesDataLine epDataLine = new OEMFile.EphemeridesDataLine(orbit,
                            acceleration);
                    pi.lastEphemeridesBlock.getEphemeridesDataLines().add(epDataLine);
                } catch (NumberFormatException nfe) {
                    throw new OrekitException(OrekitMessages.UNABLE_TO_PARSE_LINE_IN_FILE, pi.lineNumber,
                            pi.fileName, line);
                } finally {
                    if (sc != null) {
                        sc.close();
                    }
                }
            } else {
                switch (pi.keyValue.getKeyword()) {
                case META_START:
                    pi.lastEphemeridesBlock.setEphemeridesDataLinesComment(pi.commentTmp);
                    pi.commentTmp.clear();
                    pi.lineNumber--;
                    reader.reset();
                    return;
                case COVARIANCE_START:
                    pi.lastEphemeridesBlock.setEphemeridesDataLinesComment(pi.commentTmp);
                    pi.commentTmp.clear();
                    pi.lineNumber--;
                    reader.reset();
                    return;
                case COMMENT:
                    pi.commentTmp.add(pi.keyValue.getValue());
                    break;
                default:
                    throw new OrekitException(OrekitMessages.CCSDS_UNEXPECTED_KEYWORD, pi.lineNumber,
                            pi.fileName, line);
                }
            }
        }
        reader.mark(300);

    }
}

From source file:edu.harvard.iq.dataverse.ingest.tabulardata.impl.plugins.por.PORFileReader.java

private void decodeSec2(BufferedReader reader) throws IOException {
    dbgLog.fine("decodeSec2(): start");
    if (reader == null) {
        throw new IllegalArgumentException("decodeSec2: stream == null!");
    }//from  w ww . jav  a  2 s  .c  om

    // Because a 64-bit machine may not save the first 40
    // bytes of a POR file in a way as a 32-bit machine does,
    // the first 5 lines of a POR file is excluded from the read-back
    // file and the new 1st line contains the format mark "SPSSPORT"
    // somewhere in it.

    // mark the start position for the later rewind
    if (reader.markSupported()) {
        reader.mark(100000);
    }

    char[] sixthLineCharArray = new char[80];
    int nbytes_sixthLine = reader.read(sixthLineCharArray);

    String sixthLine = new String(sixthLineCharArray);
    dbgLog.fine("sixthLineCharArray=" + Arrays.deepToString(ArrayUtils.toObject(sixthLineCharArray)));
    int signatureLocation = sixthLine.indexOf(POR_MARK);

    if (signatureLocation >= 0) {
        dbgLog.fine("format signature was found at:" + signatureLocation);
    } else {
        dbgLog.severe("signature string was not found");
        throw new IOException("signature string was not found");
    }

    // rewind the position to the beginning
    reader.reset();

    // skip bytes up to the signature string
    long skippedBytes = reader.skip(signatureLocation);

    char[] sec2_leader = new char[POR_MARK.length()];
    int nbytes_sec2_leader = reader.read(sec2_leader);

    String leader_string = new String(sec2_leader);

    dbgLog.fine("format signature [SPSSPORT] detected=" + leader_string);

    if (leader_string.equals("SPSSPORT")) {
        dbgLog.fine("signature was correctly detected");

    } else {
        dbgLog.severe("the format signature is not found at the previously located column");
        throw new IOException("decodeSec2: failed to find the signature string");
    }

    int length_section_2 = LENGTH_SECTION_2;

    char[] Sec2_bytes = new char[length_section_2];

    int nbytes_sec2 = reader.read(Sec2_bytes);

    if (nbytes_sec2 == 0) {
        dbgLog.severe("decodeSec2: reading error");
        throw new IOException("decodeSec2: reading error");
    } else {
        dbgLog.fine("bytes read=" + nbytes_sec2);
    }

    String sec2 = new String(Sec2_bytes);
    dbgLog.fine("sec2[creation date/time]=" + sec2);

    // sec2
    //       0123456789012345678
    //       A8/YYYYMMDD6/HHMMSS
    // thus
    // section2 should has 3 elements

    String[] section2 = StringUtils.split(sec2, '/');

    dbgLog.fine("section2=" + StringUtils.join(section2, "|"));

    String fileCreationDate = null;
    String fileCreationTime = null;
    if ((section2.length == 3) && (section2[0].startsWith("A"))) {
        fileCreationDate = section2[1].substring(0, 7);
        fileCreationTime = section2[2];
    } else {
        dbgLog.severe("decodeSec2: file creation date/time were not correctly detected");
        throw new IOException("decodeSec2: file creation date/time were not correctly detected");
    }
    dbgLog.fine("fileCreationDate=" + fileCreationDate);
    dbgLog.fine("fileCreationTime=" + fileCreationTime);
    ///smd.getFileInformation().put("fileCreationDate", fileCreationDate);
    ///smd.getFileInformation().put("fileCreationTime", fileCreationTime);
    ///smd.getFileInformation().put("varFormat_schema", "SPSS");
    dbgLog.fine("decodeSec2(): end");
}

From source file:com.vuze.android.remote.rpc.RestJsonClient.java

public static Map<?, ?> connect(String id, String url, Map<?, ?> jsonPost, Header[] headers,
        UsernamePasswordCredentials creds, boolean sendGzip) throws RPCException {
    long readTime = 0;
    long connSetupTime = 0;
    long connTime = 0;
    int bytesRead = 0;
    if (DEBUG_DETAILED) {
        Log.d(TAG, id + "] Execute " + url);
    }//from   w w w.ja v  a 2s.co  m
    long now = System.currentTimeMillis();
    long then;

    Map<?, ?> json = Collections.EMPTY_MAP;

    try {

        URI uri = new URI(url);
        int port = uri.getPort();

        BasicHttpParams basicHttpParams = new BasicHttpParams();
        HttpProtocolParams.setUserAgent(basicHttpParams, "Vuze Android Remote");

        DefaultHttpClient httpclient;
        if ("https".equals(uri.getScheme())) {
            httpclient = MySSLSocketFactory.getNewHttpClient(port);
        } else {
            httpclient = new DefaultHttpClient(basicHttpParams);
        }

        //AndroidHttpClient.newInstance("Vuze Android Remote");

        // This doesn't set the "Authorization" header!?
        httpclient.getCredentialsProvider().setCredentials(new AuthScope(null, -1), creds);

        // Prepare a request object
        HttpRequestBase httpRequest = jsonPost == null ? new HttpGet(uri) : new HttpPost(uri); // IllegalArgumentException

        if (creds != null) {
            byte[] toEncode = (creds.getUserName() + ":" + creds.getPassword()).getBytes();
            String encoding = Base64Encode.encodeToString(toEncode, 0, toEncode.length);
            httpRequest.setHeader("Authorization", "Basic " + encoding);
        }

        if (jsonPost != null) {
            HttpPost post = (HttpPost) httpRequest;
            String postString = JSONUtils.encodeToJSON(jsonPost);
            if (AndroidUtils.DEBUG_RPC) {
                Log.d(TAG, id + "]  Post: " + postString);
            }

            AbstractHttpEntity entity = (sendGzip && Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO)
                    ? getCompressedEntity(postString)
                    : new StringEntity(postString);
            post.setEntity(entity);

            post.setHeader("Accept", "application/json");
            post.setHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
        }

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO) {
            setupRequestFroyo(httpRequest);
        }

        if (headers != null) {
            for (Header header : headers) {
                httpRequest.setHeader(header);
            }
        }

        // Execute the request
        HttpResponse response;

        then = System.currentTimeMillis();
        if (AndroidUtils.DEBUG_RPC) {
            connSetupTime = (then - now);
            now = then;
        }

        httpclient.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
            @Override
            public boolean retryRequest(IOException e, int i, HttpContext httpContext) {
                if (i < 2) {
                    return true;
                }
                return false;
            }
        });
        response = httpclient.execute(httpRequest);

        then = System.currentTimeMillis();
        if (AndroidUtils.DEBUG_RPC) {
            connTime = (then - now);
            now = then;
        }

        HttpEntity entity = response.getEntity();

        // XXX STATUSCODE!

        StatusLine statusLine = response.getStatusLine();
        if (AndroidUtils.DEBUG_RPC) {
            Log.d(TAG, "StatusCode: " + statusLine.getStatusCode());
        }

        if (entity != null) {

            long contentLength = entity.getContentLength();
            if (contentLength >= Integer.MAX_VALUE - 2) {
                throw new RPCException("JSON response too large");
            }

            // A Simple JSON Response Read
            InputStream instream = (Build.VERSION.SDK_INT >= Build.VERSION_CODES.FROYO)
                    ? getUngzippedContent(entity)
                    : entity.getContent();
            InputStreamReader isr = new InputStreamReader(instream, "utf8");

            StringBuilder sb = null;
            BufferedReader br = null;
            // JSONReader is 10x slower, plus I get more OOM errors.. :(
            //            final boolean useStringBuffer = contentLength > (4 * 1024 * 1024) ? false
            //                  : DEFAULT_USE_STRINGBUFFER;
            final boolean useStringBuffer = DEFAULT_USE_STRINGBUFFER;

            if (useStringBuffer) {
                // Setting capacity saves StringBuffer from going through many
                // enlargeBuffers, and hopefully allows toString to not make a copy
                sb = new StringBuilder(contentLength > 512 ? (int) contentLength + 2 : 512);
            } else {
                if (AndroidUtils.DEBUG_RPC) {
                    Log.d(TAG, "Using BR. ContentLength = " + contentLength);
                }
                br = new BufferedReader(isr, 8192);
                br.mark(32767);
            }

            try {

                // 9775 files on Nexus 7 (~2,258,731 bytes)
                // fastjson 1.1.46 (String)       :  527- 624ms
                // fastjson 1.1.39 (String)       :  924-1054ms
                // fastjson 1.1.39 (StringBuilder): 1227-1463ms
                // fastjson 1.1.39 (BR)           : 2233-2260ms
                // fastjson 1.1.39 (isr)          :      2312ms
                // GSON 2.2.4 (String)            : 1539-1760ms
                // GSON 2.2.4 (BufferedReader)    : 2646-3060ms
                // JSON-SMART 1.3.1 (String)      :  572- 744ms (OOMs more often than fastjson)

                if (useStringBuffer) {
                    char c[] = new char[8192];
                    while (true) {
                        int read = isr.read(c);
                        if (read < 0) {
                            break;
                        }
                        sb.append(c, 0, read);
                    }

                    if (AndroidUtils.DEBUG_RPC) {
                        then = System.currentTimeMillis();
                        if (DEBUG_DETAILED) {
                            if (sb.length() > 2000) {
                                Log.d(TAG, id + "] " + sb.substring(0, 2000) + "...");
                            } else {
                                Log.d(TAG, id + "] " + sb.toString());
                            }
                        }
                        bytesRead = sb.length();
                        readTime = (then - now);
                        now = then;
                    }

                    json = JSONUtils.decodeJSON(sb.toString());
                    //json = JSONUtilsGSON.decodeJSON(sb.toString());
                } else {

                    //json = JSONUtils.decodeJSON(isr);
                    json = JSONUtils.decodeJSON(br);
                    //json = JSONUtilsGSON.decodeJSON(br);
                }

            } catch (Exception pe) {

                //               StatusLine statusLine = response.getStatusLine();
                if (statusLine != null && statusLine.getStatusCode() == 409) {
                    throw new RPCException(response, "409");
                }

                try {
                    String line;
                    if (useStringBuffer) {
                        line = sb.subSequence(0, Math.min(128, sb.length())).toString();
                    } else {
                        br.reset();
                        line = br.readLine().trim();
                    }

                    isr.close();

                    if (AndroidUtils.DEBUG_RPC) {
                        Log.d(TAG, id + "]line: " + line);
                    }
                    Header contentType = entity.getContentType();
                    if (line.startsWith("<") || line.contains("<html")
                            || (contentType != null && contentType.getValue().startsWith("text/html"))) {
                        // TODO: use android strings.xml
                        throw new RPCException(response,
                                "Could not retrieve remote client location information.  The most common cause is being on a guest wifi that requires login before using the internet.");
                    }
                } catch (IOException ignore) {

                }

                Log.e(TAG, id, pe);
                if (statusLine != null) {
                    String msg = statusLine.getStatusCode() + ": " + statusLine.getReasonPhrase() + "\n"
                            + pe.getMessage();
                    throw new RPCException(msg, pe);
                }
                throw new RPCException(pe);
            } finally {
                closeOnNewThread(useStringBuffer ? isr : br);
            }

            if (AndroidUtils.DEBUG_RPC) {
                //               Log.d(TAG, id + "]JSON Result: " + json);
            }

        }
    } catch (RPCException e) {
        throw e;
    } catch (Throwable e) {
        Log.e(TAG, id, e);
        throw new RPCException(e);
    }

    if (AndroidUtils.DEBUG_RPC) {
        then = System.currentTimeMillis();
        Log.d(TAG, id + "] conn " + connSetupTime + "/" + connTime + "ms. Read " + bytesRead + " in " + readTime
                + "ms, parsed in " + (then - now) + "ms");
    }
    return json;
}