FileDocCategorySizeDatePackage
AudioInputStream.javaAPI DocAndroid 1.5 API5112Wed May 06 22:41:02 BST 2009javax.sound.sampled

AudioInputStream

public class AudioInputStream extends InputStream

Fields Summary
protected AudioFormat
format
protected long
frameLength
protected long
framePos
protected int
frameSize
private InputStream
stream
private TargetDataLine
line
private byte[]
oneByte
private long
marketFramePos
Constructors Summary
public AudioInputStream(InputStream stream, AudioFormat format, long length)


           
        this.stream = stream;
        this.format = format;
        this.frameLength = length;
        this.frameSize = format.getFrameSize();
    
public AudioInputStream(TargetDataLine line)

        this.line = line;
        this.format = line.getFormat();
        this.frameLength = AudioSystem.NOT_SPECIFIED; //TODO
        this.frameSize = this.format.getFrameSize();
    
Methods Summary
public intavailable()

        if (stream != null) { // InputStream
            return Math.min(stream.available(),
                    (int)((frameLength - framePos) * frameSize));
        } else { // TargetDataLine
            return line.available();
        }
    
public voidclose()

        if (stream != null) { // InputStream
            stream.close();
        } else { // TargetDataLine
            line.close();
        }
    
public javax.sound.sampled.AudioFormatgetFormat()

        return format;
    
public longgetFrameLength()

        return frameLength;
    
public voidmark(int readlimit)

        if (stream != null) { //InputStream
            stream.mark(readlimit);
            marketFramePos = framePos;
        } else { // TargetDataLine
            // do nothing
        }
    
public booleanmarkSupported()

        if (stream != null) { //InputStream
            return stream.markSupported();
        } else { // TargetDataLine
            return false;
        }
    
public intread()

        if (frameSize != 1) {
            // sound.0C=Frame size must be one byte
            throw new IOException(Messages.getString("sound.0C")); //$NON-NLS-1$
        }
        int res;
        if (stream != null) { // InputStream
            if (framePos == frameLength) {
                return 0;
            }
            res = stream.read();
            if (res == -1) {
                return -1;
            }
            framePos += 1;
            return res;
        } else { // TargetDataLine
            if (line.read(oneByte, 0, 1) == 0) {
                return -1;
            }
            framePos = line.getLongFramePosition();
            return oneByte[0];
        }
    
public intread(byte[] b)

        return read(b, 0, b.length);
    
public intread(byte[] b, int off, int len)

        int l = Math.min(len, (int) ((frameLength - framePos) * frameSize));
        l = l - (l % frameSize);
        if (l == 0) {
            return 0;
        }
        int res;
        if (stream != null) { // InputStream
            res = stream.read(b, off, l);
            if (res == -1) {
                return -1;
            }
            framePos = framePos + res / frameSize;
            return res;
        } else { // TargetDataLine
            res = line.read(b, off, l);
            if (res == 0) {
                return -1;
            }
            framePos = line.getLongFramePosition();
            return res;
        }

    
public voidreset()

        if (stream != null) { //InputStream
            stream.reset();
            framePos = marketFramePos;
        } else { // TargetDataLine
            // do nothing
        }
    
public longskip(long n)


        if (n < frameSize) {
            return 0;
        }
        byte[] skipBuf = new byte[frameSize];
        long skipped = 0;
        while (skipped < n) {
            int read = read(skipBuf, 0, frameSize);
            if (read == -1) {
                return skipped;
            }
            skipped += read;
            if (n - skipped < frameSize) {
                return skipped;
            }
        }
        return skipped;