List of usage examples for java.io InputStream available
public int available() throws IOException
From source file:net.krotscheck.util.ResourceUtilTest.java
/** * Assert that nonexistent files provide the expected response. * * @throws Exception Should not be thrown. *///w ww. jav a2 s. c o m @Test public void testGetDirectoryResource() throws Exception { String name = "/dir"; InputStream stream = ResourceUtil.getResourceAsStream(name); Assert.assertFalse(stream.available() > 0); String content = ResourceUtil.getResourceAsString(name); Assert.assertEquals("", content); }
From source file:TypeUtil.java
public static byte[] readLine(InputStream in) throws IOException { byte[] buf = new byte[256]; int i = 0;/* w w w. j a v a 2s. c om*/ int loops = 0; int ch = 0; while (true) { ch = in.read(); if (ch < 0) break; loops++; // skip a leading LF's if (loops == 1 && ch == LF) continue; if (ch == CR || ch == LF) break; if (i >= buf.length) { byte[] old_buf = buf; buf = new byte[old_buf.length + 256]; System.arraycopy(old_buf, 0, buf, 0, old_buf.length); } buf[i++] = (byte) ch; } if (ch == -1 && i == 0) return null; // skip a trailing LF if it exists if (ch == CR && in.available() >= 1 && in.markSupported()) { in.mark(1); ch = in.read(); if (ch != LF) in.reset(); } byte[] old_buf = buf; buf = new byte[i]; System.arraycopy(old_buf, 0, buf, 0, i); return buf; }
From source file:com.enioka.jqm.tools.DeliverableTest.java
/** * Retrieve a remote file with authentication, with SSL. *///from ww w .jav a2 s . co m @Test public void testGetOneDeliverableWithAuthWithSsl() throws Exception { Helpers.setSingleParam("disableWsApi", "false", em); Helpers.setSingleParam("enableWsApiAuth", "true", em); Helpers.setSingleParam("enableWsApiSsl", "true", em); JqmClientFactory.resetClient(null); Properties p = new Properties(); p.put("com.enioka.jqm.ws.truststoreFile", "./conf/trusted.jks"); p.put("com.enioka.jqm.ws.truststorePass", "SuperPassword"); JqmClientFactory.setProperties(p); int jobId = JqmSimpleTest.create(em, "pyl.EngineApiSendDeliverable") .addDefParameter("filepath", TestHelpers.node.getDlRepo()) .addDefParameter("fileName", "jqm-test-deliverable4.txt").run(this); File f = new File(TestHelpers.node.getDlRepo() + "jqm-test-deliverable4.txt"); Assert.assertEquals(false, f.exists()); // file should have been moved List<com.enioka.jqm.api.Deliverable> files = JqmClientFactory.getClient().getJobDeliverables(jobId); Assert.assertEquals(1, files.size()); InputStream tmp = JqmClientFactory.getClient().getDeliverableContent(files.get(0)); Assert.assertTrue(tmp.available() > 0); String res = IOUtils.toString(tmp); Assert.assertTrue(res.startsWith("Hello World!")); tmp.close(); }
From source file:no.ntnu.idi.socialhitchhiking.client.RequestTask.java
/** * Sends request and gets response/* w w w . j av a2s . com*/ * @return * @throws ClientProtocolException * @throws IOException */ private InputStream getResponse() throws ClientProtocolException, IOException { int port = Integer.parseInt(con.getResources().getString(R.string.server_port)); HttpHost host = new HttpHost(addr, port); response = httpclient.execute(host, entityRequest, (HttpContext) null); StatusLine statusLine = response.getStatusLine(); if (statusLine.getStatusCode() == HttpStatus.SC_OK) { return response.getEntity().getContent(); } else { //Closes the connection. InputStream stream = response.getEntity().getContent(); StringBuilder sb = new StringBuilder(); while (stream.available() > 0) { sb.append((char) stream.read()); } throw new IOException(statusLine.getReasonPhrase()); } }
From source file:org.envirocar.harvest.TrackPublisher.java
protected String readContent(InputStream content) throws IOException { Scanner sc = new Scanner(content); StringBuilder sb = new StringBuilder(content.available()); while (sc.hasNext()) { sb.append(sc.nextLine());/* ww w . ja v a 2s. com*/ } sc.close(); return sb.toString(); }
From source file:li.klass.fhem.fhem.TelnetConnection.java
private boolean waitForFilledStream(InputStream inputStream, int timeToWait) throws IOException { int initialFill = inputStream.available(); long startTime = System.currentTimeMillis(); while (inputStream.available() == initialFill && (System.currentTimeMillis() - startTime) < timeToWait) { try {// w w w . j a v a 2s . c o m Thread.sleep(100); } catch (InterruptedException e) { LOG.debug("interrupted, ignoring", e); } } return inputStream.available() > 0; }
From source file:fulcrum.xml.Parser.java
/** * Only provides parsing functions to the "fulcrum.xml" package. * /* w w w . j a va 2 s . co m*/ * @see Document * * @param fileLocation * @param document * @throws ParseException */ protected void parse(String fileLocation, Document document) throws ParseException { FileObject fileObj = null; try { fileObj = fsManager.resolveFile(fileLocation); if (fileObj != null && fileObj.exists() && fileObj.isReadable()) { FileContent content = fileObj.getContent(); InputStream is = content.getInputStream(); int size = is.available(); LOGGER.debug("Total File Size: " + size + " bytes"); byte[] buffer = new byte[size]; is.read(buffer, 0, size); LOGGER.debug("Start parsing"); parse(buffer, document); LOGGER.debug("Finished paring"); buffer = null; content.close(); fileObj.close(); } } catch (Exception e) { throw new ParseException(e); } }
From source file:com.example.testwebservice2.TestWebserviceActivity.java
private void putRequest() { HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(URL); try {/*ww w . j a v a2 s . c om*/ byte[] b = beanConvertXml().getBytes("utf-8"); InputStream is = new ByteArrayInputStream(b, 0, b.length); InputStreamEntity re = new InputStreamEntity(is, is.available());//ContentType.create(XmlContentType, WPCharset) re.setContentType(new BasicHeader(HTTP.CONTENT_TYPE, XmlContentType)); httpPost.setEntity(re); HttpResponse response = httpClient.execute(httpPost); if (response.getStatusLine().getStatusCode() == 200) { // getEntity String result = EntityUtils.toString(response.getEntity()); System.out.println("result:" + result); Toast.makeText(TestWebserviceActivity.this, "result:" + response.getStatusLine().getStatusCode() + result, Toast.LENGTH_SHORT).show(); } System.out.println("response " + response.getEntity()); } catch (Exception e) { e.printStackTrace(); } }
From source file:com.vladstirbu.cordova.CDVInstagramVideoPlugin.java
private void share(String videoUrl, String captionString) { if (videoUrl != null && videoUrl.length() > 0) { this.webView.loadUrl("javascript:console.log('processing " + videoUrl + "');"); byte[] videoData = null; try {/* w w w. j av a2s . com*/ URL url = new URL(videoUrl); URLConnection ucon = url.openConnection(); InputStream is = ucon.getInputStream(); videoData = new byte[is.available()]; is.read(videoData); is.close(); } catch (Exception e) { this.webView.loadUrl("javascript:console.log('" + e.getMessage() + "');"); e.printStackTrace(); } File file = null; FileOutputStream os = null; File parentDir = this.webView.getContext().getExternalFilesDir(null); File[] oldVideos = parentDir.listFiles(OLD_IMAGE_FILTER); for (File oldVideo : oldVideos) { oldVideo.delete(); } try { file = File.createTempFile("instagram_video", ".mp4", parentDir); os = new FileOutputStream(file, true); } catch (Exception e) { this.webView.loadUrl("javascript:console.log('" + e.getMessage() + "');"); e.printStackTrace(); } try { os.write(videoData); os.flush(); os.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } Intent shareIntent = new Intent(Intent.ACTION_SEND); shareIntent.setType("video/mp4"); //File media = new File(file); //Uri uri = Uri.fromFile(media); // Add the URI to the Intent. //share.putExtra(Intent.EXTRA_STREAM, uri); // Broadcast the Intent. //startActivity(Intent.createChooser(share, "Share to")); shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("file://" + file)); shareIntent.putExtra(Intent.EXTRA_TEXT, captionString); shareIntent.setPackage("com.instagram.android"); this.cordova.startActivityForResult((CordovaPlugin) this, shareIntent, 12345); } else { this.cbContext.error("Expected one non-empty string argument."); } }
From source file:net.doubledoordev.backend.webserver_old.SimpleWebServer.java
public Response serveFile(File file) { Response res;// w ww . j a va2s .com try { InputStream stream = new FileInputStream(file); int fileLen = stream.available(); res = createResponse(Response.Status.OK, MIME_TYPES.get(FilenameUtils.getExtension(file.getName().toLowerCase())), stream); res.addHeader("Content-Length", "" + fileLen); } catch (IOException ioe) { res = getForbiddenResponse("Reading file failed."); } return res; }