List of usage examples for java.io DataOutputStream flush
public void flush() throws IOException
From source file:com.geotrackin.gpslogger.senders.gdocs.GDocsHelper.java
private String UpdateFileContents(String authToken, String gpxFileId, byte[] fileContents, String fileName) { HttpURLConnection conn = null; String fileId = null;//from w ww.ja va2 s . co m String fileUpdateUrl = "https://www.googleapis.com/upload/drive/v2/files/" + gpxFileId + "?uploadType=media"; try { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.FROYO) { //Due to a pre-froyo bug //http://android-developers.blogspot.com/2011/09/androids-http-clients.html System.setProperty("http.keepAlive", "false"); } URL url = new URL(fileUpdateUrl); conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("PUT"); conn.setRequestProperty("User-Agent", "GPSLogger for Android"); conn.setRequestProperty("Authorization", "Bearer " + authToken); conn.setRequestProperty("Content-Type", GetMimeTypeFromFileName(fileName)); conn.setRequestProperty("Content-Length", String.valueOf(fileContents.length)); conn.setUseCaches(false); conn.setDoInput(true); conn.setDoOutput(true); DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); wr.write(fileContents); wr.flush(); wr.close(); String fileMetadata = Utilities.GetStringFromInputStream(conn.getInputStream()); JSONObject fileMetadataJson = new JSONObject(fileMetadata); fileId = fileMetadataJson.getString("id"); tracer.debug("File updated : " + fileId); } catch (Exception e) { System.out.println(e.getMessage()); } finally { if (conn != null) { conn.disconnect(); } } return fileId; }
From source file:com.mirth.connect.model.transmission.framemode.FrameStreamHandler.java
protected void write(byte[]... dataArrays) throws IOException { if (dataArrays == null || outputStream == null) { return;// w ww . j a v a 2 s. c om } DataOutputStream dos = new DataOutputStream(outputStream); for (byte[] data : dataArrays) { if (data != null) { for (byte b : data) { dos.writeByte(b); } } } try { dos.flush(); } catch (SocketException e) { logger.debug("Socket closed while trying to flush."); } }
From source file:bankingclient.DNFrame.java
public DNFrame(MainFrame vmain) { initComponents();/*from w w w . ja v a 2s. co m*/ this.jTextField1.setText(""); this.jTextField2.setText(""); this.jTextField_cmt.setText(""); this.jTextField_sdt.setText(""); this.main = vmain; noAcc = new NewOrOldAccFrame(this); this.setVisible(false); jL_sdt.setVisible(false); jL_cmtnd.setVisible(false); jTextField_cmt.setVisible(false); jTextField_sdt.setVisible(false); jBt_dn.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { if (!jCheck_qmk.isSelected()) { try { Socket client = new Socket("113.22.46.207", 6013); DataOutputStream dout = new DataOutputStream(client.getOutputStream()); dout.writeByte(2); dout.writeUTF(jTextField1.getText() + "\n" + jTextField2.getText()); dout.flush(); while (true) { break; } DataInputStream din = new DataInputStream(client.getInputStream()); byte check = din.readByte(); if (check == 1) { noAcc.setVisible(true); DNFrame.this.setVisible(false); noAcc.setMainCustomer(jTextField1.getText()); } else { JOptionPane.showMessageDialog(new JFrame(), "Tn ?ang Nhp Khng Tn Ti, hoac mat khau sai"); } } catch (Exception ex) { ex.printStackTrace(); JOptionPane.showMessageDialog(rootPane, "C Li Kt Ni Mng...."); } } else if ((!jTextField_cmt.getText().equals("")) && (!jTextField_sdt.getText().equals("")) && (NumberUtils.isNumber(jTextField_cmt.getText())) && (NumberUtils.isNumber(jTextField_sdt.getText()))) { try { Socket client = new Socket("113.22.46.207", 6013); DataOutputStream dout = new DataOutputStream(client.getOutputStream()); dout.writeByte(9); dout.writeUTF(jTextField1.getText() + "\n" + jTextField_sdt.getText() + "\n" + jTextField_cmt.getText()); dout.flush(); DataInputStream din = new DataInputStream(client.getInputStream()); byte check = din.readByte(); if (check == 1) { noAcc.setVisible(true); DNFrame.this.setVisible(false); noAcc.setMainCustomer(jTextField1.getText()); } else { JOptionPane.showMessageDialog(new JFrame(), "Khong dang nhap duoc, thong tin sai"); } } catch (Exception ex) { ex.printStackTrace(); } } else { JOptionPane.showMessageDialog(new JFrame(), "Can dien day du thong tin va dung mau"); } } }); jBt_ql.addActionListener(new ActionListener() { public void actionPerformed(ActionEvent e) { main.setVisible(true); DNFrame.this.setVisible(false); } }); jCheck_qmk.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { if (jCheck_qmk.isSelected()) { jL_sdt.setVisible(true); jL_cmtnd.setVisible(true); jTextField_cmt.setVisible(true); jTextField_sdt.setVisible(true); } else { jL_sdt.setVisible(false); jL_cmtnd.setVisible(false); jTextField_cmt.setVisible(false); jTextField_sdt.setVisible(false); } } }); }
From source file:com.ryan.ryanreader.cache.PersistentCookieStore.java
public synchronized byte[] toByteArray() { final ByteArrayOutputStream baos = new ByteArrayOutputStream(); final DataOutputStream dos = new DataOutputStream(baos); try {/*from ww w . j ava2s. c o m*/ dos.writeInt(cookies.size()); for (final Cookie cookie : cookies) { dos.writeUTF(cookie.getName()); dos.writeUTF(cookie.getValue()); dos.writeUTF(cookie.getDomain()); dos.writeUTF(cookie.getPath()); dos.writeBoolean(cookie.isSecure()); } dos.flush(); dos.close(); } catch (IOException e) { throw new RuntimeException(e); } return baos.toByteArray(); }
From source file:edu.pdx.cecs.orcycle.Uploader.java
private boolean uploadOneSegment(long currentNoteId) { boolean result = false; final String postUrl = mCtx.getResources().getString(R.string.post_url); try {//w w w .j av a 2 s. c o m URL url = new URL(postUrl); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); conn.setDoInput(true); // Allow Inputs conn.setDoOutput(true); // Allow Outputs conn.setUseCaches(false); // Don't use a Cached Copy conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("ENCTYPE", "multipart/form-data"); conn.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary); conn.setRequestProperty("Cycleatl-Protocol-Version", "4"); SegmentData segmentData = SegmentData.fetchSegment(mCtx, currentNoteId); JSONObject json = segmentData.getJSON(); String deviceId = userId; DataOutputStream stream = new DataOutputStream(conn.getOutputStream()); stream.writeBytes(makeContentField("ratesegment", json.toString())); stream.writeBytes(makeContentField("version", String.valueOf(kSaveNoteProtocolVersion))); stream.writeBytes(makeContentField("device", deviceId)); stream.writeBytes(contentFieldPrefix); stream.flush(); stream.close(); int serverResponseCode = conn.getResponseCode(); String serverResponseMessage = conn.getResponseMessage(); Log.v(MODULE_TAG, "HTTP Response is : " + serverResponseMessage + ": " + serverResponseCode); if (serverResponseCode == 201 || serverResponseCode == 202) { segmentData.updateSegmentStatus(SegmentData.STATUS_SENT); result = true; } } catch (IllegalStateException e) { e.printStackTrace(); return false; } catch (IOException e) { e.printStackTrace(); return false; } catch (JSONException e) { e.printStackTrace(); return false; } return result; }
From source file:com.geotrackin.gpslogger.senders.gdocs.GDocsHelper.java
private String CreateEmptyFile(String authToken, String fileName, String mimeType, String parentFolderId) { String fileId = null;//ww w . j av a2s . com HttpURLConnection conn = null; String createFileUrl = "https://www.googleapis.com/drive/v2/files"; String createFilePayload = " {\n" + " \"title\": \"" + fileName + "\",\n" + " \"mimeType\": \"" + mimeType + "\",\n" + " \"parents\": [\n" + " {\n" + " \"id\": \"" + parentFolderId + "\"\n" + " }\n" + " ]\n" + " }"; try { if (Build.VERSION.SDK_INT < Build.VERSION_CODES.FROYO) { //Due to a pre-froyo bug //http://android-developers.blogspot.com/2011/09/androids-http-clients.html System.setProperty("http.keepAlive", "false"); } URL url = new URL(createFileUrl); conn = (HttpURLConnection) url.openConnection(); conn.setRequestMethod("POST"); conn.setRequestProperty("User-Agent", "GPSLogger for Android"); conn.setRequestProperty("Authorization", "Bearer " + authToken); conn.setRequestProperty("Content-Type", "application/json"); conn.setUseCaches(false); conn.setDoInput(true); conn.setDoOutput(true); DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); wr.writeBytes(createFilePayload); wr.flush(); wr.close(); fileId = null; String fileMetadata = Utilities.GetStringFromInputStream(conn.getInputStream()); JSONObject fileMetadataJson = new JSONObject(fileMetadata); fileId = fileMetadataJson.getString("id"); tracer.debug("File created with ID " + fileId + " of type " + mimeType); } catch (Exception e) { System.out.println(e.getMessage()); System.out.println(e.getMessage()); } finally { if (conn != null) { conn.disconnect(); } } return fileId; }
From source file:ReadWriteStreams.java
public void writeStream(String[] sData, boolean[] bData, int[] iData) { try {/*from ww w. j a v a 2s.c om*/ // Write data into an internal byte array ByteArrayOutputStream strmBytes = new ByteArrayOutputStream(); // Write Java data types into the above byte array DataOutputStream strmDataType = new DataOutputStream(strmBytes); byte[] record; for (int i = 0; i < sData.length; i++) { // Write Java data types strmDataType.writeUTF(sData[i]); strmDataType.writeBoolean(bData[i]); strmDataType.writeInt(iData[i]); // Clear any buffered data strmDataType.flush(); // Get stream data into byte array and write record record = strmBytes.toByteArray(); rs.addRecord(record, 0, record.length); // Toss any data in the internal array so writes // starts at beginning (of the internal array) strmBytes.reset(); } strmBytes.close(); strmDataType.close(); } catch (Exception e) { db(e.toString()); } }
From source file:calliope.db.CouchConnection.java
/** * PUT a json file to the database/*from w w w. ja va 2s.c o m*/ * @param db the database name * @param docID the docID of the resource * @param json the json to put there * @return the server response */ @Override public String putToDb(String db, String docID, String json) throws AeseException { HttpURLConnection conn = null; try { docIDCheck(db, docID); docID = convertDocID(docID); String login = (user == null) ? "" : user + ":" + password + "@"; String url = "http://" + login + host + ":" + dbPort + "/" + db + "/" + docID; String revid = getRevId(db, docID); if (revid != null) json = addRevId(json, revid); URL u = new URL(url); conn = (HttpURLConnection) u.openConnection(); conn.setRequestMethod("PUT"); conn.setRequestProperty("Content-Type", MIMETypes.JSON); byte[] jData = json.getBytes(); conn.setRequestProperty("Content-Length", Integer.toString(jData.length)); conn.setRequestProperty("Content-Language", "en-US"); conn.setUseCaches(false); conn.setDoInput(true); conn.setDoOutput(true); DataOutputStream wr = new DataOutputStream(conn.getOutputStream()); wr.writeBytes(json); wr.flush(); wr.close(); //Get Response return readResponse(conn, 0L, ""); } catch (Exception e) { if (conn != null) conn.disconnect(); throw new AeseException(e); } }
From source file:com.mbrlabs.mundus.assets.EditorAssetManager.java
public TerrainAsset createTerrainAsset(int vertexResolution, int size) throws IOException { String terraFilename = "terrain_" + UUID.randomUUID().toString() + ".terra"; String metaFilename = terraFilename + ".meta"; // create meta file String metaPath = FilenameUtils.concat(rootFolder.path(), metaFilename); MetaFile meta = createNewMetaFile(new FileHandle(metaPath), AssetType.TERRAIN); meta.setTerrainSize(size);// ww w .j a v a2 s. c om meta.save(); // create terra file String terraPath = FilenameUtils.concat(rootFolder.path(), terraFilename); File terraFile = new File(terraPath); FileUtils.touch(terraFile); // create initial height data float[] data = new float[vertexResolution * vertexResolution]; for (int i = 0; i < data.length; i++) { data[i] = 0; } // write terra file DataOutputStream outputStream = new DataOutputStream( new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(terraFile)))); for (float f : data) { outputStream.writeFloat(f); } outputStream.flush(); outputStream.close(); // load & apply standard chessboard texture TerrainAsset asset = new TerrainAsset(meta, new FileHandle(terraFile)); asset.load(); TextureAsset chessboard = (TextureAsset) findAssetByID(STANDARD_ASSET_TEXTURE_CHESSBOARD); if (chessboard != null) { // create splatmap PixmapTextureAsset splatmap = createPixmapTextureAsset(SplatMap.DEFAULT_SIZE); asset.setSplatmap(splatmap); asset.setSplatBase(chessboard); asset.applyDependencies(); } addAsset(asset); return asset; }
From source file:com.tonchidot.nfc_contact_exchanger.lib.PictureUploader.java
private String doFileUploadJson(String url, String fileParamName, byte[] data) { try {/*from ww w . j a v a 2 s .c om*/ String boundary = "BOUNDARY" + new Date().getTime() + "BOUNDARY"; String lineEnd = "\r\n"; String twoHyphens = "--"; URL connUrl = new URL(url); HttpURLConnection conn = (HttpURLConnection) connUrl.openConnection(); conn.setDoInput(true); conn.setDoOutput(true); conn.setUseCaches(false); conn.setRequestMethod("POST"); conn.setRequestProperty("Connection", "Keep-Alive"); conn.setRequestProperty("Content-Type", "multipart/form-data;boundary=" + boundary); DataOutputStream dos = new DataOutputStream(conn.getOutputStream()); dos.writeBytes(twoHyphens + boundary + lineEnd); dos.writeBytes("Content-Disposition: form-data; name=\"" + fileParamName + "\";filename=\"photo.jpg\"" + lineEnd); dos.writeBytes(lineEnd); dos.write(data, 0, data.length); dos.writeBytes(lineEnd); dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd); dos.flush(); dos.close(); BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); StringBuilder result = new StringBuilder(); String line; while ((line = rd.readLine()) != null) { result.append(line); } rd.close(); return result.toString(); } catch (IOException e) { if (Config.LOGD) { Log.d(TAG, "IOException : " + e); } } return null; }