List of usage examples for java.util Scanner useDelimiter
public Scanner useDelimiter(String pattern)
From source file:com.cloudant.client.org.lightcouch.internal.CouchDbUtil.java
public static String streamToString(InputStream in) { Scanner s = new Scanner(in, "UTF-8"); s.useDelimiter("\\A"); String str = s.hasNext() ? s.next() : null; close(in);/*from w w w.j a v a 2s .co m*/ s.close();// mdb return str; }
From source file:cz.autoclient.github.json.GitHubJson.java
public static JSONObject fromURL(URL url) { Scanner s; try {/*from w ww. jav a2 s.co m*/ s = new Scanner(url.openStream(), "UTF-8"); } catch (IOException ex) { throw new IllegalArgumentException("JSON file not found at " + url); } s.useDelimiter("\\A"); if (s.hasNext()) { String out = s.next(); try { return new JSONObject(out); } catch (JSONException ex) { throw new IllegalArgumentException( "Invalid JSON contents at " + url + " - \n JSON error:" + ex); //Logger.getLogger(DataLoader.class.getName()).log(Level.SEVERE, null, ex); } } else throw new IllegalArgumentException("JSON file not found at " + url); }
From source file:aiai.ai.launchpad.experiment.ExperimentUtils.java
public static NumberOfVariants getNumberOfVariants(String variantsAsStr) { if (StringUtils.isBlank(variantsAsStr)) { return ZERO_VARIANT; }/*from w w w.ja v a 2s. c om*/ String s = variantsAsStr.trim(); if (s.charAt(0) != '(' && s.charAt(0) != '[' && !StringUtils.startsWithIgnoreCase(s.toLowerCase(), RANGE)) { final NumberOfVariants variants = new NumberOfVariants(true, null, 1); variants.values.add(s); return variants; } if (s.startsWith("[")) { int count = 0; final NumberOfVariants variants = new NumberOfVariants(true, null, 0); for (StringTokenizer st = new StringTokenizer(s, "[,] "); st.hasMoreTokens();) { String token = st.nextToken(); variants.values.add(token); count++; } variants.count = count; return variants; } String s1 = s; if (StringUtils.startsWithIgnoreCase(s1, RANGE)) { s1 = s1.substring(RANGE.length()).trim(); } if (s1.charAt(0) == '(') { Scanner scanner = new Scanner(s1.substring(1)); scanner.useDelimiter("[,)]"); int start; int end; int change; try { start = Integer.parseInt(scanner.next().trim()); end = Integer.parseInt(scanner.next().trim()); change = Integer.parseInt(scanner.next().trim()); } catch (NumberFormatException | NoSuchElementException e) { return new NumberOfVariants(false, "Wrong string format for string: " + s, 0); } int count = 0; final NumberOfVariants variants = new NumberOfVariants(true, null, 0); for (int i = start; i < end; i += change) { variants.values.add(Integer.toString(i)); count++; if (count > 100) { return new NumberOfVariants(false, "Too many variants for string: " + s, 0); } } variants.count = count; return variants; } return new NumberOfVariants(false, "Wrong number format for string: " + s, 0); }
From source file:org.bibsonomy.rest.utils.HeaderUtils.java
/** * /* ww w .j a v a2 s . c o m*/ * @param acceptHeader * the HTML ACCEPT Header * <br/>example: * <code>ACCEPT: text/xml,text/html;q=0.9,text/plain;q=0.8,image/png</code> * would be interpreted in the following precedence: * <ol> * <li>text/xml</li> * <li>image/png</li> * <li>text/html</li> * <li>text/plain</li> * </ol> * ) * @return a sorted map with the precedences */ public static SortedMap<Double, Vector<String>> getPreferredTypes(final String acceptHeader) { // maps the q-value to output format (reverse order) final SortedMap<Double, Vector<String>> preferredTypes = new TreeMap<Double, Vector<String>>( new Comparator<Double>() { @Override public int compare(Double o1, Double o2) { if (o1.doubleValue() > o2.doubleValue()) return -1; else if (o1.doubleValue() < o2.doubleValue()) return 1; else return o1.hashCode() - o2.hashCode(); } }); if (!present(acceptHeader)) { return preferredTypes; } // fill map with q-values and formats final Scanner scanner = new Scanner(acceptHeader.toLowerCase()); scanner.useDelimiter(","); while (scanner.hasNext()) { final String[] types = scanner.next().split(";"); final String type = types[0]; double qValue = 1; if (types.length != 1) { /* * FIXME: we get * java.lang.NumberFormatException: For input string: "screen" * in the error log because the format we assume seems to be * different by some clients. Until we find out, what is really * wrong (our parsing or the client), we are more careful with * parsing external data. */ try { qValue = Double.parseDouble(types[1].split("=")[1]); } catch (NumberFormatException e) { qValue = 0; log.error("Couldn't parse accept header '" + acceptHeader + "'"); } } Vector<String> v = preferredTypes.get(qValue); if (!preferredTypes.containsKey(qValue)) { v = new Vector<String>(); preferredTypes.put(qValue, v); } v.add(type); } return preferredTypes; }
From source file:IO.java
public static int[] readIntVec(File file, int nData) { int[] data = new int[nData]; try {// w w w.j a v a 2 s .c o m Scanner sc = new Scanner(file); sc.useDelimiter(",|\\n"); for (int i = 0; i < nData; i++) { data[i] = sc.nextInt(); } } catch (FileNotFoundException e) { e.printStackTrace(); } return data; }
From source file:com.adyen.httpclient.HttpURLConnectionClient.java
private static String getResponseBody(InputStream responseStream) throws IOException { //\A is the beginning of the stream boundary Scanner scanner = new Scanner(responseStream, CHARSET); String rBody = scanner.useDelimiter("\\A").next(); scanner.close();//from w w w. jav a 2 s. c om responseStream.close(); return rBody; }
From source file:org.wso2.carbon.la.core.utils.LogPatternExtractor.java
public static Map<String, String> processDelimiter(String logLine, String delimiter) { String value = null;/*from w w w. j av a 2s . c o m*/ String delemiterConf = null; Map<String, String> logEvent = new HashMap<>(); switch (delimiter) { case "space": delemiterConf = LAConstants.DELIMITER_SPACE; break; case "comma": delemiterConf = LAConstants.DELIMITER_COMMA; break; case "pipe": delemiterConf = LAConstants.DELIMITER_PIPE; break; case "tab": delemiterConf = LAConstants.DELIMITER_TAB; break; default: delemiterConf = delimiter; break; } // Initialize Scanner object Scanner scan = new Scanner(logLine.trim()); // initialize the string delimiter scan.useDelimiter(delemiterConf.trim()); int fieldIndex = 0; while (scan.hasNext()) { String fieldName = "field" + fieldIndex; logEvent.put(fieldName, scan.next()); fieldIndex++; } // closing the scanner stream scan.close(); return logEvent; }
From source file:IO.java
public static double[][] readDoubleMat(File file, int row, int col) { double[][] data = new double[row][col]; try {// w w w . j av a 2s . co m Scanner sc = new Scanner(file); sc.useDelimiter(",|\\n"); for (int i = 0; i < row; i++) for (int j = 0; j < col; j++) { data[i][j] = Double.parseDouble(sc.next()); } } catch (FileNotFoundException e) { e.printStackTrace(); } return data; }
From source file:IO.java
public static double[] readDoubleVec(File file) { double[] data; try {/*from w ww . j a v a 2 s .c o m*/ int nData = countLines(file.getAbsolutePath()); data = new double[nData]; Scanner sc = new Scanner(file); sc.useDelimiter(",|\\n"); for (int i = 0; i < nData; i++) { data[i] = sc.nextDouble(); } return data; } catch (IOException e) { e.printStackTrace(); data = new double[0]; return data; } }
From source file:com.almende.pi5.lch.SimManager.java
private static String convertStreamToString(InputStream is) { final Scanner s = new Scanner(is, "UTF-8"); s.useDelimiter("\\A"); final String result = s.hasNext() ? s.next() : ""; s.close();//w w w . java 2 s . co m return result; }