001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package javax.sound.sampled;
019:
020: import java.io.IOException;
021: import java.io.InputStream;
022:
023: import org.apache.harmony.sound.internal.nls.Messages;
024:
025: public class AudioInputStream extends InputStream {
026:
027: protected AudioFormat format;
028:
029: protected long frameLength;
030:
031: protected long framePos;
032:
033: protected int frameSize;
034:
035: private InputStream stream;
036:
037: private TargetDataLine line;
038:
039: private byte[] oneByte = new byte[1];
040:
041: private long marketFramePos;
042:
043: public AudioInputStream(InputStream stream, AudioFormat format,
044: long length) {
045: this .stream = stream;
046: this .format = format;
047: this .frameLength = length;
048: this .frameSize = format.getFrameSize();
049: }
050:
051: public AudioInputStream(TargetDataLine line) {
052: this .line = line;
053: this .format = line.getFormat();
054: this .frameLength = AudioSystem.NOT_SPECIFIED; //TODO
055: this .frameSize = this .format.getFrameSize();
056: }
057:
058: public AudioFormat getFormat() {
059: return format;
060: }
061:
062: public long getFrameLength() {
063: return frameLength;
064: }
065:
066: public int read() throws IOException {
067: if (frameSize != 1) {
068: // sound.0C=Frame size must be one byte
069: throw new IOException(Messages.getString("sound.0C")); //$NON-NLS-1$
070: }
071: int res;
072: if (stream != null) { // InputStream
073: if (framePos == frameLength) {
074: return 0;
075: }
076: res = stream.read();
077: if (res == -1) {
078: return -1;
079: }
080: framePos += 1;
081: return res;
082: } else { // TargetDataLine
083: if (line.read(oneByte, 0, 1) == 0) {
084: return -1;
085: }
086: framePos = line.getLongFramePosition();
087: return oneByte[0];
088: }
089: }
090:
091: public int read(byte[] b) throws IOException {
092: return read(b, 0, b.length);
093: }
094:
095: public int read(byte[] b, int off, int len) throws IOException {
096: int l = Math.min(len,
097: (int) ((frameLength - framePos) * frameSize));
098: l = l - (l % frameSize);
099: if (l == 0) {
100: return 0;
101: }
102: int res;
103: if (stream != null) { // InputStream
104: res = stream.read(b, off, l);
105: if (res == -1) {
106: return -1;
107: }
108: framePos = framePos + res / frameSize;
109: return res;
110: } else { // TargetDataLine
111: res = line.read(b, off, l);
112: if (res == 0) {
113: return -1;
114: }
115: framePos = line.getLongFramePosition();
116: return res;
117: }
118:
119: }
120:
121: public long skip(long n) throws IOException {
122:
123: if (n < frameSize) {
124: return 0;
125: }
126: byte[] skipBuf = new byte[frameSize];
127: long skipped = 0;
128: while (skipped < n) {
129: int read = read(skipBuf, 0, frameSize);
130: if (read == -1) {
131: return skipped;
132: }
133: skipped += read;
134: if (n - skipped < frameSize) {
135: return skipped;
136: }
137: }
138: return skipped;
139:
140: }
141:
142: public int available() throws IOException {
143: if (stream != null) { // InputStream
144: return Math.min(stream.available(),
145: (int) ((frameLength - framePos) * frameSize));
146: } else { // TargetDataLine
147: return line.available();
148: }
149: }
150:
151: public void close() throws IOException {
152: if (stream != null) { // InputStream
153: stream.close();
154: } else { // TargetDataLine
155: line.close();
156: }
157: }
158:
159: public void mark(int readlimit) {
160: if (stream != null) { //InputStream
161: stream.mark(readlimit);
162: marketFramePos = framePos;
163: } else { // TargetDataLine
164: // do nothing
165: }
166: }
167:
168: public void reset() throws IOException {
169: if (stream != null) { //InputStream
170: stream.reset();
171: framePos = marketFramePos;
172: } else { // TargetDataLine
173: // do nothing
174: }
175: }
176:
177: public boolean markSupported() {
178: if (stream != null) { //InputStream
179: return stream.markSupported();
180: } else { // TargetDataLine
181: return false;
182: }
183: }
184:
185: }
|