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.
26 import java
.io
.IOException
;
27 import java
.io
.OutputStream
;
28 import android
.util
.Log
;
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;
44 byte[] buffer
= new byte[BUFFER_SIZE
];
47 OutputThread(String _name
, OutputStream _os
) {
48 super("InputThread " + _name
);
56 synchronized void close() {
57 OutputStream os2
= os
;
65 } catch (IOException e
) {
71 synchronized boolean drain() {
72 while (os
!= null && head
< tail
) {
75 } catch (InterruptedException e
) {
83 void setTimeout(int _timeout
) {
87 private void shift() {
88 System
.arraycopy(buffer
, head
, buffer
, 0, tail
- head
);
93 @Override public void run() {
95 byte[] copy
= new byte[BUFFER_SIZE
];
102 while (os
!= null && head
>= tail
) {
105 } catch (InterruptedException e
) {
111 // close() was called
115 System
.arraycopy(buffer
, head
, copy
, 0, size
);
118 os2
.write(copy
, 0, size
);
125 } catch (IOException e
) {
127 Log
.e(TAG
, "Failed to write to " + name
, e
);
137 public synchronized int write(byte[] data
, int length
) {
141 if (tail
>= BUFFER_SIZE
) {
150 } catch (InterruptedException e
) {
154 if (os
== null || head
== 0)
155 // still full, timeout
162 final boolean was_empty
= head
== tail
;
163 int nbytes
= BUFFER_SIZE
- tail
;
167 System
.arraycopy(data
, 0, buffer
, tail
, nbytes
);
172 // notify the thread that it may continue writing