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