DelayedDiskWrite.java revision 6346155c5a48495944041e65ec279c88b0fa1391
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.server.net;
18
19import android.os.Handler;
20import android.os.HandlerThread;
21import android.text.TextUtils;
22import android.util.Log;
23
24import java.io.BufferedOutputStream;
25import java.io.DataOutputStream;
26import java.io.FileOutputStream;
27import java.io.IOException;
28
29public class DelayedDiskWrite {
30    private HandlerThread mDiskWriteHandlerThread;
31    private Handler mDiskWriteHandler;
32    /* Tracks multiple writes on the same thread */
33    private int mWriteSequence = 0;
34    private final String TAG = "DelayedDiskWrite";
35
36    public interface Writer {
37        public void onWriteCalled(DataOutputStream out) throws IOException;
38    }
39
40    public void write(final String filePath, final Writer w) {
41        if (TextUtils.isEmpty(filePath)) {
42            throw new IllegalArgumentException("empty file path");
43        }
44
45        /* Do a delayed write to disk on a separate handler thread */
46        synchronized (this) {
47            if (++mWriteSequence == 1) {
48                mDiskWriteHandlerThread = new HandlerThread("DelayedDiskWriteThread");
49                mDiskWriteHandlerThread.start();
50                mDiskWriteHandler = new Handler(mDiskWriteHandlerThread.getLooper());
51            }
52        }
53
54        mDiskWriteHandler.post(new Runnable() {
55            @Override
56            public void run() {
57                doWrite(filePath, w);
58            }
59        });
60    }
61
62    private void doWrite(String filePath, Writer w) {
63        DataOutputStream out = null;
64        try {
65            out = new DataOutputStream(new BufferedOutputStream(
66                        new FileOutputStream(filePath)));
67            w.onWriteCalled(out);
68        } catch (IOException e) {
69            loge("Error writing data file " + filePath);
70        } finally {
71            if (out != null) {
72                try {
73                    out.close();
74                } catch (Exception e) {}
75            }
76
77            // Quit if no more writes sent
78            synchronized (this) {
79                if (--mWriteSequence == 0) {
80                    mDiskWriteHandler.getLooper().quit();
81                    mDiskWriteHandler = null;
82                    mDiskWriteHandlerThread = null;
83                }
84            }
85        }
86    }
87
88    private void loge(String s) {
89        Log.e(TAG, s);
90    }
91}
92
93