2
|
1 |
/*
|
|
2 |
* Copyright 1999-2007 Sun Microsystems, Inc. All Rights Reserved.
|
|
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
|
|
7 |
* published by the Free Software Foundation. Sun designates this
|
|
8 |
* particular file as subject to the "Classpath" exception as provided
|
|
9 |
* by Sun in the LICENSE file that accompanied this code.
|
|
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 |
*
|
|
21 |
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
|
|
22 |
* CA 95054 USA or visit www.sun.com if you need additional information or
|
|
23 |
* have any questions.
|
|
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 |
import java.io.RandomAccessFile;
|
|
34 |
import java.net.URL;
|
|
35 |
|
|
36 |
import javax.sound.sampled.AudioFormat;
|
|
37 |
import javax.sound.sampled.AudioFileFormat;
|
|
38 |
import javax.sound.sampled.AudioInputStream;
|
|
39 |
import javax.sound.sampled.AudioSystem;
|
|
40 |
import javax.sound.sampled.UnsupportedAudioFileException;
|
|
41 |
import javax.sound.sampled.spi.AudioFileWriter;
|
|
42 |
|
|
43 |
|
|
44 |
|
|
45 |
|
|
46 |
/**
|
|
47 |
* Abstract File Writer class.
|
|
48 |
*
|
|
49 |
* @author Jan Borgersen
|
|
50 |
*/
|
|
51 |
abstract class SunFileWriter extends AudioFileWriter {
|
|
52 |
|
|
53 |
|
|
54 |
// buffer size for write
|
|
55 |
protected static final int bufferSize = 16384;
|
|
56 |
|
|
57 |
// buffer size for temporary input streams
|
|
58 |
protected static final int bisBufferSize = 4096;
|
|
59 |
|
|
60 |
|
|
61 |
final AudioFileFormat.Type types[];
|
|
62 |
|
|
63 |
|
|
64 |
/**
|
|
65 |
* Constructs a new SunParser object.
|
|
66 |
*/
|
|
67 |
SunFileWriter(AudioFileFormat.Type types[]) {
|
|
68 |
this.types = types;
|
|
69 |
}
|
|
70 |
|
|
71 |
|
|
72 |
|
|
73 |
// METHODS TO IMPLEMENT AudioFileWriter
|
|
74 |
|
|
75 |
// new, 10.27.99
|
|
76 |
|
|
77 |
public AudioFileFormat.Type[] getAudioFileTypes(){
|
|
78 |
|
|
79 |
AudioFileFormat.Type[] localArray = new AudioFileFormat.Type[types.length];
|
|
80 |
System.arraycopy(types, 0, localArray, 0, types.length);
|
|
81 |
return localArray;
|
|
82 |
}
|
|
83 |
|
|
84 |
|
|
85 |
public abstract AudioFileFormat.Type[] getAudioFileTypes(AudioInputStream stream);
|
|
86 |
|
|
87 |
public abstract int write(AudioInputStream stream, AudioFileFormat.Type fileType, OutputStream out) throws IOException;
|
|
88 |
|
|
89 |
public abstract int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException;
|
|
90 |
|
|
91 |
|
|
92 |
// HELPER METHODS
|
|
93 |
|
|
94 |
|
|
95 |
/**
|
|
96 |
* rllong
|
|
97 |
* Protected helper method to read 64 bits and changing the order of
|
|
98 |
* each bytes.
|
|
99 |
* @param DataInputStream
|
|
100 |
* @return 32 bits swapped value.
|
|
101 |
* @exception IOException
|
|
102 |
*/
|
|
103 |
protected int rllong(DataInputStream dis) throws IOException {
|
|
104 |
|
|
105 |
int b1, b2, b3, b4 ;
|
|
106 |
int i = 0;
|
|
107 |
|
|
108 |
i = dis.readInt();
|
|
109 |
|
|
110 |
b1 = ( i & 0xFF ) << 24 ;
|
|
111 |
b2 = ( i & 0xFF00 ) << 8;
|
|
112 |
b3 = ( i & 0xFF0000 ) >> 8;
|
|
113 |
b4 = ( i & 0xFF000000 ) >>> 24;
|
|
114 |
|
|
115 |
i = ( b1 | b2 | b3 | b4 );
|
|
116 |
|
|
117 |
return i;
|
|
118 |
}
|
|
119 |
|
|
120 |
/**
|
|
121 |
* big2little
|
|
122 |
* Protected helper method to swap the order of bytes in a 32 bit int
|
|
123 |
* @param int
|
|
124 |
* @return 32 bits swapped value
|
|
125 |
*/
|
|
126 |
protected int big2little(int i) {
|
|
127 |
|
|
128 |
int b1, b2, b3, b4 ;
|
|
129 |
|
|
130 |
b1 = ( i & 0xFF ) << 24 ;
|
|
131 |
b2 = ( i & 0xFF00 ) << 8;
|
|
132 |
b3 = ( i & 0xFF0000 ) >> 8;
|
|
133 |
b4 = ( i & 0xFF000000 ) >>> 24;
|
|
134 |
|
|
135 |
i = ( b1 | b2 | b3 | b4 );
|
|
136 |
|
|
137 |
return i;
|
|
138 |
}
|
|
139 |
|
|
140 |
/**
|
|
141 |
* rlshort
|
|
142 |
* Protected helper method to read 16 bits value. Swap high with low byte.
|
|
143 |
* @param DataInputStream
|
|
144 |
* @return the swapped value.
|
|
145 |
* @exception IOException
|
|
146 |
*/
|
|
147 |
protected short rlshort(DataInputStream dis) throws IOException {
|
|
148 |
|
|
149 |
short s=0;
|
|
150 |
short high, low;
|
|
151 |
|
|
152 |
s = dis.readShort();
|
|
153 |
|
|
154 |
high = (short)(( s & 0xFF ) << 8) ;
|
|
155 |
low = (short)(( s & 0xFF00 ) >>> 8);
|
|
156 |
|
|
157 |
s = (short)( high | low );
|
|
158 |
|
|
159 |
return s;
|
|
160 |
}
|
|
161 |
|
|
162 |
/**
|
|
163 |
* big2little
|
|
164 |
* Protected helper method to swap the order of bytes in a 16 bit short
|
|
165 |
* @param int
|
|
166 |
* @return 16 bits swapped value
|
|
167 |
*/
|
|
168 |
protected short big2littleShort(short i) {
|
|
169 |
|
|
170 |
short high, low;
|
|
171 |
|
|
172 |
high = (short)(( i & 0xFF ) << 8) ;
|
|
173 |
low = (short)(( i & 0xFF00 ) >>> 8);
|
|
174 |
|
|
175 |
i = (short)( high | low );
|
|
176 |
|
|
177 |
return i;
|
|
178 |
}
|
|
179 |
|
|
180 |
}
|