| 98 | } |
| 99 | |
| 100 | @Override public void run() { |
| 101 | try { |
| 102 | byte[] copy = new byte[BUFFER_SIZE]; |
| 103 | |
| 104 | while (true) { |
| 105 | OutputStream os2; |
| 106 | int size; |
| 107 | |
| 108 | synchronized(this) { |
| 109 | while (os != null && head >= tail) { |
| 110 | try { |
| 111 | wait(); |
| 112 | } catch (InterruptedException e) { |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | os2 = os; |
| 117 | if (os2 == null) |
| 118 | // close() was called |
| 119 | break; |
| 120 | |
| 121 | size = tail - head; |
| 122 | System.arraycopy(buffer, head, copy, 0, size); |
| 123 | } |
| 124 | |
| 125 | os2.write(copy, 0, size); |
| 126 | |
| 127 | synchronized(this) { |
| 128 | head += size; |
| 129 | notifyAll(); |
| 130 | } |
| 131 | } |
| 132 | } catch (IOException e) { |
| 133 | if (os != null) |
| 134 | Log.e(TAG, "Failed to write to " + name, e); |
| 135 | |
| 136 | close(); |
| 137 | } finally { |
| 138 | synchronized(this) { |
| 139 | notifyAll(); |
| 140 | } |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | public synchronized int write(byte[] data, int length) { |
| 145 | if (os == null) |