author | darcy |
Mon, 05 May 2014 23:19:00 -0700 | |
changeset 24548 | 9c007a986347 |
parent 18215 | b2afd66ce6db |
permissions | -rw-r--r-- |
2 | 1 |
/* |
18215 | 2 |
* Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. |
2 | 3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
4 |
* |
|
5 |
* This code is free software; you can redistribute it and/or modify it |
|
6 |
* under the terms of the GNU General Public License version 2 only, as |
|
5506 | 7 |
* published by the Free Software Foundation. Oracle designates this |
2 | 8 |
* particular file as subject to the "Classpath" exception as provided |
5506 | 9 |
* by Oracle in the LICENSE file that accompanied this code. |
2 | 10 |
* |
11 |
* This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 |
* version 2 for more details (a copy is included in the LICENSE file that |
|
15 |
* accompanied this code). |
|
16 |
* |
|
17 |
* You should have received a copy of the GNU General Public License version |
|
18 |
* 2 along with this work; if not, write to the Free Software Foundation, |
|
19 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 |
* |
|
5506 | 21 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
22 |
* or visit www.oracle.com if you need additional information or have any |
|
23 |
* questions. |
|
2 | 24 |
*/ |
25 |
||
26 |
package com.sun.media.sound; |
|
27 |
||
28 |
import java.io.File; |
|
29 |
import java.io.InputStream; |
|
30 |
import java.io.OutputStream; |
|
31 |
import java.io.IOException; |
|
32 |
import java.io.DataInputStream; |
|
33 |
||
34 |
import javax.sound.sampled.AudioFileFormat; |
|
35 |
import javax.sound.sampled.AudioInputStream; |
|
36 |
import javax.sound.sampled.spi.AudioFileWriter; |
|
37 |
||
38 |
||
39 |
||
40 |
||
41 |
/** |
|
42 |
* Abstract File Writer class. |
|
43 |
* |
|
44 |
* @author Jan Borgersen |
|
45 |
*/ |
|
46 |
abstract class SunFileWriter extends AudioFileWriter { |
|
47 |
||
48 |
||
49 |
// buffer size for write |
|
50 |
protected static final int bufferSize = 16384; |
|
51 |
||
52 |
// buffer size for temporary input streams |
|
53 |
protected static final int bisBufferSize = 4096; |
|
54 |
||
55 |
||
56 |
final AudioFileFormat.Type types[]; |
|
57 |
||
58 |
||
59 |
/** |
|
60 |
* Constructs a new SunParser object. |
|
61 |
*/ |
|
62 |
SunFileWriter(AudioFileFormat.Type types[]) { |
|
63 |
this.types = types; |
|
64 |
} |
|
65 |
||
66 |
||
67 |
||
68 |
// METHODS TO IMPLEMENT AudioFileWriter |
|
69 |
||
70 |
// new, 10.27.99 |
|
71 |
||
18215 | 72 |
public final AudioFileFormat.Type[] getAudioFileTypes(){ |
2 | 73 |
AudioFileFormat.Type[] localArray = new AudioFileFormat.Type[types.length]; |
74 |
System.arraycopy(types, 0, localArray, 0, types.length); |
|
75 |
return localArray; |
|
76 |
} |
|
77 |
||
78 |
||
79 |
public abstract AudioFileFormat.Type[] getAudioFileTypes(AudioInputStream stream); |
|
80 |
||
81 |
public abstract int write(AudioInputStream stream, AudioFileFormat.Type fileType, OutputStream out) throws IOException; |
|
82 |
||
83 |
public abstract int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException; |
|
84 |
||
85 |
||
86 |
// HELPER METHODS |
|
87 |
||
88 |
||
89 |
/** |
|
90 |
* rllong |
|
91 |
* Protected helper method to read 64 bits and changing the order of |
|
92 |
* each bytes. |
|
93 |
* @param DataInputStream |
|
94 |
* @return 32 bits swapped value. |
|
95 |
* @exception IOException |
|
96 |
*/ |
|
18215 | 97 |
final int rllong(DataInputStream dis) throws IOException { |
2 | 98 |
|
99 |
int b1, b2, b3, b4 ; |
|
100 |
int i = 0; |
|
101 |
||
102 |
i = dis.readInt(); |
|
103 |
||
104 |
b1 = ( i & 0xFF ) << 24 ; |
|
105 |
b2 = ( i & 0xFF00 ) << 8; |
|
106 |
b3 = ( i & 0xFF0000 ) >> 8; |
|
107 |
b4 = ( i & 0xFF000000 ) >>> 24; |
|
108 |
||
109 |
i = ( b1 | b2 | b3 | b4 ); |
|
110 |
||
111 |
return i; |
|
112 |
} |
|
113 |
||
114 |
/** |
|
115 |
* big2little |
|
116 |
* Protected helper method to swap the order of bytes in a 32 bit int |
|
117 |
* @param int |
|
118 |
* @return 32 bits swapped value |
|
119 |
*/ |
|
18215 | 120 |
final int big2little(int i) { |
2 | 121 |
|
122 |
int b1, b2, b3, b4 ; |
|
123 |
||
124 |
b1 = ( i & 0xFF ) << 24 ; |
|
125 |
b2 = ( i & 0xFF00 ) << 8; |
|
126 |
b3 = ( i & 0xFF0000 ) >> 8; |
|
127 |
b4 = ( i & 0xFF000000 ) >>> 24; |
|
128 |
||
129 |
i = ( b1 | b2 | b3 | b4 ); |
|
130 |
||
131 |
return i; |
|
132 |
} |
|
133 |
||
134 |
/** |
|
135 |
* rlshort |
|
136 |
* Protected helper method to read 16 bits value. Swap high with low byte. |
|
137 |
* @param DataInputStream |
|
138 |
* @return the swapped value. |
|
139 |
* @exception IOException |
|
140 |
*/ |
|
18215 | 141 |
final short rlshort(DataInputStream dis) throws IOException { |
2 | 142 |
|
143 |
short s=0; |
|
144 |
short high, low; |
|
145 |
||
146 |
s = dis.readShort(); |
|
147 |
||
148 |
high = (short)(( s & 0xFF ) << 8) ; |
|
149 |
low = (short)(( s & 0xFF00 ) >>> 8); |
|
150 |
||
151 |
s = (short)( high | low ); |
|
152 |
||
153 |
return s; |
|
154 |
} |
|
155 |
||
156 |
/** |
|
157 |
* big2little |
|
158 |
* Protected helper method to swap the order of bytes in a 16 bit short |
|
159 |
* @param int |
|
160 |
* @return 16 bits swapped value |
|
161 |
*/ |
|
18215 | 162 |
final short big2littleShort(short i) { |
2 | 163 |
|
164 |
short high, low; |
|
165 |
||
166 |
high = (short)(( i & 0xFF ) << 8) ; |
|
167 |
low = (short)(( i & 0xFF00 ) >>> 8); |
|
168 |
||
169 |
i = (short)( high | low ); |
|
170 |
||
171 |
return i; |
|
172 |
} |
|
173 |
||
8525
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
174 |
/** |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
175 |
* InputStream wrapper class which prevent source stream from being closed. |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
176 |
* The class is usefull for use with SequenceInputStream to prevent |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
177 |
* closing of the source input streams. |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
178 |
*/ |
18215 | 179 |
final class NoCloseInputStream extends InputStream { |
8525
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
180 |
private final InputStream in; |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
181 |
|
18215 | 182 |
NoCloseInputStream(InputStream in) { |
8525
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
183 |
this.in = in; |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
184 |
} |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
185 |
|
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
186 |
@Override |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
187 |
public int read() throws IOException { |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
188 |
return in.read(); |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
189 |
} |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
190 |
|
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
191 |
@Override |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
192 |
public int read(byte b[]) throws IOException { |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
193 |
return in.read(b); |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
194 |
} |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
195 |
|
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
196 |
@Override |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
197 |
public int read(byte b[], int off, int len) throws IOException { |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
198 |
return in.read(b, off, len); |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
199 |
} |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
200 |
|
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
201 |
@Override |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
202 |
public long skip(long n) throws IOException { |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
203 |
return in.skip(n); |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
204 |
} |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
205 |
|
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
206 |
@Override |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
207 |
public int available() throws IOException { |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
208 |
return in.available(); |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
209 |
} |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
210 |
|
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
211 |
@Override |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
212 |
public void close() throws IOException { |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
213 |
// don't propagate the call |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
214 |
} |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
215 |
|
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
216 |
@Override |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
217 |
public void mark(int readlimit) { |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
218 |
in.mark(readlimit); |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
219 |
} |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
220 |
|
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
221 |
@Override |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
222 |
public void reset() throws IOException { |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
223 |
in.reset(); |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
224 |
} |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
225 |
|
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
226 |
@Override |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
227 |
public boolean markSupported() { |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
228 |
return in.markSupported(); |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
229 |
} |
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
230 |
|
08f98f5a11df
7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents:
5506
diff
changeset
|
231 |
} |
2 | 232 |
} |