Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 1 | // |
| 2 | // Copyright (C) 2002 HorizonLive.com, Inc. All Rights Reserved. |
| 3 | // |
| 4 | // This is free software; you can redistribute it and/or modify |
| 5 | // it under the terms of the GNU General Public License as published by |
| 6 | // the Free Software Foundation; either version 2 of the License, or |
| 7 | // (at your option) any later version. |
| 8 | // |
| 9 | // This software is distributed in the hope that it will be useful, |
| 10 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | // GNU General Public License for more details. |
| 13 | // |
| 14 | // You should have received a copy of the GNU General Public License |
| 15 | // along with this software; if not, write to the Free Software |
| 16 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, |
| 17 | // USA. |
| 18 | // |
| 19 | |
| 20 | // |
| 21 | // FbsInputStream.java |
| 22 | // |
| 23 | |
| 24 | import java.io.*; |
| 25 | |
| 26 | class FbsInputStream extends InputStream { |
| 27 | |
| 28 | protected InputStream in; |
| 29 | protected long startTime; |
| 30 | protected long timeOffset; |
| 31 | |
| 32 | protected byte[] buffer; |
| 33 | protected int bufferSize; |
| 34 | protected int bufferPos; |
| 35 | |
| 36 | // |
| 37 | // Constructors. |
| 38 | // |
| 39 | |
| 40 | FbsInputStream() throws IOException { |
| 41 | throw new IOException("FbsInputStream: no such constructor"); |
| 42 | } |
| 43 | |
| 44 | FbsInputStream(InputStream in) throws IOException |
| 45 | { |
| 46 | this.in = in; |
| 47 | startTime = System.currentTimeMillis(); |
| 48 | timeOffset = 0; |
| 49 | |
| 50 | byte[] b = new byte[12]; |
| 51 | readFully(b); |
| 52 | |
| 53 | if (b[0] != 'F' || b[1] != 'B' || b[2] != 'S' || b[3] != ' ' || |
| 54 | b[4] != '0' || b[5] != '0' || b[6] != '1' || b[7] != '.' || |
| 55 | b[8] < '0' || b[8] > '9' || b[9] < '0' || b[9] > '9' || |
| 56 | b[10] < '0' || b[10] > '9' || b[11] != '\n') { |
| 57 | throw new IOException("Incorrect protocol version"); |
| 58 | } |
| 59 | |
| 60 | buffer = null; |
| 61 | bufferSize = 0; |
| 62 | bufferPos = 0; |
| 63 | } |
| 64 | |
| 65 | // |
| 66 | // Basic methods overriding InputStream's methods. |
| 67 | // |
| 68 | |
| 69 | public int read() throws IOException |
| 70 | { |
| 71 | while (bufferSize == 0) { |
| 72 | if (!fillBuffer()) |
| 73 | return -1; |
| 74 | } |
| 75 | bufferSize--; |
| 76 | return buffer[bufferPos++] & 0xFF; |
| 77 | } |
| 78 | |
| 79 | public int available() throws IOException |
| 80 | { |
| 81 | // FIXME: This will work incorrectly if our caller will wait until |
| 82 | // some amount of data is available when the buffer contains less |
| 83 | // data than then that. Current implementation never reads more |
| 84 | // data until the buffer is fully exhausted. |
| 85 | return bufferSize; |
| 86 | } |
| 87 | |
| 88 | public void close() throws IOException |
| 89 | { |
| 90 | in.close(); |
| 91 | in = null; |
| 92 | startTime = 0; |
| 93 | timeOffset = 0; |
| 94 | |
| 95 | buffer = null; |
| 96 | bufferSize = 0; |
| 97 | bufferPos = 0; |
| 98 | } |
| 99 | |
| 100 | // |
| 101 | // Methods providing additional functionality. |
| 102 | // |
| 103 | |
Constantin Kaplinsky | fe07983 | 2002-05-29 00:52:32 +0000 | [diff] [blame] | 104 | public int getPos() |
| 105 | { |
| 106 | return (int)(timeOffset / 1000); |
| 107 | } |
| 108 | |
Constantin Kaplinsky | 30f786a | 2002-05-29 10:59:52 +0000 | [diff] [blame^] | 109 | public void setPos(int pos) |
| 110 | { |
| 111 | } |
| 112 | |
| 113 | public boolean isSeeking() |
| 114 | { |
| 115 | return false; |
| 116 | } |
| 117 | |
| 118 | public void pausePlayback() |
| 119 | { |
| 120 | } |
| 121 | |
| 122 | public void resumePlayback() |
Constantin Kaplinsky | 903009e | 2002-05-20 10:55:47 +0000 | [diff] [blame] | 123 | { |
| 124 | startTime = System.currentTimeMillis() - timeOffset; |
| 125 | } |
| 126 | |
| 127 | // |
| 128 | // Methods for internal use. |
| 129 | // |
| 130 | |
| 131 | private boolean fillBuffer() throws IOException |
| 132 | { |
| 133 | bufferSize = (int)readUnsigned32(); |
| 134 | if (bufferSize >= 0) { |
| 135 | int realSize = (bufferSize + 3) & 0xFFFFFFFC; |
| 136 | buffer = new byte[realSize]; |
| 137 | readFully(buffer); |
| 138 | bufferPos = 0; |
| 139 | |
| 140 | timeOffset = readUnsigned32(); |
| 141 | } |
| 142 | |
| 143 | if (bufferSize < 0 || timeOffset < 0) { |
| 144 | buffer = null; |
| 145 | bufferSize = 0; |
| 146 | bufferPos = 0; |
| 147 | return false; |
| 148 | } |
| 149 | |
| 150 | while (true) { |
| 151 | long timeDiff = startTime + timeOffset - System.currentTimeMillis(); |
| 152 | if (timeDiff <= 0) { |
| 153 | break; |
| 154 | } |
| 155 | try { |
| 156 | Thread.currentThread().sleep(timeDiff); |
| 157 | } catch (InterruptedException e) { |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | private long readUnsigned32() throws IOException |
| 165 | { |
| 166 | byte[] buf = new byte[4]; |
| 167 | if (!readFully(buf)) |
| 168 | return -1; |
| 169 | |
| 170 | return ((long)(buf[0] & 0xFF) << 24 | |
| 171 | (buf[1] & 0xFF) << 16 | |
| 172 | (buf[2] & 0xFF) << 8 | |
| 173 | (buf[3] & 0xFF)); |
| 174 | } |
| 175 | |
| 176 | private boolean readFully(byte[] b) throws IOException |
| 177 | { |
| 178 | int off = 0; |
| 179 | int len = b.length; |
| 180 | |
| 181 | while (off != len) { |
| 182 | int count = in.read(b, off, len - off); |
| 183 | if (count < 0) { |
| 184 | return false; |
| 185 | } |
| 186 | off += count; |
| 187 | } |
| 188 | |
| 189 | return true; |
| 190 | } |
| 191 | } |
| 192 | |