List of usage examples for java.io DataOutputStream DataOutputStream
public DataOutputStream(OutputStream out)
From source file:com.mopaas_mobile.http.BaseHttpRequester.java
public static String doPOST(String urlstr, List<BasicNameValuePair> params) throws IOException { String result = null;//from www .j a va2 s .co m URL url = new URL(urlstr); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setDoInput(true); connection.setRequestMethod("POST"); connection.setUseCaches(false); connection.setInstanceFollowRedirects(false); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // connection.setRequestProperty("token",token); connection.setConnectTimeout(30000); connection.setReadTimeout(30000); connection.connect(); DataOutputStream out = new DataOutputStream(connection.getOutputStream()); String content = ""; if (params != null && params.size() > 0) { for (int i = 0; i < params.size(); i++) { content = content + "&" + URLEncoder.encode(((NameValuePair) params.get(i)).getName(), "UTF-8") + "=" + URLEncoder.encode(((NameValuePair) params.get(i)).getValue(), "UTF-8"); } out.writeBytes(content.substring(1)); } out.flush(); out.close(); InputStream is = connection.getInputStream(); BufferedReader br = new BufferedReader(new InputStreamReader(is, "UTF-8")); StringBuffer b = new StringBuffer(); int ch; while ((ch = br.read()) != -1) { b.append((char) ch); } result = b.toString().trim(); connection.disconnect(); return result; }
From source file:TimeComputation.java
public void run() throws IOException { DataOutputStream n = new DataOutputStream(new BufferedOutputStream(new FileOutputStream("java2s"))); long t0, t1;/* ww w . j a v a 2s .c o m*/ System.out.println("Java Starts at " + (t0 = System.currentTimeMillis())); double k; for (int i = 0; i < 100000; i++) { k = 2.1 * Math.sqrt((double) i); n.writeDouble(k); } System.out.println("Java Ends at " + (t1 = System.currentTimeMillis())); double deltaT = t1 - t0; System.out.println("This run took " + DecimalFormat.getInstance().format(deltaT / 1000.) + " seconds."); }
From source file:com.zf.util.Post_NetNew.java
/** * ?//from w ww. ja v a2s .com * * @param pams * @param ip * @param port * @return * @throws Exception */ public static String pn(Map<String, String> pams, String ip, int port) throws Exception { if (null == pams) { return ""; } InetSocketAddress addr = new InetSocketAddress(ip, port); Proxy proxy = new Proxy(Type.HTTP, addr); String strtmp = "url"; URL url = new URL(pams.get(strtmp)); pams.remove(strtmp); strtmp = "body"; String body = pams.get(strtmp); pams.remove(strtmp); strtmp = "POST"; if (StringUtils.isEmpty(body)) strtmp = "GET"; HttpURLConnection httpConn = (HttpURLConnection) url.openConnection(proxy); httpConn.setConnectTimeout(30000); httpConn.setReadTimeout(30000); httpConn.setUseCaches(false); httpConn.setRequestMethod(strtmp); for (String pam : pams.keySet()) { httpConn.setRequestProperty(pam, pams.get(pam)); } if ("POST".equals(strtmp)) { httpConn.setDoOutput(true); httpConn.setDoInput(true); DataOutputStream dos = new DataOutputStream(httpConn.getOutputStream()); dos.writeBytes(body); dos.flush(); } int resultCode = httpConn.getResponseCode(); StringBuilder sb = new StringBuilder(); sb.append(resultCode).append("\n"); String readLine; InputStream stream; try { stream = httpConn.getInputStream(); } catch (Exception ignored) { stream = httpConn.getErrorStream(); } try { BufferedReader responseReader = new BufferedReader(new InputStreamReader(stream, "UTF-8")); while ((readLine = responseReader.readLine()) != null) { sb.append(readLine).append("\n"); } } catch (Exception ignored) { } return sb.toString(); }
From source file:net.sourceforge.fenixedu.presentationTier.Action.publico.FileDownload.java
@Override public ActionForward execute(final ActionMapping mapping, final ActionForm actionForm, final HttpServletRequest request, final HttpServletResponse response) throws Exception { final String oid = request.getParameter("oid"); final File file = FenixFramework.getDomainObject(oid); if (file == null) { response.setStatus(HttpServletResponse.SC_BAD_REQUEST); response.getWriter().write(HttpStatus.getStatusText(HttpStatus.SC_BAD_REQUEST)); response.getWriter().close();/*from w w w .ja v a 2 s . c om*/ } else { final Person person = AccessControl.getPerson(); if (!file.isPrivate() || file.isPersonAllowedToAccess(person)) { response.setContentType(file.getContentType()); response.addHeader("Content-Disposition", "attachment; filename=" + file.getFilename()); response.setContentLength(file.getSize().intValue()); final DataOutputStream dos = new DataOutputStream(response.getOutputStream()); dos.write(file.getContents()); dos.close(); } else if (file.isPrivate() && person == null) { response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); response.getWriter().write(HttpStatus.getStatusText(HttpStatus.SC_UNAUTHORIZED)); response.getWriter().close(); } else { response.setStatus(HttpServletResponse.SC_FORBIDDEN); response.getWriter().write(HttpStatus.getStatusText(HttpStatus.SC_FORBIDDEN)); response.getWriter().close(); } } return null; }
From source file:com.rabbitmq.client.impl.SocketFrameHandler.java
/** * @param socket the socket to use//from w w w. j av a 2s. c o m */ public SocketFrameHandler(Socket socket) throws IOException { _socket = socket; _inputStream = new DataInputStream(new BufferedInputStream(socket.getInputStream())); _outputStream = new DataOutputStream(new BufferedOutputStream(socket.getOutputStream())); this.socketFrameHandler = this; }
From source file:ja.lingo.engine.searchindex.NodeSerializer.java
public void close() throws IOException { tempDos.close();//from ww w . j av a 2 s . c o m LOG.info("calculateSize(...) spent " + timeMeasurer); long size = Files.length(tempFileName); if (size > Integer.MAX_VALUE) { States.doThrow("File is \"" + fileName + "\"to large. Maximum allowed size is " + Integer.MAX_VALUE + ". Actual size is " + size); } FileOutputStream fos = new FileOutputStream(fileName); DataOutputStream dos = new DataOutputStream(fos); serializeNodeHeader(dos, (int) size, firstLevelNodesCount, '\u0000', 0); dos.flush(); EngineFiles.appendFileAndDelete(tempFileName, fos); fos.close(); }
From source file:com.acc.util.InstagramClient.java
public String getProfilePicOfUser(final String code) { String profilePic = null;/*from ww w.j a v a2 s .co m*/ // final String inputJson = "client_id=e580d04d0687403189f86d49545b69a4&client_secret=a2943297f601402d894f8d21400bdfd5&grant_type=authorization_code&redirect_uri=http://electronics.local:9001/yacceleratorstorefront/electronics/en/facerecognitionpage&code=" // + code; //String inputJson1 = "{\"Identities\": [{\"Score\": \"99.99986\",\"IdentityDetails\": {\"BiographicData\": [{\"Key\": \"Name\",\"Value\": \"Ravi\"},{\"Key\": \"ID\",\"Value\": \"DRIVING_LICENSE_ID\"}],\"BiometricDatas\": [{\"Base64Data\": \"/9j/4AAQSkZJ\",\"Modality\": \"Face_Face2D\",\"Type\": \"Image\"}],\"IdentityId\": \"BiometricDev[1:S:58840];\"}}],\"InWatchList\": false}"; // System.out.println(inputJson); // Make Web Service Call // final Client client = ClientBuilder.newClient(); // final String instagramOutputJson = client.target("https://api.instagram.com/oauth/access_token").request() // .method("POST", Entity.entity(inputJson, MediaType.APPLICATION_JSON), String.class); try { final URL url = new URL("https://api.instagram.com/oauth/access_token"); final HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setRequestMethod("POST"); final String urlParameters = "client_id=e580d04d0687403189f86d49545b69a4" + "&client_secret=a2943297f601402d894f8d21400bdfd5" + "&grant_type=authorization_code" + "&redirect_uri=http://electronics.local:9001/yacceleratorstorefront/electronics/en/facerecognitionpage" + "&code=" + code; connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("charset", "utf-8"); connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParameters.getBytes().length)); final DataOutputStream wr = new DataOutputStream(connection.getOutputStream()); wr.writeBytes(urlParameters); wr.flush(); wr.close(); final BufferedReader rd = new BufferedReader(new InputStreamReader(connection.getInputStream())); final StringBuffer result = new StringBuffer(); String line = ""; while ((line = rd.readLine()) != null) { result.append(line); } System.out.println(result.toString()); System.out.println("Output Json from Instagram is " + result); profilePic = processJson(result.toString()); } catch (final MalformedURLException e) { // YTODO Auto-generated catch block e.printStackTrace(); } catch (final IOException e) { // YTODO Auto-generated catch block e.printStackTrace(); } return profilePic; }
From source file:eu.stratosphere.pact.runtime.task.util.SerializationCopier.java
/** * Creates a SerializationCopier with an byte array of specified size. * /*from w w w.ja va2 s.c o m*/ * @param initialCopySize * Initial size of the byte array that stores the serialized copy. */ public SerializationCopier(int initialCopySize) { serializedCopy = new byte[initialCopySize]; baos = new ByteArrayOutputStream(); dos = new DataOutputStream(baos); bais = new ByteArrayInputStream(serializedCopy); dis = new DataInputStream(bais); }
From source file:com.lightboxtechnologies.spectrum.FsEntryHBaseInputFormat.java
static String convertScanToString(Scan scan) throws IOException { final ByteArrayOutputStream out = new ByteArrayOutputStream(); DataOutputStream dos = null;/*from w ww . j a v a 2 s . co m*/ try { dos = new DataOutputStream(out); scan.write(dos); dos.close(); return Base64.encodeBytes(out.toByteArray()); } finally { IOUtils.closeQuietly(dos); } }
From source file:com.igormaznitsa.jhexed.values.HexSVGImageValue.java
@Override public void write(final OutputStream out) throws IOException { out.write(TYPE_SVGIMAGE);/*from ww w. jav a 2s . c o m*/ super.write(out); final ByteArrayOutputStream imageDataBuffer = new ByteArrayOutputStream(4096); this.image.write(imageDataBuffer, true); final byte[] imageData = imageDataBuffer.toByteArray(); final DataOutputStream dout = out instanceof DataOutputStream ? (DataOutputStream) out : new DataOutputStream(out); dout.writeInt(imageData.length); dout.write(imageData); }