List of usage examples for java.io BufferedReader ready
public boolean ready() throws IOException
From source file:org.kalypso.gml.processes.constDelaunay.TriangleExe.java
public static GM_Position[] parseTriangleNodeOutput(final BufferedReader nodeReader) throws IOException { // ignore first line, we don't check the file format final String firstLine = nodeReader.readLine(); final StringTokenizer firstTokenizer = new StringTokenizer(firstLine); final int pointCount = Integer.parseInt(firstTokenizer.nextToken()); /* final int coordCount = */Integer.parseInt(firstTokenizer.nextToken()); final int attCount = Integer.parseInt(firstTokenizer.nextToken()); final GM_Position[] points = new GM_Position[pointCount]; while (nodeReader.ready()) { final String string = nodeReader.readLine(); if (string == null) break; if (string.startsWith("#")) //$NON-NLS-1$ continue; final StringTokenizer tokenizer = new StringTokenizer(string); final int id = Integer.parseInt(tokenizer.nextToken()); final double x = Double.parseDouble(tokenizer.nextToken()); final double y = Double.parseDouble(tokenizer.nextToken()); final double z = attCount > 0 ? Double.parseDouble(tokenizer.nextToken()) : Double.NaN; final GM_Position position = GeometryFactory.createGM_Position(x, y, z); points[id] = position;// w ww . j a v a2s. co m } return points; }
From source file:org.cesecore.certificates.util.DnComponents.java
/** * Reads properties from a properties file. Fails nicely if file wasn't found. * /*from w w w.j a v a2s .com*/ * @param propertiesFile */ private static void loadProfileMappingsFromFile(String propertiesFile) { // Read the file to an array of lines String line; BufferedReader in = null; InputStreamReader inf = null; try { InputStream is = obj.getClass().getResourceAsStream(propertiesFile); if (is != null) { inf = new InputStreamReader(is); in = new BufferedReader(inf); if (!in.ready()) { throw new IOException("Couldn't read " + propertiesFile); } String[] splits = null; int lines = 0; ArrayList<Integer> dnids = new ArrayList<Integer>(); ArrayList<Integer> profileids = new ArrayList<Integer>(); while ((line = in.readLine()) != null) { if (!line.startsWith("#")) { // # is a comment line splits = StringUtils.split(line, ';'); if ((splits != null) && (splits.length > 5)) { String type = splits[0]; String dnname = splits[1]; Integer dnid = Integer.valueOf(splits[2]); String profilename = splits[3]; Integer profileid = Integer.valueOf(splits[4]); String errstr = splits[5]; String langstr = splits[6]; if (dnids.contains(dnid)) { log.error("Duplicated DN Id " + dnid + " detected in mapping file."); } else { dnids.add(dnid); } if (profileids.contains(profileid)) { log.error("Duplicated Profile Id " + profileid + " detected in mapping file."); } else { profileids.add(profileid); } // Fill maps dnNameIdMap.put(dnname, dnid); profileNameIdMap.put(profilename, profileid); dnIdToProfileNameMap.put(dnid, profilename); dnIdToProfileIdMap.put(dnid, profileid); dnIdErrorMap.put(dnid, errstr); profileIdToDnIdMap.put(profileid, dnid); dnErrorTextMap.put(dnid, errstr); profileNameLanguageMap.put(profilename, langstr); profileIdLanguageMap.put(profileid, langstr); if (type.equals("DN")) { dnProfileFields.add(profilename); dnProfileFieldsHashSet.add(profilename); dnLanguageTexts.add(langstr); dnDnIds.add(dnid); dnExtractorFields.add(dnname + "="); dnIdToExtractorFieldMap.put(dnid, dnname + "="); } if (type.equals("ALTNAME")) { altNameFields.add(dnname); altNameFieldsHashSet.add(dnname); altNameLanguageTexts.add(langstr); altNameDnIds.add(dnid); altNameExtractorFields.add(dnname + "="); altNameIdToExtractorFieldMap.put(dnid, dnname + "="); } if (type.equals("DIRATTR")) { dirAttrFields.add(dnname); dirAttrFieldsHashSet.add(dnname); dirAttrLanguageTexts.add(langstr); dirAttrDnIds.add(dnid); dirAttrExtractorFields.add(dnname + "="); dirAttrIdToExtractorFieldMap.put(dnid, dnname + "="); } lines++; } } } in.close(); if (log.isDebugEnabled()) { log.debug("Read profile maps with " + lines + " lines."); } } else { if (log.isDebugEnabled()) { log.debug("Properties file " + propertiesFile + " was not found."); } } } catch (IOException e) { log.error("Can not load profile mappings: ", e); } finally { try { if (inf != null) { inf.close(); } if (in != null) { in.close(); } } catch (IOException e) { } } }
From source file:edu.ku.brc.specify.dbsupport.cleanuptools.LocalityCleanup.java
/** * //from w ww. j a v a 2 s.c om */ public static void fixGeonames() { String connectStr = "jdbc:mysql://localhost/"; String dbName = "testfish"; DBConnection dbc = new DBConnection("root", "root", connectStr + dbName + "?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true", "com.mysql.jdbc.Driver", "org.hibernate.dialect.MySQLDialect", dbName); Connection conn = dbc.createConnection(); BasicSQLUtils.setDBConnection(conn); File file = new File("/Users/rods/Downloads/allCountries.txt"); try { int cnt = 0; int updateCnt = 0; InputStream fileIS = new FileInputStream(file); PreparedStatement ps = conn.prepareStatement("UPDATE geoname SET name=? WHERE geonameId = ?"); BufferedReader bufReader = new BufferedReader(new InputStreamReader(fileIS)); while (bufReader.ready()) { String s = bufReader.readLine(); //System.out.println(s); String str = new String(s.getBytes(), "UTF8"); //System.out.println(str); String[] toks = new StrTokenizer(str, "\t").getTokenArray(); //StringUtils.tokenizeToStringArray(str, "\t"); Integer key = Integer.parseInt(toks[0]); if (BasicSQLUtils.getCountAsInt(conn, "SELECT COUNT(*) FROM geoname WHERE geonameId = " + key) == 1) { ps.setString(1, toks[1]); ps.setInt(2, key); System.out.println(toks[1] + " " + key); if (ps.executeUpdate() != 1) { System.err.println("Error updating " + key); } updateCnt++; } cnt++; if (cnt % 1000 == 0) { System.out.println(cnt); } } bufReader.close(); ps.close(); conn.close(); System.out.println(cnt + " " + updateCnt); } catch (Exception e1) { // TODO Auto-generated catch block e1.printStackTrace(); } }
From source file:org.ejbca.util.dn.DnComponents.java
/** * Load DN ordering used in CertTools.stringToBCDNString etc. * Loads from file placed in src/dncomponents.properties * /*from ww w .j a v a 2s. c om*/ * A line is: * DNName;DNid;ProfileName;ProfileId,ErrorString,LanguageConstant * */ private static void loadMappings() { // Read the file to an array of lines String line; BufferedReader in = null; InputStreamReader inf = null; try { InputStream is = obj.getClass().getResourceAsStream("/profilemappings.properties"); //log.info("is is: " + is); if (is != null) { inf = new InputStreamReader(is); in = new BufferedReader(inf); if (!in.ready()) { throw new IOException(); } String[] splits = null; int lines = 0; ArrayList<Integer> dnids = new ArrayList<Integer>(); ArrayList<Integer> profileids = new ArrayList<Integer>(); while ((line = in.readLine()) != null) { if (!line.startsWith("#")) { // # is a comment line splits = StringUtils.split(line, ';'); if ((splits != null) && (splits.length > 5)) { String type = splits[0]; String dnname = splits[1]; Integer dnid = Integer.valueOf(splits[2]); String profilename = splits[3]; Integer profileid = Integer.valueOf(splits[4]); String errstr = splits[5]; String langstr = splits[6]; if (dnids.contains(dnid)) { log.error("Duplicated DN Id " + dnid + " detected in mapping file."); } else { dnids.add(dnid); } if (profileids.contains(profileid)) { log.error("Duplicated Profile Id " + profileid + " detected in mapping file."); } else { profileids.add(profileid); } // Fill maps dnNameIdMap.put(dnname, dnid); profileNameIdMap.put(profilename, profileid); dnIdToProfileNameMap.put(dnid, profilename); dnIdToProfileIdMap.put(dnid, profileid); dnIdErrorMap.put(dnid, errstr); profileIdToDnIdMap.put(profileid, dnid); dnErrorTextMap.put(dnid, errstr); profileNameLanguageMap.put(profilename, langstr); profileIdLanguageMap.put(profileid, langstr); if (type.equals("DN")) { dnProfileFields.add(profilename); dnProfileFieldsHashSet.add(profilename); dnLanguageTexts.add(langstr); dnDnIds.add(dnid); dnExtractorFields.add(dnname + "="); dnIdToExtractorFieldMap.put(dnid, dnname + "="); } if (type.equals("ALTNAME")) { altNameFields.add(dnname); altNameFieldsHashSet.add(dnname); altNameLanguageTexts.add(langstr); altNameDnIds.add(dnid); altNameExtractorFields.add(dnname + "="); altNameIdToExtractorFieldMap.put(dnid, dnname + "="); } if (type.equals("DIRATTR")) { dirAttrFields.add(dnname); dirAttrFieldsHashSet.add(dnname); dirAttrLanguageTexts.add(langstr); dirAttrDnIds.add(dnid); dirAttrExtractorFields.add(dnname + "="); dirAttrIdToExtractorFieldMap.put(dnid, dnname + "="); } lines++; } } } in.close(); log.debug("Read profile maps with " + lines + " lines."); } else { throw new IOException("Input stream for /profilemappings.properties is null"); } } catch (IOException e) { log.error("Can not load profile mappings: ", e); } finally { try { if (inf != null) { inf.close(); } if (in != null) { in.close(); } } catch (IOException e) { } } }
From source file:org.ejbca.util.dn.DnComponents.java
/** * Load DN ordering used in CertTools.stringToBCDNString etc. * Loads from file placed in src/dncomponents.properties * *///from ww w . ja va 2s. com private static void loadOrdering() { // Read the file to an array of lines String line; LinkedHashMap<String, DERObjectIdentifier> map = new LinkedHashMap<String, DERObjectIdentifier>(); BufferedReader in = null; InputStreamReader inf = null; try { InputStream is = obj.getClass().getResourceAsStream("/dncomponents.properties"); //log.info("is is: " + is); if (is != null) { inf = new InputStreamReader(is); //inf = new FileReader("c:\\foo.properties"); in = new BufferedReader(inf); if (!in.ready()) { throw new IOException(); } String[] splits = null; while ((line = in.readLine()) != null) { if (!line.startsWith("#")) { // # is a comment line splits = StringUtils.split(line, '='); if ((splits != null) && (splits.length > 1)) { String name = splits[0].toLowerCase(); DERObjectIdentifier oid = new DERObjectIdentifier(splits[1]); map.put(name, oid); } } } in.close(); // Now we have read it in, transfer it to the main oid map log.info("Using DN components from properties file"); oids.clear(); oids.putAll(map); Set<String> keys = map.keySet(); // Set the maps to the desired ordering dNObjectsForward = (String[]) keys.toArray(new String[0]); } else { log.debug("Using default values for DN components"); } } catch (IOException e) { log.debug("Using default values for DN components"); } finally { try { if (inf != null) { inf.close(); } if (in != null) { in.close(); } } catch (IOException e) { } } }
From source file:org.cesecore.certificates.util.DnComponents.java
/** * Load DN ordering used in CertTools.stringToBCDNString etc. * Loads from file placed in src/dncomponents.properties * *//*www . j a v a 2 s .co m*/ private static void loadOrdering() { // Read the file to an array of lines String line; LinkedHashMap<String, ASN1ObjectIdentifier> map = new LinkedHashMap<String, ASN1ObjectIdentifier>(); BufferedReader in = null; InputStreamReader inf = null; try { InputStream is = obj.getClass().getResourceAsStream("/dncomponents.properties"); //log.info("is is: " + is); if (is != null) { inf = new InputStreamReader(is); //inf = new FileReader("c:\\foo.properties"); in = new BufferedReader(inf); if (!in.ready()) { throw new IOException(); } String[] splits = null; while ((line = in.readLine()) != null) { if (!line.startsWith("#")) { // # is a comment line splits = StringUtils.split(line, '='); if ((splits != null) && (splits.length > 1)) { String name = splits[0].toLowerCase(); ASN1ObjectIdentifier oid = new ASN1ObjectIdentifier(splits[1]); map.put(name, oid); } } } in.close(); // Now we have read it in, transfer it to the main oid map log.info("Using DN components from properties file"); oids.clear(); oids.putAll(map); Set<String> keys = map.keySet(); // Set the maps to the desired ordering dNObjectsForward = (String[]) keys.toArray(new String[keys.size()]); } else { log.debug("Using default values for DN components"); } } catch (IOException e) { log.debug("Using default values for DN components"); } finally { try { if (inf != null) { inf.close(); } if (in != null) { in.close(); } } catch (IOException e) { } } }
From source file:org.wso2.carbon.repository.core.utils.MediaTypesUtils.java
private static void populateMediaTypeMappings() throws RepositoryException { BufferedReader reader; humanReadableMediaTypeMap = new HashMap<String, String>(); try {/*from w w w.java 2s . co m*/ File mimeFile = getHumanMediaTypeMappingsFile(); reader = new BufferedReader(new InputStreamReader(new FileInputStream(mimeFile))); } catch (Exception e) { String msg = FAILED_TO_READ_THE_THE_HUMAN_READABLE_MEDIA_TYPE_MIME_TYPE_MAPPINGS_FILE_MSG; log.warn(msg, e); return; } try { while (reader.ready()) { String mediaTypeData = reader.readLine().trim(); if (mediaTypeData.startsWith("#")) { // ignore the comments continue; } if (mediaTypeData.length() == 0) { // ignore the blank lines continue; } // mime.mappings file delimits media types:extensions by tabs. if there is no // extension associated with a media type, there are no tabs in the line. so we // don't need such lines. if (mediaTypeData.indexOf('\t') > 0) { String[] parts = mediaTypeData.split("\t+"); if (parts.length == 2 && parts[0].length() > 0 && parts[1].length() > 0) { // there can multiple extensions associated with a single media type. in // that case, extensions are delimited by a space. humanReadableMediaTypeMap.put(parts[1], parts[0]); } } } } catch (IOException e) { String msg = FAILED_TO_READ_THE_THE_HUMAN_READABLE_MEDIA_TYPE_MIME_TYPE_MAPPINGS_FILE_MSG; log.error(msg, e); throw new RepositoryUserContentException(msg, RepositoryErrorCodes.UNAVAILABLE_FILE_REFERRED); } finally { try { reader.close(); if (humanReadableMediaTypeMap == null) { humanReadableMediaTypeMap = Collections.emptyMap(); } } catch (IOException ignore) { } } }
From source file:org.jpos.q2.Q2.java
public static String getLicensee() { InputStream is = Q2.class.getResourceAsStream(LICENSEE); ByteArrayOutputStream baos = new ByteArrayOutputStream(); if (is != null) { BufferedReader br = new BufferedReader(new InputStreamReader(is)); PrintStream p = new PrintStream(baos); p.println();//from w w w.j a v a2 s . co m p.println(); try { while (br.ready()) p.println(br.readLine()); } catch (Exception e) { e.printStackTrace(p); } } return baos.toString(); }
From source file:org.graphwalker.Util.java
protected static String readFile(final File file) throws IOException { StringBuilder stringBuilder = new StringBuilder(); FileReader reader = null;/*from ww w . ja v a2s . c o m*/ try { reader = new FileReader(file); BufferedReader bufferedReader = new BufferedReader(reader); while (bufferedReader.ready()) { stringBuilder.append(bufferedReader.readLine()); stringBuilder.append(System.getProperty("line.separator")); } bufferedReader.close(); } catch (IOException e) { throw e; // TODO: remove the IOE from method signature and throw a new // exception extending runtime } finally { closeQuietly(reader); } return stringBuilder.toString(); }
From source file:com.instagram.test.InstagramSerDeTest.java
@Test public void Tests() { mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, Boolean.TRUE); mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, Boolean.TRUE); mapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, Boolean.TRUE); InputStream is = InstagramSerDeTest.class.getResourceAsStream("/instagram_jsons.txt"); InputStreamReader isr = new InputStreamReader(is); BufferedReader br = new BufferedReader(isr); try {/* w w w . j a v a2 s .c o m*/ while (br.ready()) { String line = br.readLine(); LOGGER.debug(line); Instagram ser = mapper.readValue(line, Instagram.class); String des = mapper.writeValueAsString(ser); LOGGER.debug(des); } } catch (Exception e) { e.printStackTrace(); Assert.fail(); } }