1 /*
   2  * Copyright (c) 1999, 2011, 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.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 
  72     public AudioFileFormat.Type[] getAudioFileTypes(){
  73 
  74         AudioFileFormat.Type[] localArray = new AudioFileFormat.Type[types.length];
  75         System.arraycopy(types, 0, localArray, 0, types.length);
  76         return localArray;
  77     }
  78 
  79 
  80     public abstract AudioFileFormat.Type[] getAudioFileTypes(AudioInputStream stream);
  81 
  82     public abstract int write(AudioInputStream stream, AudioFileFormat.Type fileType, OutputStream out) throws IOException;
  83 
  84     public abstract int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException;
  85 
  86 
  87     // HELPER METHODS
  88 
  89 
  90     /**
  91      * rllong
  92      * Protected helper method to read 64 bits and changing the order of
  93      * each bytes.
  94      * @param DataInputStream
  95      * @return 32 bits swapped value.
  96      * @exception IOException
  97      */
  98     protected int rllong(DataInputStream dis) throws IOException {
  99 
 100         int b1, b2, b3, b4 ;
 101         int i = 0;
 102 
 103         i = dis.readInt();
 104 
 105         b1 = ( i & 0xFF ) << 24 ;
 106         b2 = ( i & 0xFF00 ) << 8;
 107         b3 = ( i & 0xFF0000 ) >> 8;
 108         b4 = ( i & 0xFF000000 ) >>> 24;
 109 
 110         i = ( b1 | b2 | b3 | b4 );
 111 
 112         return i;
 113     }
 114 
 115     /**
 116      * big2little
 117      * Protected helper method to swap the order of bytes in a 32 bit int
 118      * @param int
 119      * @return 32 bits swapped value
 120      */
 121     protected int big2little(int i) {
 122 
 123         int b1, b2, b3, b4 ;
 124 
 125         b1 = ( i & 0xFF ) << 24 ;
 126         b2 = ( i & 0xFF00 ) << 8;
 127         b3 = ( i & 0xFF0000 ) >> 8;
 128         b4 = ( i & 0xFF000000 ) >>> 24;
 129 
 130         i = ( b1 | b2 | b3 | b4 );
 131 
 132         return i;
 133     }
 134 
 135     /**
 136      * rlshort
 137      * Protected helper method to read 16 bits value. Swap high with low byte.
 138      * @param DataInputStream
 139      * @return the swapped value.
 140      * @exception IOException
 141      */
 142     protected short rlshort(DataInputStream dis)  throws IOException {
 143 
 144         short s=0;
 145         short high, low;
 146 
 147         s = dis.readShort();
 148 
 149         high = (short)(( s & 0xFF ) << 8) ;
 150         low = (short)(( s & 0xFF00 ) >>> 8);
 151 
 152         s = (short)( high | low );
 153 
 154         return s;
 155     }
 156 
 157     /**
 158      * big2little
 159      * Protected helper method to swap the order of bytes in a 16 bit short
 160      * @param int
 161      * @return 16 bits swapped value
 162      */
 163     protected short big2littleShort(short i) {
 164 
 165         short high, low;
 166 
 167         high = (short)(( i & 0xFF ) << 8) ;
 168         low = (short)(( i & 0xFF00 ) >>> 8);
 169 
 170         i = (short)( high | low );
 171 
 172         return i;
 173     }
 174 
 175     /**
 176      * InputStream wrapper class which prevent source stream from being closed.
 177      * The class is usefull for use with SequenceInputStream to prevent
 178      * closing of the source input streams.
 179      */
 180     protected class NoCloseInputStream extends InputStream {
 181         private final InputStream in;
 182 
 183         public NoCloseInputStream(InputStream in) {
 184             this.in = in;
 185         }
 186 
 187         @Override
 188         public int read() throws IOException {
 189             return in.read();
 190         }
 191 
 192         @Override
 193         public int read(byte b[]) throws IOException {
 194             return in.read(b);
 195         }
 196 
 197         @Override
 198         public int read(byte b[], int off, int len) throws IOException {
 199             return in.read(b, off, len);
 200         }
 201 
 202         @Override
 203         public long skip(long n) throws IOException {
 204             return in.skip(n);
 205         }
 206 
 207         @Override
 208         public int available() throws IOException {
 209             return in.available();
 210         }
 211 
 212         @Override
 213         public void close() throws IOException {
 214             // don't propagate the call
 215         }
 216 
 217         @Override
 218         public void mark(int readlimit) {
 219             in.mark(readlimit);
 220         }
 221 
 222         @Override
 223         public void reset() throws IOException {
 224             in.reset();
 225         }
 226 
 227         @Override
 228         public boolean markSupported() {
 229             return in.markSupported();
 230         }
 231 
 232     }
 233 }