List of usage examples for java.io FileDescriptor sync
public native void sync() throws SyncFailedException;
From source file:Main.java
public static void main(String[] argv) throws Exception { FileOutputStream os = new FileOutputStream("outfilename"); FileDescriptor fd = os.getFD(); os.write(1);/*from w w w . j av a 2 s.com*/ os.flush(); fd.sync(); }
From source file:Main.java
public static boolean saveApk(File apk, String savePath) { FileInputStream in = null;//from ww w.j av a 2s . co m RandomAccessFile accessFile = null; try { in = new FileInputStream(apk); byte[] buf = new byte[1024 * 4]; int len; File file = new File(savePath); accessFile = new RandomAccessFile(file, "rw"); FileDescriptor fd = accessFile.getFD(); while ((len = in.read(buf)) != -1) { accessFile.write(buf, 0, len); } fd.sync(); accessFile.close(); in.close(); return true; } catch (Exception e) { e.printStackTrace(); try { if (in != null) { in.close(); } if (accessFile != null) { accessFile.close(); } } catch (IOException e1) { e1.printStackTrace(); } return false; } }
From source file:com.fujitsu.dc.core.bar.BarFileInstaller.java
/** * ??./*from w w w . j a v a2 s. c om*/ * @param fd * @throws SyncFailedException ?? */ public void sync(FileDescriptor fd) throws SyncFailedException { fd.sync(); }
From source file:tree.love.providers.downloads.DownloadThread.java
/** * Transfer data from the given connection to the destination file. *//*from www . j a v a 2 s . c om*/ private void transferData(State state, HttpURLConnection conn) throws StopRequestException { // DrmManagerClient drmClient = null; InputStream in = null; OutputStream out = null; FileDescriptor outFd = null; try { try { in = conn.getInputStream(); } catch (IOException e) { throw new StopRequestException(STATUS_HTTP_DATA_ERROR, e); } try { // if (DownloadDrmHelper.isDrmConvertNeeded(state.mMimeType)) { // drmClient = new DrmManagerClient(mContext); // final RandomAccessFile file = new RandomAccessFile( // new File(state.mFilename), "rw"); // out = new DrmOutputStream(drmClient, file, state.mMimeType); // outFd = file.getFD(); // } else { out = new FileOutputStream(state.mFilename, true); outFd = ((FileOutputStream) out).getFD(); // } } catch (IOException e) { throw new StopRequestException(STATUS_FILE_ERROR, e); } // Start streaming data, periodically watch for pause/cancel // commands and checking disk space as needed. transferData(state, in, out); // try { // if (out instanceof DrmOutputStream) { // ((DrmOutputStream) out).finish(); // } // } catch (IOException e) { // throw new StopRequestException(STATUS_FILE_ERROR, e); // } } finally { // if (drmClient != null) { // drmClient.release(); // } IoUtils.closeQuietly(in); try { if (out != null) out.flush(); if (outFd != null) outFd.sync(); } catch (IOException e) { } finally { IoUtils.closeQuietly(out); } } }
From source file:tufts.vue.action.ActionUtil.java
private static void doMarshallMap(final File targetFile, final File tmpFile, final LWMap map) throws java.io.IOException, org.exolab.castor.xml.MarshalException, org.exolab.castor.xml.ValidationException, org.exolab.castor.mapping.MappingException { final String path = tmpFile.getAbsolutePath().replaceAll("%20", " "); final FileOutputStream fos = new FileOutputStream(path); final OutputStreamWriter writer; FileDescriptor FD = null; try {/* w ww .j a v a2 s . co m*/ FD = fos.getFD(); // getting the FileDescriptor is not required -- we just use it // for a final call to sync after we save to increase the likelyhood // of our save file actually making it to disk. } catch (Throwable t) { Log.warn("No FileDescriptor for " + path + "; failsafe sync will be skipped: " + t); } if (OUTPUT_ENCODING.equals("UTF-8") || OUTPUT_ENCODING.equals("UTF8")) { writer = new OutputStreamWriter(fos, OUTPUT_ENCODING); } else { // For the actual file writer we can use the default encoding because we're // marshalling specifically in US-ASCII. E.g., because we direct castor to // fully encode any special characters via setEncoding("US-ASCII"), we'll // only have ASCII chars to write anyway, and any default encoding will // handle that... writer = new OutputStreamWriter(fos); // below creates duplicate FOS, but may be better for debug? (MarshallException's can find file?) //if (FD == null) // writer = new FileWriter(path); //else // writer = new FileWriter(FD); } if (DEBUG.IO) { try { Log.debug(String.format("%s; %s; encoding: \"%s\", which will represent \"%s\" XML content", tmpFile, writer, writer.getEncoding(), OUTPUT_ENCODING)); } catch (Throwable t) { Log.warn(t); } } //======================================================= // Marshall the map to the tmp file: // --------------------------------- marshallMapToWriter(writer, map, targetFile, tmpFile); //======================================================= // Run a filesystem sync if we can just to be sure: Especially helpful on some // linux file systems, such as Ext3, which may not normally touch the disk for // another 5 seconds, or XFS/Ext4, which may take their own sweet time. // For more see: // https://bugs.launchpad.net/ubuntu/+source/linux/+bug/317781/comments/54 if (DEBUG.IO) Log.debug("flushing " + writer); writer.flush(); if (FD != null) { try { if (DEBUG.IO) Log.debug("syncing " + FD + "; for " + tmpFile); // just as backup -- must however be done before writer.close() FD.sync(); Log.info(" sync'd " + FD + "; for " + tmpFile); } catch (Throwable t) { Log.warn("after save to " + targetFile + "; sync failed: " + t); } } if (DEBUG.IO) Log.debug("closing " + writer); writer.close(); if (DEBUG.IO) Log.debug(" closed " + writer); }
From source file:com.codename1.impl.android.AndroidImplementation.java
/** * @inheritDoc/* www . ja v a2 s. c om*/ */ public void closingOutput(OutputStream s) { // For some reasons the Android guys chose not doing this by default: // http://android-developers.blogspot.com/2010/12/saving-data-safely.html // this seems to be a mistake of sacrificing stability for minor performance // gains which will only be noticeable on a server. if (s != null) { if (s instanceof FileOutputStream) { try { FileDescriptor fd = ((FileOutputStream) s).getFD(); if (fd != null) { fd.sync(); } } catch (IOException ex) { // this exception doesn't help us ex.printStackTrace(); } } } }