1 /*
2  * Copyright (c) 1999, 2018, Oracle and/or its affiliates. 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.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle 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 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.
24  */
25 
26 package com.sun.media.sound;
27 
28 import java.io.DataInputStream;
29 import java.io.IOException;
30 import java.io.InputStream;
31 
32 import javax.sound.sampled.AudioFileFormat;
33 import javax.sound.sampled.spi.AudioFileWriter;
34 
35 /**
36  * Abstract File Writer class.
37  *
38  * @author Jan Borgersen
39  */
40 abstract class SunFileWriter extends AudioFileWriter {
41 
42     // buffer size for write
43     protected static final int bufferSize = 16384;
44 
45     // buffer size for temporary input streams
46     protected static final int bisBufferSize = 4096;
47 
48     final AudioFileFormat.Type[] types;
49 
50     /**
51      * Constructs a new SunParser object.
52      */
SunFileWriter(AudioFileFormat.Type[] types)53     SunFileWriter(AudioFileFormat.Type[] types) {
54         this.types = types;
55     }
56 
57     // METHODS TO IMPLEMENT AudioFileWriter
58 
59     @Override
getAudioFileTypes()60     public final AudioFileFormat.Type[] getAudioFileTypes(){
61         AudioFileFormat.Type[] localArray = new AudioFileFormat.Type[types.length];
62         System.arraycopy(types, 0, localArray, 0, types.length);
63         return localArray;
64     }
65 
66     // HELPER METHODS
67 
68     /**
69      * rllong
70      * Protected helper method to read 64 bits and changing the order of
71      * each bytes.
72      * @return 32 bits swapped value.
73      * @exception IOException
74      */
rllong(DataInputStream dis)75     final int rllong(DataInputStream dis) throws IOException {
76 
77         int b1, b2, b3, b4 ;
78         int i = 0;
79 
80         i = dis.readInt();
81 
82         b1 = ( i & 0xFF ) << 24 ;
83         b2 = ( i & 0xFF00 ) << 8;
84         b3 = ( i & 0xFF0000 ) >> 8;
85         b4 = ( i & 0xFF000000 ) >>> 24;
86 
87         i = ( b1 | b2 | b3 | b4 );
88 
89         return i;
90     }
91 
92     /**
93      * big2little
94      * Protected helper method to swap the order of bytes in a 32 bit int
95      * @return 32 bits swapped value
96      */
big2little(int i)97     final int big2little(int i) {
98 
99         int b1, b2, b3, b4 ;
100 
101         b1 = ( i & 0xFF ) << 24 ;
102         b2 = ( i & 0xFF00 ) << 8;
103         b3 = ( i & 0xFF0000 ) >> 8;
104         b4 = ( i & 0xFF000000 ) >>> 24;
105 
106         i = ( b1 | b2 | b3 | b4 );
107 
108         return i;
109     }
110 
111     /**
112      * rlshort
113      * Protected helper method to read 16 bits value. Swap high with low byte.
114      * @return the swapped value.
115      * @exception IOException
116      */
rlshort(DataInputStream dis)117     final short rlshort(DataInputStream dis)  throws IOException {
118 
119         short s=0;
120         short high, low;
121 
122         s = dis.readShort();
123 
124         high = (short)(( s & 0xFF ) << 8) ;
125         low = (short)(( s & 0xFF00 ) >>> 8);
126 
127         s = (short)( high | low );
128 
129         return s;
130     }
131 
132     /**
133      * big2little
134      * Protected helper method to swap the order of bytes in a 16 bit short
135      * @return 16 bits swapped value
136      */
big2littleShort(short i)137     final short big2littleShort(short i) {
138 
139         short high, low;
140 
141         high = (short)(( i & 0xFF ) << 8) ;
142         low = (short)(( i & 0xFF00 ) >>> 8);
143 
144         i = (short)( high | low );
145 
146         return i;
147     }
148 
149     /**
150      * InputStream wrapper class which prevent source stream from being closed.
151      * The class is usefull for use with SequenceInputStream to prevent
152      * closing of the source input streams.
153      */
154     final class NoCloseInputStream extends InputStream {
155         private final InputStream in;
156 
NoCloseInputStream(InputStream in)157         NoCloseInputStream(InputStream in) {
158             this.in = in;
159         }
160 
161         @Override
read()162         public int read() throws IOException {
163             return in.read();
164         }
165 
166         @Override
read(byte[] b)167         public int read(byte[] b) throws IOException {
168             return in.read(b);
169         }
170 
171         @Override
read(byte[] b, int off, int len)172         public int read(byte[] b, int off, int len) throws IOException {
173             return in.read(b, off, len);
174         }
175 
176         @Override
skip(long n)177         public long skip(long n) throws IOException {
178             return in.skip(n);
179         }
180 
181         @Override
available()182         public int available() throws IOException {
183             return in.available();
184         }
185 
186         @Override
close()187         public void close() throws IOException {
188             // don't propagate the call
189         }
190 
191         @Override
mark(int readlimit)192         public void mark(int readlimit) {
193             in.mark(readlimit);
194         }
195 
196         @Override
reset()197         public void reset() throws IOException {
198             in.reset();
199         }
200 
201         @Override
markSupported()202         public boolean markSupported() {
203             return in.markSupported();
204         }
205     }
206 }
207