1 /*
   2  * Copyright (c) 1999, 2015, 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 
  43     // buffer size for write
  44     protected static final int bufferSize = 16384;
  45 
  46     // buffer size for temporary input streams
  47     protected static final int bisBufferSize = 4096;
  48 
  49 
  50     final AudioFileFormat.Type types[];
  51 
  52 
  53     /**
  54      * Constructs a new SunParser object.
  55      */
  56     SunFileWriter(AudioFileFormat.Type types[]) {
  57         this.types = types;
  58     }
  59 
  60 
  61 
  62     // METHODS TO IMPLEMENT AudioFileWriter
  63 
  64     // new, 10.27.99
  65 
  66     public final AudioFileFormat.Type[] getAudioFileTypes(){
  67         AudioFileFormat.Type[] localArray = new AudioFileFormat.Type[types.length];
  68         System.arraycopy(types, 0, localArray, 0, types.length);
  69         return localArray;
  70     }
  71 
  72     // HELPER METHODS
  73 
  74 
  75     /**
  76      * rllong
  77      * Protected helper method to read 64 bits and changing the order of
  78      * each bytes.
  79      * @param DataInputStream
  80      * @return 32 bits swapped value.
  81      * @exception IOException
  82      */
  83     final int rllong(DataInputStream dis) throws IOException {
  84 
  85         int b1, b2, b3, b4 ;
  86         int i = 0;
  87 
  88         i = dis.readInt();
  89 
  90         b1 = ( i & 0xFF ) << 24 ;
  91         b2 = ( i & 0xFF00 ) << 8;
  92         b3 = ( i & 0xFF0000 ) >> 8;
  93         b4 = ( i & 0xFF000000 ) >>> 24;
  94 
  95         i = ( b1 | b2 | b3 | b4 );
  96 
  97         return i;
  98     }
  99 
 100     /**
 101      * big2little
 102      * Protected helper method to swap the order of bytes in a 32 bit int
 103      * @param int
 104      * @return 32 bits swapped value
 105      */
 106     final int big2little(int i) {
 107 
 108         int b1, b2, b3, b4 ;
 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      * rlshort
 122      * Protected helper method to read 16 bits value. Swap high with low byte.
 123      * @param DataInputStream
 124      * @return the swapped value.
 125      * @exception IOException
 126      */
 127     final short rlshort(DataInputStream dis)  throws IOException {
 128 
 129         short s=0;
 130         short high, low;
 131 
 132         s = dis.readShort();
 133 
 134         high = (short)(( s & 0xFF ) << 8) ;
 135         low = (short)(( s & 0xFF00 ) >>> 8);
 136 
 137         s = (short)( high | low );
 138 
 139         return s;
 140     }
 141 
 142     /**
 143      * big2little
 144      * Protected helper method to swap the order of bytes in a 16 bit short
 145      * @param int
 146      * @return 16 bits swapped value
 147      */
 148     final short big2littleShort(short i) {
 149 
 150         short high, low;
 151 
 152         high = (short)(( i & 0xFF ) << 8) ;
 153         low = (short)(( i & 0xFF00 ) >>> 8);
 154 
 155         i = (short)( high | low );
 156 
 157         return i;
 158     }
 159 
 160     /**
 161      * InputStream wrapper class which prevent source stream from being closed.
 162      * The class is usefull for use with SequenceInputStream to prevent
 163      * closing of the source input streams.
 164      */
 165     final class NoCloseInputStream extends InputStream {
 166         private final InputStream in;
 167 
 168         NoCloseInputStream(InputStream in) {
 169             this.in = in;
 170         }
 171 
 172         @Override
 173         public int read() throws IOException {
 174             return in.read();
 175         }
 176 
 177         @Override
 178         public int read(byte b[]) throws IOException {
 179             return in.read(b);
 180         }
 181 
 182         @Override
 183         public int read(byte b[], int off, int len) throws IOException {
 184             return in.read(b, off, len);
 185         }
 186 
 187         @Override
 188         public long skip(long n) throws IOException {
 189             return in.skip(n);
 190         }
 191 
 192         @Override
 193         public int available() throws IOException {
 194             return in.available();
 195         }
 196 
 197         @Override
 198         public void close() throws IOException {
 199             // don't propagate the call
 200         }
 201 
 202         @Override
 203         public void mark(int readlimit) {
 204             in.mark(readlimit);
 205         }
 206 
 207         @Override
 208         public void reset() throws IOException {
 209             in.reset();
 210         }
 211 
 212         @Override
 213         public boolean markSupported() {
 214             return in.markSupported();
 215         }
 216 
 217     }
 218 }