List of usage examples for java.util Scanner close
public void close()
From source file:com.farmerbb.secondscreen.util.U.java
public static String generateBlurb(Activity a, String key, String value, boolean isNotification) { String blurb = " "; if (a instanceof TaskerQuickActionsActivity) { blurb = a.getResources().getStringArray(R.array.pref_notification_action_list)[1]; // If this blurb is being generated for the notification, and the value is "Toggle", // set value to the actual "On" or "Off" state if (isNotification && value.equals("Toggle")) { SharedPreferences prefCurrent = getPrefCurrent(a); switch (key) { case "temp_backlight_off": if (prefCurrent.getBoolean("backlight_off", false)) value = a.getResources().getStringArray(R.array.pref_quick_actions)[0]; else value = a.getResources().getStringArray(R.array.pref_quick_actions)[1]; break; case "temp_chrome": if (prefCurrent.getBoolean("chrome", false)) value = a.getResources().getStringArray(R.array.pref_quick_actions)[1]; else value = a.getResources().getStringArray(R.array.pref_quick_actions)[0]; break; case "temp_immersive": case "temp_immersive_new": if (key.equals("temp_immersive_new")) key = "temp_immersive"; switch (prefCurrent.getString("immersive_new", "fallback")) { case "immersive-mode": value = a.getResources().getStringArray(R.array.pref_quick_actions)[1]; break; default: value = a.getResources().getStringArray(R.array.pref_quick_actions)[0]; break; }/*ww w . j a v a2 s .c o m*/ break; case "temp_overscan": if (prefCurrent.getBoolean("overscan", false)) value = a.getResources().getStringArray(R.array.pref_quick_actions)[1]; else value = a.getResources().getStringArray(R.array.pref_quick_actions)[0]; break; case "temp_vibration_off": if (prefCurrent.getBoolean("vibration_off", false)) value = a.getResources().getStringArray(R.array.pref_quick_actions)[0]; else value = a.getResources().getStringArray(R.array.pref_quick_actions)[1]; break; case "temp_freeform": if (prefCurrent.getBoolean("freeform", false)) value = a.getResources().getStringArray(R.array.pref_quick_actions)[1]; else value = a.getResources().getStringArray(R.array.pref_quick_actions)[0]; break; case "temp_hdmi_rotation": switch (prefCurrent.getString("hdmi_rotation", "landscape")) { case "portrait": value = a.getResources().getStringArray(R.array.pref_hdmi_rotation_list)[1]; break; case "landscape": value = a.getResources().getStringArray(R.array.pref_hdmi_rotation_list)[0]; break; } break; } } // Modifications for non-English locales if (value.equals(a.getResources().getStringArray(R.array.pref_quick_actions_values)[0])) value = a.getResources().getStringArray(R.array.pref_quick_actions)[0]; else if (value.equals(a.getResources().getStringArray(R.array.pref_quick_actions_values)[1])) { if (key.equals("temp_overscan")) value = a.getResources().getStringArray(R.array.pref_quick_actions_overscan)[0]; else value = a.getResources().getStringArray(R.array.pref_quick_actions)[1]; } } switch (key) { case "turn_off": blurb = a.getResources().getString(R.string.quick_turn_off); break; case "lock_device": blurb = a.getResources().getStringArray(R.array.pref_notification_action_list)[2]; break; case "temp_backlight_off": blurb = a.getResources().getString(R.string.quick_backlight) + " " + value; break; case "temp_chrome": blurb = a.getResources().getString(R.string.quick_chrome) + " " + value; break; case "temp_immersive": blurb = a.getResources().getString(R.string.quick_immersive) + " " + value; break; case "temp_immersive_new": switch (value) { case "do-nothing": blurb = a.getResources().getStringArray(R.array.pref_immersive_list_alt)[0]; break; case "status-only": blurb = a.getResources().getStringArray(R.array.pref_immersive_list_alt)[1]; break; case "immersive-mode": blurb = a.getResources().getStringArray(R.array.pref_immersive_list_alt)[2]; break; case "Toggle": blurb = a.getResources().getStringArray(R.array.pref_immersive_list_alt)[3]; break; } break; case "density": case "temp_density": switch (value) { case "reset": blurb = a.getResources().getStringArray(R.array.pref_dpi_list)[0]; break; case "120": blurb = a.getResources().getStringArray(R.array.pref_dpi_list)[1]; break; case "160": blurb = a.getResources().getStringArray(R.array.pref_dpi_list)[2]; break; case "213": blurb = a.getResources().getStringArray(R.array.pref_dpi_list)[3]; break; case "240": blurb = a.getResources().getStringArray(R.array.pref_dpi_list)[4]; break; case "320": blurb = a.getResources().getStringArray(R.array.pref_dpi_list)[6]; break; case "480": blurb = a.getResources().getStringArray(R.array.pref_dpi_list)[8]; break; case "640": blurb = a.getResources().getStringArray(R.array.pref_dpi_list)[10]; break; default: blurb = value + a.getResources().getString(R.string.dpi); break; } break; case "temp_overscan": blurb = a.getResources().getString(R.string.quick_overscan) + " " + value; break; case "size": case "temp_size": SharedPreferences prefMain = getPrefMain(a); if (value.equals("reset")) blurb = a.getResources().getStringArray(R.array.pref_resolution_list)[0]; else if (prefMain.getBoolean("landscape", false)) { switch (value) { case "1920x1080": blurb = a.getResources().getStringArray(R.array.pref_resolution_list)[1]; break; case "1280x720": blurb = a.getResources().getStringArray(R.array.pref_resolution_list)[2]; break; case "854x480": blurb = a.getResources().getStringArray(R.array.pref_resolution_list)[3]; break; default: blurb = value; break; } } else { switch (value) { case "1080x1920": blurb = a.getResources().getStringArray(R.array.pref_resolution_list)[1]; break; case "720x1280": blurb = a.getResources().getStringArray(R.array.pref_resolution_list)[2]; break; case "480x854": blurb = a.getResources().getStringArray(R.array.pref_resolution_list)[3]; break; default: Scanner scanner = new Scanner(value); scanner.useDelimiter("x"); int height = scanner.nextInt(); int width = scanner.nextInt(); scanner.close(); blurb = Integer.toString(width) + "x" + Integer.toString(height); break; } } break; case "temp_rotation_lock_new": switch (value) { case "do-nothing": blurb = a.getResources().getStringArray(R.array.pref_rotation_list)[0]; break; case "auto-rotate": blurb = a.getResources().getStringArray(R.array.pref_rotation_list)[1]; break; case "landscape": blurb = a.getResources().getStringArray(R.array.pref_rotation_list)[2]; break; } break; case "temp_vibration_off": blurb = a.getResources().getString(R.string.quick_vibration) + " " + value; break; case "temp_freeform": blurb = a.getResources().getString(R.string.quick_freeform) + " " + value; break; case "temp_hdmi_rotation": blurb = a.getResources().getString(R.string.quick_hdmi_rotation) + " " + value; break; } return blurb; }
From source file:org.sasabus.export2Freegis.network.SubscriptionManager.java
public void unsubscribe() throws IOException { Scanner sc = new Scanner(new File(UNSUBSCRIPTIONFILE)); String subscriptionstring = ""; while (sc.hasNextLine()) { subscriptionstring += sc.nextLine(); }/*from ww w. j a va2 s . c o m*/ sc.close(); SimpleDateFormat date_date = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat date_time = new SimpleDateFormat("HH:mm:ssZ"); Date d = new Date(); String timestamp = date_date.format(d) + "T" + date_time.format(d); timestamp = timestamp.substring(0, timestamp.length() - 2) + ":" + timestamp.substring(timestamp.length() - 2); subscriptionstring = subscriptionstring.replaceAll(":timestamp", timestamp); String requestString = "http://" + this.address + ":" + this.portnumber_sender + "/TmEvNotificationService/gms/subscription.xml"; HttpPost subrequest = new HttpPost(requestString); StringEntity requestEntity = new StringEntity(subscriptionstring, ContentType.create("text/xml", "ISO-8859-1")); CloseableHttpClient httpClient = HttpClients.createDefault(); subrequest.setEntity(requestEntity); CloseableHttpResponse response = httpClient.execute(subrequest); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { try { System.out.println("Stauts Response: " + response.getStatusLine().getStatusCode()); System.out.println("Status Phrase: " + response.getStatusLine().getReasonPhrase()); HttpEntity responseEntity = response.getEntity(); if (responseEntity != null) { String responsebody = EntityUtils.toString(responseEntity); System.out.println(responsebody); } } finally { response.close(); httpClient.close(); } } }
From source file:hu.sztaki.incremental.ml.streaming.imsr.MatrixVectorPairSource.java
private void readMatricesSideBySide(Scanner scanner, RealMatrix... matrices) { for (int i = 0; i < matrices[0].getRowDimension(); i++) { if (!scanner.hasNextLine()) { return; //there will be some 0 rows }/* ww w. ja va 2 s . c o m*/ String line = scanner.nextLine(); Scanner lineScanner = initCsvScanner(new Scanner(line)); for (RealMatrix m : matrices) { for (int j = 0; j < m.getColumnDimension(); j++) { double d = lineScanner.nextDouble(); m.setEntry(i, j, d); } } lineScanner.close(); } }
From source file:com.git.ifly6.javatelegram.util.JInfoFetcher.java
/** Queries the NationStates API for a listing of all World Assembly delegates. * * @return <code>String[]</code> with the recipients inside * @throws IOException in case there is a problem with connecting to the NS API */ public List<String> getDelegates() throws JTelegramException { // If necessary, fetch the Delegates list. if (delegates == null) { try {/*from ww w . j a va 2 s.c o m*/ List<String> delegates = new ArrayList<String>(0); URL website = new URL("http://www.nationstates.net/cgi-bin/api.cgi?wa=1&q=delegates"); String xml_raw = ""; // Read the list of Delegates from the file we just got. @SuppressWarnings("resource") Scanner xml_scan = new Scanner(website.openConnection().getInputStream()).useDelimiter("\\A"); xml_raw = xml_scan.next(); xml_scan.close(); // Remove the XML tags from that list of Delegates. xml_raw = xml_raw.replaceFirst("<WA council=\"1\"><DELEGATES>", ""); xml_raw = xml_raw.replaceFirst("</DELEGATES></WA>", ""); // Split that list into a String[] and remove all new lines. for (String each : xml_raw.split(",")) { if (!StringUtils.isEmpty(each)) { delegates.add(each); } } this.delegates = delegates; } catch (IOException e) { throw new JTelegramException("Failed to get list of delegates."); } } // Return the new or saved Delegates list. return delegates; }
From source file:com.github.matthesrieke.simplebroker.SimpleBrokerServlet.java
protected String readContent(HttpServletRequest req) throws IOException { String enc = req.getCharacterEncoding(); Scanner sc = new Scanner(req.getInputStream(), enc == null ? "utf-8" : enc); StringBuilder sb = new StringBuilder(); while (sc.hasNext()) { sb.append(sc.nextLine());/*w ww . j a va2 s . c om*/ } sc.close(); return sb.toString(); }
From source file:BlockingQueueTest.java
/** * Searches a file for a given keyword and prints all matching lines. * @param file the file to search/* w ww.java 2 s . c om*/ */ public void search(File file) throws IOException { Scanner in = new Scanner(new FileInputStream(file)); int lineNumber = 0; while (in.hasNextLine()) { lineNumber++; String line = in.nextLine(); if (line.contains(keyword)) System.out.printf("%s:%d:%s%n", file.getPath(), lineNumber, line); } in.close(); }
From source file:org.springframework.cloud.lattice.connector.AbstractServiceInfoCreatorTests.java
protected String readTestDataFile(String fileName) { Scanner scanner = null; try {/*from w w w.j a v a2s . c o m*/ Reader fileReader = new InputStreamReader(getClass().getResourceAsStream(fileName)); scanner = new Scanner(fileReader); return scanner.useDelimiter("\\Z").next(); } finally { if (scanner != null) { scanner.close(); } } }
From source file:gtu._work.ui.RegexTestUI.java
private void jText1OrJArea1Change(DocumentEvent doc) { try {// ww w .j a v a2s . co m String complie1 = regexText.getText(); String complie2 = regexText0.getText(); String complie = complie1; if (StringUtils.isBlank(complie1)) { complie = complie2; } String matcherStr = srcArea.getText(); if (StringUtils.isBlank(complie)) { setTitle("Regex"); return; } if (StringUtils.isBlank(matcherStr)) { setTitle("content"); return; } Pattern pattern = Pattern.compile(complie); Matcher matcher = pattern.matcher(matcherStr); DefaultComboBoxModel model1 = new DefaultComboBoxModel(); groupList.setModel(model1); while (matcher.find()) { model1.addElement("---------------------"); for (int ii = 0; ii <= matcher.groupCount(); ii++) { model1.addElement(ii + " : [" + matcher.group(ii) + "]"); } } DefaultComboBoxModel model2 = new DefaultComboBoxModel(); scannerList.setModel(model2); Scanner scanner = new Scanner(matcherStr); scanner.useDelimiter(pattern); while (scanner.hasNext()) { model2.addElement("[" + scanner.next() + "]"); } scanner.close(); this.setTitle("?"); } catch (Exception ex) { this.setTitle(ex.getMessage()); ex.printStackTrace(); } }
From source file:org.sasabus.export2Freegis.network.SubscriptionManager.java
public boolean subscribe() throws IOException { for (int i = 0; i < SUBFILEARRAY.length; ++i) { Scanner sc = new Scanner(new File(SUBFILEARRAY[i])); String subscriptionstring = ""; while (sc.hasNextLine()) { subscriptionstring += sc.nextLine(); }//from w w w . java 2s. c om sc.close(); SimpleDateFormat date_date = new SimpleDateFormat("yyyy-MM-dd"); SimpleDateFormat date_time = new SimpleDateFormat("HH:mm:ssZ"); Date d = new Date(); String timestamp = date_date.format(d) + "T" + date_time.format(d); timestamp = timestamp.substring(0, timestamp.length() - 2) + ":" + timestamp.substring(timestamp.length() - 2); Calendar c = Calendar.getInstance(); c.setTime(d); c.add(Calendar.DATE, 1); d = c.getTime(); String valid_until = date_date.format(d) + "T" + date_time.format(d); valid_until = valid_until.substring(0, valid_until.length() - 2) + ":" + valid_until.substring(valid_until.length() - 2); subscriptionstring = subscriptionstring.replaceAll(":timestamp_valid", valid_until); subscriptionstring = subscriptionstring.replaceAll(":timestamp", timestamp); String requestString = "http://" + this.address + ":" + this.portnumber_sender + "/TmEvNotificationService/gms/subscription.xml"; HttpPost subrequest = new HttpPost(requestString); StringEntity requestEntity = new StringEntity(subscriptionstring, ContentType.create("text/xml", "ISO-8859-1")); CloseableHttpClient httpClient = HttpClients.createDefault(); subrequest.setEntity(requestEntity); CloseableHttpResponse response = httpClient.execute(subrequest); if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) { try { System.out.println("Stauts Response: " + response.getStatusLine().getStatusCode()); System.out.println("Status Phrase: " + response.getStatusLine().getReasonPhrase()); HttpEntity responseEntity = response.getEntity(); if (responseEntity != null) { String responsebody = EntityUtils.toString(responseEntity); System.out.println(responsebody); } } finally { response.close(); httpClient.close(); } return false; } System.out.println("Subscription of " + SUBFILEARRAY[i]); } return true; }
From source file:it.acubelab.batframework.systemPlugins.TagmeAnnotator.java
@Override public HashSet<ScoredAnnotation> solveSa2W(String text) throws AnnotationException { // System.out.println(text.length()+ " "+text.substring(0, // Math.min(text.length(), 15))); // TODO: workaround for a bug in tagme. should be deleted afterwards. String newText = ""; for (int i = 0; i < text.length(); i++) newText += (text.charAt(i) > 127 ? ' ' : text.charAt(i)); text = newText;//from w w w . j av a 2s . c o m // avoid crashes for empty documents int j = 0; while (j < text.length() && text.charAt(j) == ' ') j++; if (j == text.length()) return new HashSet<ScoredAnnotation>(); HashSet<ScoredAnnotation> res; String params = null; try { res = new HashSet<ScoredAnnotation>(); params = "key=" + this.key; params += "&lang=en"; if (epsilon >= 0) params += "&epsilon=" + epsilon; if (minComm >= 0) params += "&min_comm=" + minComm; if (minLink >= 0) params += "&min_link=" + minLink; params += "&text=" + URLEncoder.encode(text, "UTF-8"); URL wikiApi = new URL(url); HttpURLConnection slConnection = (HttpURLConnection) wikiApi.openConnection(); slConnection.setRequestProperty("accept", "text/xml"); slConnection.setDoOutput(true); slConnection.setDoInput(true); slConnection.setRequestMethod("POST"); slConnection.setRequestProperty("charset", "utf-8"); slConnection.setRequestProperty("Content-Length", Integer.toString(params.getBytes().length)); slConnection.setUseCaches(false); slConnection.setReadTimeout(0); DataOutputStream wr = new DataOutputStream(slConnection.getOutputStream()); wr.writeBytes(params); wr.flush(); wr.close(); Scanner s = new Scanner(slConnection.getInputStream()); Scanner s2 = s.useDelimiter("\\A"); String resultStr = s2.hasNext() ? s2.next() : ""; s.close(); JSONObject obj = (JSONObject) JSONValue.parse(resultStr); lastTime = (Long) obj.get("time"); JSONArray jsAnnotations = (JSONArray) obj.get("annotations"); for (Object js_ann_obj : jsAnnotations) { JSONObject js_ann = (JSONObject) js_ann_obj; System.out.println(js_ann); int start = ((Long) js_ann.get("start")).intValue(); int end = ((Long) js_ann.get("end")).intValue(); int id = ((Long) js_ann.get("id")).intValue(); float rho = Float.parseFloat((String) js_ann.get("rho")); System.out.println(text.substring(start, end) + "->" + id + " (" + rho + ")"); res.add(new ScoredAnnotation(start, end - start, id, (float) rho)); } } catch (Exception e) { e.printStackTrace(); throw new AnnotationException("An error occurred while querying TagMe API. Message: " + e.getMessage() + " Parameters:" + params); } return res; }