< prev index next >

src/java.desktop/share/classes/com/sun/media/sound/RIFFReader.java

Print this page




 101         if (lastiterator != null)
 102             lastiterator.finish();
 103         if (avail == 0)
 104             return null;
 105         lastiterator = new RIFFReader(this);
 106         return lastiterator;
 107     }
 108 
 109     public String getFormat() {
 110         return fourcc;
 111     }
 112 
 113     public String getType() {
 114         return riff_type;
 115     }
 116 
 117     public long getSize() {
 118         return ckSize;
 119     }
 120 

 121     public int read() throws IOException {
 122         if (avail == 0) {
 123             return -1;
 124         }
 125         int b = stream.read();
 126         if (b == -1) {
 127             avail = 0;
 128             return -1;
 129         }
 130         avail--;
 131         filepointer++;
 132         return b;
 133     }
 134 

 135     public int read(byte[] b, int offset, int len) throws IOException {
 136         if (avail == 0) {
 137             return -1;
 138         }
 139         if (len > avail) {
 140             int rlen = stream.read(b, offset, (int)avail);
 141             if (rlen != -1)
 142                 filepointer += rlen;
 143             avail = 0;
 144             return rlen;
 145         } else {
 146             int ret = stream.read(b, offset, len);
 147             if (ret == -1) {
 148                 avail = 0;
 149                 return -1;
 150             }
 151             avail -= ret;
 152             filepointer += ret;
 153             return ret;
 154         }
 155     }
 156 
 157     public void readFully(byte b[]) throws IOException {
 158         readFully(b, 0, b.length);
 159     }
 160 
 161     public void readFully(byte b[], int off, int len) throws IOException {
 162         if (len < 0)
 163             throw new IndexOutOfBoundsException();
 164         while (len > 0) {
 165             int s = read(b, off, len);
 166             if (s < 0)
 167                 throw new EOFException();
 168             if (s == 0)
 169                 Thread.yield();
 170             off += s;
 171             len -= s;
 172         }
 173     }
 174 
 175     public long skipBytes(long n) throws IOException {
 176         if (n < 0)

 177             return 0;
 178         long skipped = 0;
 179         while (skipped != n) {
 180             long s = skip(n - skipped);
 181             if (s < 0)
 182                 break;
 183             if (s == 0)
 184                 Thread.yield();
 185             skipped += s;
 186         }
 187         return skipped;
 188     }
 189 
 190     public long skip(long n) throws IOException {
 191         if (avail == 0)
 192             return -1;
 193         if (n > avail) {
 194             long len = stream.skip(avail);
 195             if (len != -1)
 196                 filepointer += len;
 197             avail = 0;
 198             return len;
 199         } else {
 200             long ret = stream.skip(n);
 201             if (ret == -1) {

 202                 avail = 0;
 203                 return -1;
 204             }

 205             avail -= ret;
 206             filepointer += ret;
 207             return ret;
 208         }

 209     }
 210 

 211     public int available() {
 212         return (int)avail;
 213     }
 214 
 215     public void finish() throws IOException {
 216         if (avail != 0) {
 217             skipBytes(avail);
 218         }
 219     }
 220 
 221     // Read ASCII chars from stream
 222     public String readString(final int len) throws IOException {
 223         final byte[] buff;
 224         try {
 225             buff = new byte[len];
 226         } catch (final OutOfMemoryError oom) {
 227             throw new IOException("Length too big", oom);
 228         }
 229         readFully(buff);
 230         for (int i = 0; i < buff.length; i++) {
 231             if (buff[i] == 0) {
 232                 return new String(buff, 0, i, "ascii");
 233             }
 234         }
 235         return new String(buff, "ascii");
 236     }
 237 


 320         return ch1 | (ch2 << 8);
 321     }
 322 
 323     // Read 32 bit unsigned integer from stream
 324     public long readUnsignedInt() throws IOException {
 325         long ch1 = read();
 326         long ch2 = read();
 327         long ch3 = read();
 328         long ch4 = read();
 329         if (ch1 < 0)
 330             throw new EOFException();
 331         if (ch2 < 0)
 332             throw new EOFException();
 333         if (ch3 < 0)
 334             throw new EOFException();
 335         if (ch4 < 0)
 336             throw new EOFException();
 337         return ch1 + (ch2 << 8) | (ch3 << 16) | (ch4 << 24);
 338     }
 339 

 340     public void close() throws IOException {
 341         finish();
 342         if (this == root)
 343             stream.close();
 344         stream = null;
 345     }
 346 }


 101         if (lastiterator != null)
 102             lastiterator.finish();
 103         if (avail == 0)
 104             return null;
 105         lastiterator = new RIFFReader(this);
 106         return lastiterator;
 107     }
 108 
 109     public String getFormat() {
 110         return fourcc;
 111     }
 112 
 113     public String getType() {
 114         return riff_type;
 115     }
 116 
 117     public long getSize() {
 118         return ckSize;
 119     }
 120 
 121     @Override
 122     public int read() throws IOException {
 123         if (avail == 0) {
 124             return -1;
 125         }
 126         int b = stream.read();
 127         if (b == -1) {
 128             avail = 0;
 129             return -1;
 130         }
 131         avail--;
 132         filepointer++;
 133         return b;
 134     }
 135 
 136     @Override
 137     public int read(byte[] b, int offset, int len) throws IOException {
 138         if (avail == 0) {
 139             return -1;
 140         }
 141         if (len > avail) {
 142             int rlen = stream.read(b, offset, (int)avail);
 143             if (rlen != -1)
 144                 filepointer += rlen;
 145             avail = 0;
 146             return rlen;
 147         } else {
 148             int ret = stream.read(b, offset, len);
 149             if (ret == -1) {
 150                 avail = 0;
 151                 return -1;
 152             }
 153             avail -= ret;
 154             filepointer += ret;
 155             return ret;
 156         }
 157     }
 158 
 159     public void readFully(byte b[]) throws IOException {
 160         readFully(b, 0, b.length);
 161     }
 162 
 163     public void readFully(byte b[], int off, int len) throws IOException {
 164         if (len < 0)
 165             throw new IndexOutOfBoundsException();
 166         while (len > 0) {
 167             int s = read(b, off, len);
 168             if (s < 0)
 169                 throw new EOFException();
 170             if (s == 0)
 171                 Thread.yield();
 172             off += s;
 173             len -= s;
 174         }
 175     }
 176 
 177     @Override
 178     public long skip(final long n) throws IOException {
 179         if (n <= 0 || avail == 0) {
 180             return 0;








 181         }
 182         // will not skip more than
 183         long remaining = Math.min(n, avail);
 184         while (remaining > 0) {
 185             // Some input streams like FileInputStream can return more bytes,
 186             // when EOF is reached.
 187             long ret = Math.min(stream.skip(remaining), remaining);
 188             if (ret == 0) {
 189                 // EOF or not? we need to check.
 190                 Thread.yield();
 191                 if (stream.read() == -1) {
 192                     avail = 0;
 193                     break;
 194                 }
 195                 ret = 1;
 196             } else if (ret < 0) {
 197                 // the skip should not return negative value, but check it also
 198                 avail = 0;
 199                 break;
 200             }
 201             remaining -= ret;
 202             avail -= ret;
 203             filepointer += ret;

 204         }
 205         return n - remaining;
 206     }
 207 
 208     @Override
 209     public int available() {
 210         return (int)avail;
 211     }
 212 
 213     public void finish() throws IOException {
 214         if (avail != 0) {
 215             skip(avail);
 216         }
 217     }
 218 
 219     // Read ASCII chars from stream
 220     public String readString(final int len) throws IOException {
 221         final byte[] buff;
 222         try {
 223             buff = new byte[len];
 224         } catch (final OutOfMemoryError oom) {
 225             throw new IOException("Length too big", oom);
 226         }
 227         readFully(buff);
 228         for (int i = 0; i < buff.length; i++) {
 229             if (buff[i] == 0) {
 230                 return new String(buff, 0, i, "ascii");
 231             }
 232         }
 233         return new String(buff, "ascii");
 234     }
 235 


 318         return ch1 | (ch2 << 8);
 319     }
 320 
 321     // Read 32 bit unsigned integer from stream
 322     public long readUnsignedInt() throws IOException {
 323         long ch1 = read();
 324         long ch2 = read();
 325         long ch3 = read();
 326         long ch4 = read();
 327         if (ch1 < 0)
 328             throw new EOFException();
 329         if (ch2 < 0)
 330             throw new EOFException();
 331         if (ch3 < 0)
 332             throw new EOFException();
 333         if (ch4 < 0)
 334             throw new EOFException();
 335         return ch1 + (ch2 << 8) | (ch3 << 16) | (ch4 << 24);
 336     }
 337 
 338     @Override
 339     public void close() throws IOException {
 340         finish();
 341         if (this == root)
 342             stream.close();
 343         stream = null;
 344     }
 345 }
< prev index next >