FileCache: use the cache even when the system clock is wrong
[xcsoar.git] / android / src / OutputThread.java
blob2ceadc974ce5c7f93eff3e3b8ed439c227c9a799
1 /*
2 Copyright_License {
4 XCSoar Glide Computer - http://www.xcsoar.org/
5 Copyright (C) 2000-2013 The XCSoar Project
6 A detailed list of copyright holders can be found in the file "AUTHORS".
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License
10 as published by the Free Software Foundation; either version 2
11 of the License, or (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 package org.xcsoar;
26 import java.io.IOException;
27 import java.io.OutputStream;
28 import android.util.Log;
30 /**
31 * A wrapper for an OutputStream which allows writing with a timeout.
33 class OutputThread extends Thread {
34 private static final String TAG = "XCSoar";
36 static final int BUFFER_SIZE = 256;
38 final String name;
40 OutputStream os;
42 int timeout = 0;
44 byte[] buffer = new byte[BUFFER_SIZE];
45 int head, tail;
47 OutputThread(String _name, OutputStream _os) {
48 super("InputThread " + _name);
50 name = _name;
51 os = _os;
53 start();
56 synchronized void close() {
57 OutputStream os2 = os;
58 if (os2 == null)
59 return;
61 os = null;
63 try {
64 os2.close();
65 } catch (IOException e) {
68 notifyAll();
71 synchronized boolean drain() {
72 while (os != null && head < tail) {
73 try {
74 wait();
75 } catch (InterruptedException e) {
76 return false;
80 return os != null;
83 void setTimeout(int _timeout) {
84 timeout = _timeout;
87 private void shift() {
88 System.arraycopy(buffer, head, buffer, 0, tail - head);
89 tail -= head;
90 head = 0;
93 @Override public void run() {
94 try {
95 byte[] copy = new byte[BUFFER_SIZE];
97 while (true) {
98 OutputStream os2;
99 int size;
101 synchronized(this) {
102 while (os != null && head >= tail) {
103 try {
104 wait();
105 } catch (InterruptedException e) {
109 os2 = os;
110 if (os2 == null)
111 // close() was called
112 break;
114 size = tail - head;
115 System.arraycopy(buffer, head, copy, 0, size);
118 os2.write(copy, 0, size);
120 synchronized(this) {
121 head += size;
122 notifyAll();
125 } catch (IOException e) {
126 if (os != null)
127 Log.e(TAG, "Failed to write to " + name, e);
129 close();
130 } finally {
131 synchronized(this) {
132 notifyAll();
137 public synchronized int write(byte[] data, int length) {
138 if (os == null)
139 return -1;
141 if (tail >= BUFFER_SIZE) {
142 if (head == 0) {
143 // buffer is full
145 if (timeout <= 0)
146 return -1;
148 try {
149 wait(timeout);
150 } catch (InterruptedException e) {
151 return -1;
154 if (os == null || head == 0)
155 // still full, timeout
156 return -1;
159 shift();
162 final boolean was_empty = head == tail;
163 int nbytes = BUFFER_SIZE - tail;
164 if (nbytes > length)
165 nbytes = length;
167 System.arraycopy(data, 0, buffer, tail, nbytes);
169 tail += nbytes;
171 if (was_empty)
172 // notify the thread that it may continue writing
173 notifyAll();
175 return nbytes;