List of usage examples for java.util Scanner close
public void close()
From source file:com.viddu.handlebars.HandlebarsNashorn.java
@Override public String render(InputStream templateStream, String contextJson) throws HandlebarsException { Scanner scanner = new Scanner(templateStream, StandardCharsets.UTF_8.name()); String template = scanner.useDelimiter("\\A").next(); scanner.close(); return render(template, contextJson); }
From source file:com.viddu.handlebars.HandlebarsNashorn.java
@Override public String render(InputStream templateStream, Map<String, Object> context) throws HandlebarsException { Scanner scanner = new Scanner(templateStream, StandardCharsets.UTF_8.name()); String template = scanner.useDelimiter("\\A").next(); scanner.close(); return render(template, context); }
From source file:com.viddu.handlebars.HandlebarsNashorn.java
@Override public String render(InputStream templateStream, Collection<?> context) throws HandlebarsException { Scanner scanner = new Scanner(templateStream, StandardCharsets.UTF_8.name()); String template = scanner.useDelimiter("\\A").next(); scanner.close(); return render(template, context); }
From source file:com.viddu.handlebars.HandlebarsNashorn.java
@Override public String render(InputStream templateStream, Object context) throws HandlebarsException { Scanner scanner = new Scanner(templateStream, StandardCharsets.UTF_8.name()); String template = scanner.useDelimiter("\\A").next(); scanner.close(); return render(template, context); }
From source file:net.dv8tion.jda.player.Playlist.java
public static Playlist getPlaylist(String url) { List<String> infoArgs = new LinkedList<>(); infoArgs.addAll(YOUTUBE_DL_PLAYLIST_ARGS); infoArgs.add("--"); //Url separator. Deals with YT ids that start with -- infoArgs.add(url);//from w w w . ja va 2 s . com //Fire up Youtube-dl and get all sources from the provided url. List<AudioSource> sources = new ArrayList<>(); Scanner scan = null; try { Process infoProcess = new ProcessBuilder().command(infoArgs).start(); byte[] infoData = IOUtils.readFully(infoProcess.getInputStream(), -1, false); if (infoData == null || infoData.length == 0) throw new NullPointerException( "The YT-DL playlist process resulted in a null or zero-length INFO!"); String sInfo = new String(infoData); scan = new Scanner(sInfo); JSONObject source = new JSONObject(scan.nextLine()); if (source.has("_type"))//Is a playlist { sources.add(new RemoteSource(source.getString("url"))); while (scan.hasNextLine()) { source = new JSONObject(scan.nextLine()); sources.add(new RemoteSource(source.getString("url"))); } } else //Single source link { sources.add(new RemoteSource(source.getString("webpage_url"))); } } catch (IOException e) { e.printStackTrace(); } finally { if (scan != null) scan.close(); } //Now that we have all the sources we can create our Playlist object. Playlist playlist = new Playlist("New Playlist"); playlist.sources = sources; return playlist; }
From source file:org.sasabus.export2Freegis.network.DataRequestManager.java
public String datarequest() throws IOException { Scanner sc = new Scanner(new File(DATAREQUEST)); String subscriptionstring = ""; while (sc.hasNextLine()) { subscriptionstring += sc.nextLine(); }/* w w w.j a v a2 s .com*/ 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/polldata.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); //System.out.println("Stauts Response: " + response.getStatusLine().getStatusCode()); //System.out.println("Status Phrase: " + response.getStatusLine().getReasonPhrase()); HttpEntity responseEntity = response.getEntity(); String responsebody = ""; if (responseEntity != null) { responsebody = EntityUtils.toString(responseEntity); } return responsebody; }
From source file:csns.importer.parser.csula.StudentsParserImpl.java
/** * This parser handles data copy&pasted from an Excel file produced from GET * data. The format is expected to be <quarter cin first_name last_name ...> * where quarter is a 4-digit code. Currently we only process the first four * fields.// www .ja v a2s.co m */ @Override public List<ImportedUser> parse(String text) { List<ImportedUser> students = new ArrayList<ImportedUser>(); Scanner scanner = new Scanner(text); while (scanner.hasNextLine()) { ImportedUser student = parseLine(scanner.nextLine()); if (student != null) students.add(student); } scanner.close(); return students; }
From source file:games.livestreams.providers.Twitch.java
@Override public String[] getStreams(String tag) { final ArrayList<String> streams = new ArrayList<String>(); String apiUrl = String.format(ExtensionObject.Configuration.get("twitch.link"), Integer.parseInt(ExtensionObject.Configuration.get("streams.limit")), tag); try {// ww w.j av a 2 s. co m // apiUrl = URLEncoder.encode(apiUrl, "UTF-8"); URL url = new URL(apiUrl); Scanner scan = new Scanner(url.openStream()); String jsonAnswer = ""; while (scan.hasNext()) jsonAnswer += scan.nextLine(); scan.close(); JSONObject jsonObject = new JSONObject(jsonAnswer); JSONArray jsonArray = jsonObject.getJSONArray("streams"); for (int i = 0; i < jsonArray.length(); i++) { JSONObject streamObject = jsonArray.getJSONObject(i); String streamName = streamObject.getJSONObject("channel").getString("display_name"); String streamStatus = "online"; String streamUrl = streamObject.getJSONObject("channel").getString("url"); String streamViewers = streamObject.getString("viewers"); streamName = IrcMessageTextModifier.makeBold(streamName); streamViewers = IrcMessageTextModifier.makeColoured(IrcMessageTextModifier.makeBold(streamViewers), IrcTextColor.Brown); String realStatus = streamObject.getJSONObject("channel").getString("status"); if (realStatus != null && !realStatus.trim().isEmpty()) { streamStatus = realStatus; } String formattedStreamInfoOutput = String.format("[%s] (%s) %s (%s viewers)", streamName, streamStatus, streamUrl, streamViewers); streams.add(formattedStreamInfoOutput); } } catch (Exception e) { System.out.println(e.getMessage()); } if (!streams.isEmpty()) { String[] streamsArray = new String[] {}; return streams.toArray(streamsArray); } else { throw new ProviderError(String.format("No streams found on \"%s\" service with tag \"%s\"", this.getProviderName(), tag)); } }
From source file:org.terracotta.management.registry.ManagementRegistryTest.java
@Test public void test_management_registry_exposes() throws IOException { ManagementRegistry registry = new AbstractManagementRegistry() { @Override//w w w .j av a 2 s. c om public ContextContainer getContextContainer() { return new ContextContainer("cacheManagerName", "my-cm-name"); } }; registry.addManagementProvider(new MyManagementProvider()); registry.register(new MyObject("myCacheManagerName", "myCacheName1")); registry.register(new MyObject("myCacheManagerName", "myCacheName2")); Scanner scanner = new Scanner(ManagementRegistryTest.class.getResourceAsStream("/capabilities.json"), "UTF-8").useDelimiter("\\A"); String expectedJson = scanner.next(); scanner.close(); ObjectMapper mapper = new ObjectMapper(); mapper.configure(SerializationFeature.INDENT_OUTPUT, true); mapper.addMixIn(CapabilityContext.class, CapabilityContextMixin.class); String actual = mapper.writeValueAsString(registry.getCapabilities()); System.out.println(expectedJson); System.out.println(actual); assertEquals(expectedJson, actual); ContextualReturn<?> cr = registry.withCapability("TheActionProvider") .call("incr", int.class, new Parameter(Integer.MAX_VALUE, "int")).on(Context.empty() .with("cacheManagerName", "myCacheManagerName").with("cacheName", "myCacheName1")) .build().execute().getSingleResult(); try { cr.getValue(); fail(); } catch (ExecutionException e) { assertEquals(IllegalArgumentException.class, e.getCause().getClass()); } }
From source file:se.vgregion.portal.cs.util.CryptoUtilImpl.java
private byte[] readKeyFile() { Scanner scanner = null; try {// w ww .j av a 2 s . c om scanner = new Scanner(keyFile).useDelimiter("\\Z"); String keyValue = scanner.next(); scanner.close(); return hexStringToByteArray(keyValue); } catch (FileNotFoundException e) { e.printStackTrace(); return new byte[0]; } }