FileDocCategorySizeDatePackage
DelayedDiskWrite.javaAPI DocAndroid 5.1 API2931Thu Mar 12 22:22:42 GMT 2015com.android.server.net

DelayedDiskWrite

public class DelayedDiskWrite extends Object

Fields Summary
private android.os.HandlerThread
mDiskWriteHandlerThread
private android.os.Handler
mDiskWriteHandler
private int
mWriteSequence
private final String
TAG
Constructors Summary
Methods Summary
private voiddoWrite(java.lang.String filePath, com.android.server.net.DelayedDiskWrite$Writer w)

        DataOutputStream out = null;
        try {
            out = new DataOutputStream(new BufferedOutputStream(
                        new FileOutputStream(filePath)));
            w.onWriteCalled(out);
        } catch (IOException e) {
            loge("Error writing data file " + filePath);
        } finally {
            if (out != null) {
                try {
                    out.close();
                } catch (Exception e) {}
            }

            // Quit if no more writes sent
            synchronized (this) {
                if (--mWriteSequence == 0) {
                    mDiskWriteHandler.getLooper().quit();
                    mDiskWriteHandler = null;
                    mDiskWriteHandlerThread = null;
                }
            }
        }
    
private voidloge(java.lang.String s)

        Log.e(TAG, s);
    
public voidwrite(java.lang.String filePath, com.android.server.net.DelayedDiskWrite$Writer w)


       
             
    

            
        if (TextUtils.isEmpty(filePath)) {
            throw new IllegalArgumentException("empty file path");
        }

        /* Do a delayed write to disk on a separate handler thread */
        synchronized (this) {
            if (++mWriteSequence == 1) {
                mDiskWriteHandlerThread = new HandlerThread("DelayedDiskWriteThread");
                mDiskWriteHandlerThread.start();
                mDiskWriteHandler = new Handler(mDiskWriteHandlerThread.getLooper());
            }
        }

        mDiskWriteHandler.post(new Runnable() {
            @Override
            public void run() {
                doWrite(filePath, w);
            }
        });