List of usage examples for java.nio.channels Channels newChannel
public static WritableByteChannel newChannel(OutputStream out)
From source file:Network.CSMSHandler.java
@Override public void update(Observable o, Object arg) { Socket objSocket = (Socket) arg; int intSeq = -1; try {/*from w w w .j a v a 2 s. c o m*/ JSONParser jsonParser = new JSONParser(); JSONObject objJSON = (JSONObject) jsonParser.parse(new InputStreamReader(objSocket.getInputStream())); int intTempSeq = Integer.parseInt(objJSON.get("id").toString()); for (CSMS objSMS : CSMSManager.loadFromJson((JSONArray) objJSON.get("sms"))) { CSMSFactory.getSMSSender().sendSMS(objSMS); } intSeq = intTempSeq; } catch (Exception ex) { System.out.println(ex); } finally { try (Writer objWriter = Channels.newWriter(Channels.newChannel(objSocket.getOutputStream()), StandardCharsets.US_ASCII.name())) { objWriter.write(intSeq + ""); objWriter.flush(); } catch (IOException ex) { System.out.println(ex); } } }
From source file:Network.CEmailHandler.java
@Override public void update(Observable o, Object arg) { Socket objSocket = (Socket) arg; int intSeq = -1; try {// w w w. j a v a 2 s . c om JSONParser jsonParser = new JSONParser(); JSONObject objJSON = (JSONObject) jsonParser.parse(new InputStreamReader(objSocket.getInputStream())); int intTempSeq = Integer.parseInt(objJSON.get("id").toString()); emailSender.getEmailsToBeSent((JSONArray) objJSON.get("messagesToBeSent")); emailSender.dispatchEmail(); intSeq = intTempSeq; } catch (IOException | ParseException | NumberFormatException ex) { System.out.println(ex); } finally { try (Writer objWriter = Channels.newWriter(Channels.newChannel(objSocket.getOutputStream()), StandardCharsets.US_ASCII.name())) { objWriter.write(intSeq + ""); objWriter.flush(); } catch (IOException ex) { System.out.println(ex); } } }
From source file:com.intuit.tank.service.impl.v1.report.FileReader.java
/** * Gets a StreamingOutput from the passedin file from start to end or from beginning to end if start is greater than * end. if a negative number is passed, it will get the last n lines of the file. If 0 is passed it will return the * entire file./*from w w w. j av a 2 s .c o m*/ * * @param total * * @return a StreamingOutput */ public static StreamingOutput getFileStreamingOutput(final File f, long total, String start) { long l = 0; if (start != null) { try { l = Long.parseLong(start); // num lines to get from end if (l < 0) { l = getStartChar(f, Math.abs(l), total); } } catch (Exception e) { LOG.error("Error parsing start " + start + ": " + e); } } final long to = l > total ? 0 : total; final long from = l; StreamingOutput streamer = new StreamingOutput() { @SuppressWarnings("resource") @Override public void write(final OutputStream output) throws IOException, WebApplicationException { final FileChannel inputChannel = new FileInputStream(f).getChannel(); final WritableByteChannel outputChannel = Channels.newChannel(output); try { inputChannel.transferTo(from, to, outputChannel); } finally { // closing the channels inputChannel.close(); outputChannel.close(); } } }; LOG.debug("returning data from " + from + " - " + to + " of total " + total); return streamer; }
From source file:com.kurento.kmf.test.services.Recorder.java
public static float getPesqMos(String audio, int sampleRate) { float pesqmos = 0; try {/* ww w . jav a 2s. c om*/ String pesq = KurentoServicesTestHelper.getTestFilesPath() + "/bin/pesq/PESQ"; String origWav = ""; if (audio.startsWith(HTTP_TEST_FILES)) { origWav = KurentoServicesTestHelper.getTestFilesPath() + audio.replace(HTTP_TEST_FILES, ""); } else { // Download URL origWav = KurentoMediaServerManager.getWorkspace() + "/downloaded.wav"; URL url = new URL(audio); ReadableByteChannel rbc = Channels.newChannel(url.openStream()); FileOutputStream fos = new FileOutputStream(origWav); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); fos.close(); } Shell.runAndWait(pesq, "+" + sampleRate, origWav, RECORDED_WAV); List<String> lines = FileUtils.readLines(new File(PESQ_RESULTS), "utf-8"); pesqmos = Float.parseFloat(lines.get(1).split("\t")[2].trim()); log.info("PESQMOS " + pesqmos); Shell.runAndWait("rm", PESQ_RESULTS); } catch (IOException e) { log.error("Exception recording local audio", e); } return pesqmos; }
From source file:org.alfresco.services.solr.GetTextContentResponse.java
public GetTextContentResponse(Response response) throws IOException { super(response); InputStream in = response.getContentAsStream(); this.channel = Channels.newChannel(in); this.transformStatusStr = response.getHeader("X-Alfresco-transformStatus"); this.transformException = response.getHeader("X-Alfresco-transformException"); String tmp = response.getHeader("X-Alfresco-transformDuration"); this.transformDuration = (tmp != null && !tmp.equals("") ? Long.valueOf(tmp) : null); setStatus();/*from w ww . j a v a 2 s . c o m*/ }
From source file:net.formio.upload.RequestUploadedFile.java
@Override public ReadableByteChannel getContent() throws IOException { assertNotCleared(); return Channels.newChannel(fileItem.getInputStream()); }
From source file:org.phenotips.textanalysis.internal.BiolarkFileUtils.java
/** * Downloads file located at given url and saves it with given filename in the current directory. * * @param fileName Name under which the file will be saved locally * @param url URL of hte file/*from w ww . j a v a 2s . co m*/ * @return File which was downloaded * @throws IOException if downloading fails */ public static File downloadFile(String fileName, String url) throws IOException { final URL resourcesURL = new URL(url); final ReadableByteChannel rbc = Channels.newChannel(resourcesURL.openStream()); FileOutputStream fos = new FileOutputStream(fileName); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); fos.close(); return new File(fileName); }
From source file:math.tools.Browse.java
/** * Creates new form jsontable/*from ww w .j a v a 2s.c o m*/ */ public Browse() { BufferedReader in; FileOutputStream fos; new File(System.getProperty("user.home") + "//MathHelper//tools//").mkdirs(); try { URL website = new URL("https://dl.dropboxusercontent.com/u/67622419/mathTools/toolInfo.json"); ReadableByteChannel rbc = Channels.newChannel(website.openStream()); fos = new FileOutputStream(System.getProperty("user.home") + "//MathHelper//toolInfo.json"); fos.getChannel().transferFrom(rbc, 0, Long.MAX_VALUE); } catch (IOException ex) { Logger.getLogger(Browse.class.getName()).log(Level.SEVERE, null, ex); } try { in = new BufferedReader( new FileReader(System.getProperty("user.home") + "//MathHelper//toolInfo.json")); String inputLine; int i = -1; JSONObject json; while ((inputLine = in.readLine()) != null) { i++; json = (JSONObject) new JSONParser().parse(inputLine); table[i][0] = json.get("name"); table[i][1] = json.get("author"); table[i][2] = json.get("rating"); File f = new File( System.getProperty("user.home") + "//MathHelper//tools//" + json.get("name") + ".jar"); if (!f.exists()) { table[i][3] = "Not Downloaded Yet"; } else { table[i][3] = "Downloaded"; } table[i][4] = json.get("adress"); } in.close(); } catch (IOException | ParseException ex) { Logger.getLogger(Browse.class.getName()).log(Level.SEVERE, null, ex); } initComponents(); }
From source file:com.evrythng.java.wrapper.util.FileUtils.java
/** * Uploads file with public read access. * * @param url upload url./*from www . j ava 2 s .c o m*/ * @param contentType content type. * @param file file for upload. */ public static void uploadPublicFile(final URL url, final String contentType, final File file) throws IOException { HttpURLConnection connection = getConnectionForPublicUpload(url, contentType); try (OutputStream output = connection.getOutputStream(); WritableByteChannel wbc = Channels.newChannel(output); FileInputStream fis = new FileInputStream(file); FileChannel fc = fis.getChannel()) { fc.transferTo(0, fc.size(), wbc); } validateConnectionAfterUpload(connection); }
From source file:org.alfresco.MockContentGetter.java
public MockContentGetter addTestContent(long nodeInternalId, String nodeId, Long nodeVersion, String nodePath, String nodeContent, String mimeType) { long size = nodeContent.getBytes().length; ByteArrayInputStream in = new ByteArrayInputStream(nodeContent.getBytes()); ReadableByteChannel channel = Channels.newChannel(in); Node node = Node.build().nodeId(nodeId).nodeInternalId(nodeInternalId).nodeVersion(nodeVersion); Content content = new Content(channel, size); testContentByNodeId.put(node, content); testContentByNodeInternalId.put(nodeInternalId, content); testContentByNodePath.put(nodePath, content); return this; }