jdk/src/share/classes/com/sun/media/sound/AiffFileWriter.java
author ohair
Wed, 06 Apr 2011 22:06:11 -0700
changeset 9035 1255eb81cc2f
parent 8525 08f98f5a11df
child 18215 b2afd66ce6db
permissions -rw-r--r--
7033660: Update copyright year to 2011 on any files changed in 2011 Reviewed-by: dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
9035
1255eb81cc2f 7033660: Update copyright year to 2011 on any files changed in 2011
ohair
parents: 8525
diff changeset
     2
 * Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package com.sun.media.sound;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.io.File;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.InputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.OutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.io.IOException;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import java.io.BufferedOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
import java.io.DataOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
import java.io.FileOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
import java.io.ByteArrayInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
import java.io.ByteArrayOutputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
import java.io.RandomAccessFile;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
import java.io.SequenceInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
import javax.sound.sampled.AudioFileFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
import javax.sound.sampled.AudioInputStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
import javax.sound.sampled.AudioFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
import javax.sound.sampled.AudioSystem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
//$$fb this class is buggy. Should be replaced in future.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
 * AIFF file writer.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
 * @author Jan Borgersen
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
public class AiffFileWriter extends SunFileWriter {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
     * AIFF type
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
    private static final AudioFileFormat.Type aiffTypes[] = {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        AudioFileFormat.Type.AIFF
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
    };
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
     * Constructs a new AiffFileWriter object.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    public AiffFileWriter() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        super(aiffTypes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    // METHODS TO IMPLEMENT AudioFileWriter
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    public AudioFileFormat.Type[] getAudioFileTypes(AudioInputStream stream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        AudioFileFormat.Type[] filetypes = new AudioFileFormat.Type[types.length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
        System.arraycopy(types, 0, filetypes, 0, types.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
        // make sure we can write this stream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        AudioFormat format = stream.getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        AudioFormat.Encoding encoding = format.getEncoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
        if( (AudioFormat.Encoding.ALAW.equals(encoding)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
            (AudioFormat.Encoding.ULAW.equals(encoding)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
            (AudioFormat.Encoding.PCM_SIGNED.equals(encoding)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            (AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding)) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
            return filetypes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
        return new AudioFileFormat.Type[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
    public int write(AudioInputStream stream, AudioFileFormat.Type fileType, OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
        //$$fb the following check must come first ! Otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        // the next frame length check may throw an IOException and
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        // interrupt iterating File Writers. (see bug 4351296)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        // throws IllegalArgumentException if not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
        AiffFileFormat aiffFileFormat = (AiffFileFormat)getAudioFileFormat(fileType, stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        // we must know the total data length to calculate the file length
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
        if( stream.getFrameLength() == AudioSystem.NOT_SPECIFIED ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
            throw new IOException("stream length not specified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
        int bytesWritten = writeAiffFile(stream, aiffFileFormat, out);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
        return bytesWritten;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    public int write(AudioInputStream stream, AudioFileFormat.Type fileType, File out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
        // throws IllegalArgumentException if not supported
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
        AiffFileFormat aiffFileFormat = (AiffFileFormat)getAudioFileFormat(fileType, stream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        // first write the file without worrying about length fields
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        FileOutputStream fos = new FileOutputStream( out );     // throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        BufferedOutputStream bos = new BufferedOutputStream( fos, bisBufferSize );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        int bytesWritten = writeAiffFile(stream, aiffFileFormat, bos );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        bos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        // now, if length fields were not specified, calculate them,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        // open as a random access file, write the appropriate fields,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
        // close again....
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
        if( aiffFileFormat.getByteLength()== AudioSystem.NOT_SPECIFIED ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            // $$kk: 10.22.99: jan: please either implement this or throw an exception!
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
            // $$fb: 2001-07-13: done. Fixes Bug 4479981
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            int ssndBlockSize           = (aiffFileFormat.getFormat().getChannels() * aiffFileFormat.getFormat().getSampleSizeInBits());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
            int aiffLength=bytesWritten;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
            int ssndChunkSize=aiffLength-aiffFileFormat.getHeaderSize()+16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
            long dataSize=ssndChunkSize-16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
            int numFrames=(int) (dataSize*8/ssndBlockSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
            RandomAccessFile raf=new RandomAccessFile(out, "rw");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
            // skip FORM magic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
            raf.skipBytes(4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            raf.writeInt(aiffLength-8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            // skip aiff2 magic, fver chunk, comm magic, comm size, channel count,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
            raf.skipBytes(4+aiffFileFormat.getFverChunkSize()+4+4+2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
            // write frame count
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
            raf.writeInt(numFrames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
            // skip sample size, samplerate, SSND magic
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
            raf.skipBytes(2+10+4);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
            raf.writeInt(ssndChunkSize-8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
            // that's all
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
            raf.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        return bytesWritten;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    // -----------------------------------------------------------------------
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
     * Returns the AudioFileFormat describing the file that will be written from this AudioInputStream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
     * Throws IllegalArgumentException if not supported.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
    private AudioFileFormat getAudioFileFormat(AudioFileFormat.Type type, AudioInputStream stream) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
        AudioFormat format = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        AiffFileFormat fileFormat = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        AudioFormat.Encoding encoding = AudioFormat.Encoding.PCM_SIGNED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        AudioFormat streamFormat = stream.getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        AudioFormat.Encoding streamEncoding = streamFormat.getEncoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        float sampleRate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
        int sampleSizeInBits;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
        int channels;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
        int frameSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
        float frameRate;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
        int fileSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
        boolean convert8to16 = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if( !types[0].equals(type) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            throw new IllegalArgumentException("File type " + type + " not supported.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
        if( (AudioFormat.Encoding.ALAW.equals(streamEncoding)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
            (AudioFormat.Encoding.ULAW.equals(streamEncoding)) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            if( streamFormat.getSampleSizeInBits()==8 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                encoding = AudioFormat.Encoding.PCM_SIGNED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                sampleSizeInBits=16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
                convert8to16 = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
                // can't convert non-8-bit ALAW,ULAW
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
                throw new IllegalArgumentException("Encoding " + streamEncoding + " supported only for 8-bit data.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
        } else if ( streamFormat.getSampleSizeInBits()==8 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
            encoding = AudioFormat.Encoding.PCM_UNSIGNED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
            sampleSizeInBits=8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            encoding = AudioFormat.Encoding.PCM_SIGNED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
            sampleSizeInBits=streamFormat.getSampleSizeInBits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        format = new AudioFormat( encoding,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
                                  streamFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                                  sampleSizeInBits,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                                  streamFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                                  streamFormat.getFrameSize(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
                                  streamFormat.getFrameRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
                                  true);        // AIFF is big endian
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        if( stream.getFrameLength()!=AudioSystem.NOT_SPECIFIED ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
            if( convert8to16 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
                fileSize = (int)stream.getFrameLength()*streamFormat.getFrameSize()*2 + AiffFileFormat.AIFF_HEADERSIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
                fileSize = (int)stream.getFrameLength()*streamFormat.getFrameSize() + AiffFileFormat.AIFF_HEADERSIZE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
            fileSize = AudioSystem.NOT_SPECIFIED;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
        fileFormat = new AiffFileFormat( AudioFileFormat.Type.AIFF,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
                                         fileSize,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                                         format,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
                                         (int)stream.getFrameLength() );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
        return fileFormat;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
    private int writeAiffFile(InputStream in, AiffFileFormat aiffFileFormat, OutputStream out) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        int bytesRead = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        int bytesWritten = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
        InputStream fileStream = getFileStream(aiffFileFormat, in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
        byte buffer[] = new byte[bisBufferSize];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
        int maxLength = aiffFileFormat.getByteLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
        while( (bytesRead = fileStream.read( buffer )) >= 0 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
            if (maxLength>0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
                if( bytesRead < maxLength ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
                    out.write( buffer, 0, (int)bytesRead );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
                    bytesWritten += bytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
                    maxLength -= bytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
                } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                    out.write( buffer, 0, (int)maxLength );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                    bytesWritten += maxLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                    maxLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
                out.write( buffer, 0, (int)bytesRead );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
                bytesWritten += bytesRead;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        return bytesWritten;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    private InputStream getFileStream(AiffFileFormat aiffFileFormat, InputStream audioStream) throws IOException  {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
        // private method ... assumes aiffFileFormat is a supported file format
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
        AudioFormat format = aiffFileFormat.getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
        AudioFormat streamFormat = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
        AudioFormat.Encoding encoding = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        //$$fb a little bit nicer handling of constants
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        //int headerSize          = 54;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        int headerSize          = aiffFileFormat.getHeaderSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        //int fverChunkSize       = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        int fverChunkSize       = aiffFileFormat.getFverChunkSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        //int commChunkSize       = 26;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
        int commChunkSize       = aiffFileFormat.getCommChunkSize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
        int aiffLength          = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
        int ssndChunkSize       = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
        //int ssndOffset                        = headerSize - 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
        int ssndOffset                  = aiffFileFormat.getSsndChunkOffset();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
        short channels = (short) format.getChannels();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
        short sampleSize = (short) format.getSampleSizeInBits();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
        int ssndBlockSize               = (channels * sampleSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
        int numFrames                   = aiffFileFormat.getFrameLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
        long dataSize            = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
        if( numFrames != AudioSystem.NOT_SPECIFIED) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
            dataSize = (long) numFrames * ssndBlockSize / 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
            ssndChunkSize = (int)dataSize + 16;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            aiffLength = (int)dataSize+headerSize;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
        float sampleFramesPerSecond = format.getSampleRate();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
        int compCode = AiffFileFormat.AIFC_PCM;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
        byte header[] = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
        ByteArrayInputStream headerStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
        ByteArrayOutputStream baos = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
        DataOutputStream dos = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
        SequenceInputStream aiffStream = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        InputStream codedAudioStream = audioStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
        // if we need to do any format conversion, do it here....
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
        if( audioStream instanceof AudioInputStream ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            streamFormat = ((AudioInputStream)audioStream).getFormat();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
            encoding = streamFormat.getEncoding();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
            // $$jb: Note that AIFF samples are ALWAYS signed
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
            if( (AudioFormat.Encoding.PCM_UNSIGNED.equals(encoding)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                ( (AudioFormat.Encoding.PCM_SIGNED.equals(encoding)) && !streamFormat.isBigEndian() ) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                // plug in the transcoder to convert to PCM_SIGNED. big endian
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                codedAudioStream = AudioSystem.getAudioInputStream( new AudioFormat (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                                                                                     AudioFormat.Encoding.PCM_SIGNED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                                                                                     streamFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                                                                                     streamFormat.getSampleSizeInBits(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                                                                                     streamFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
                                                                                     streamFormat.getFrameSize(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                                                                                     streamFormat.getFrameRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                                                                                     true ),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                                                                    (AudioInputStream)audioStream );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
            } else if( (AudioFormat.Encoding.ULAW.equals(encoding)) ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
                       (AudioFormat.Encoding.ALAW.equals(encoding)) ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
                if( streamFormat.getSampleSizeInBits() != 8 ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
                    throw new IllegalArgumentException("unsupported encoding");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
                                //$$fb 2001-07-13: this is probably not what we want:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
                                //     writing PCM when ULAW/ALAW is requested. AIFC is able to write ULAW !
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
                                // plug in the transcoder to convert to PCM_SIGNED_BIG_ENDIAN
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
                codedAudioStream = AudioSystem.getAudioInputStream( new AudioFormat (
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
                                                                                     AudioFormat.Encoding.PCM_SIGNED,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
                                                                                     streamFormat.getSampleRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
                                                                                     streamFormat.getSampleSizeInBits() * 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
                                                                                     streamFormat.getChannels(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
                                                                                     streamFormat.getFrameSize() * 2,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
                                                                                     streamFormat.getFrameRate(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
                                                                                     true ),
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
                                                                    (AudioInputStream)audioStream );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
        // Now create an AIFF stream header...
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        baos = new ByteArrayOutputStream();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        dos = new DataOutputStream(baos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        // Write the outer FORM chunk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        dos.writeInt(AiffFileFormat.AIFF_MAGIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        dos.writeInt( (aiffLength-8) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        dos.writeInt(AiffFileFormat.AIFF_MAGIC2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
        // Write a FVER chunk - only for AIFC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
        //dos.writeInt(FVER_MAGIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        //dos.writeInt( (fverChunkSize-8) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        //dos.writeInt(FVER_TIMESTAMP);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
        // Write a COMM chunk
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
        dos.writeInt(AiffFileFormat.COMM_MAGIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        dos.writeInt( (commChunkSize-8) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
        dos.writeShort(channels);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
        dos.writeInt(numFrames);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
        dos.writeShort(sampleSize);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
        write_ieee_extended(dos, sampleFramesPerSecond);   // 10 bytes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
        //Only for AIFC
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
        //dos.writeInt(compCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        //dos.writeInt(compCode);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        //dos.writeShort(0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        // Write the SSND chunk header
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        dos.writeInt(AiffFileFormat.SSND_MAGIC);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        dos.writeInt( (ssndChunkSize-8) );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        // ssndOffset and ssndBlockSize set to 0 upon
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        // recommendation in "Sound Manager" chapter in
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        // "Inside Macintosh Sound", pp 2-87  (from Babu)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        dos.writeInt(0);        // ssndOffset
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        dos.writeInt(0);        // ssndBlockSize
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        // Concat this with the audioStream and return it
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        dos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        header = baos.toByteArray();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        headerStream = new ByteArrayInputStream( header );
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
8525
08f98f5a11df 7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents: 5506
diff changeset
   399
        aiffStream = new SequenceInputStream(headerStream,
08f98f5a11df 7013521: AudioSystem.write for AIFF files closes source audio stream
amenkov
parents: 5506
diff changeset
   400
                            new NoCloseInputStream(codedAudioStream));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        return aiffStream;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
    // HELPER METHODS
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
    private static final int DOUBLE_MANTISSA_LENGTH = 52;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
    private static final int DOUBLE_EXPONENT_LENGTH = 11;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
    private static final long DOUBLE_SIGN_MASK     = 0x8000000000000000L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
    private static final long DOUBLE_EXPONENT_MASK = 0x7FF0000000000000L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
    private static final long DOUBLE_MANTISSA_MASK = 0x000FFFFFFFFFFFFFL;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
    private static final int DOUBLE_EXPONENT_OFFSET = 1023;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
    private static final int EXTENDED_EXPONENT_OFFSET = 16383;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
    private static final int EXTENDED_MANTISSA_LENGTH = 63;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
    private static final int EXTENDED_EXPONENT_LENGTH = 15;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
    private static final long EXTENDED_INTEGER_MASK = 0x8000000000000000L;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
     * Extended precision IEEE floating-point conversion routine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
     * @argument DataOutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
     * @argument double
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
     * @exception IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
    private void write_ieee_extended(DataOutputStream dos, float f) throws IOException {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
        /* The special cases NaN, Infinity and Zero are ignored, since
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
           they do not represent useful sample rates anyway.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
           Denormalized number aren't handled, too. Below, there is a cast
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
           from float to double. We hope that in this conversion,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
           numbers are normalized. Numbers that cannot be normalized are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
           ignored, too, as they, too, do not represent useful sample rates. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
        long doubleBits = Double.doubleToLongBits((double) f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
        long sign = (doubleBits & DOUBLE_SIGN_MASK)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
            >> (DOUBLE_EXPONENT_LENGTH + DOUBLE_MANTISSA_LENGTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
        long doubleExponent = (doubleBits & DOUBLE_EXPONENT_MASK)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
            >> DOUBLE_MANTISSA_LENGTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
        long doubleMantissa = doubleBits & DOUBLE_MANTISSA_MASK;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
        long extendedExponent = doubleExponent - DOUBLE_EXPONENT_OFFSET
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
            + EXTENDED_EXPONENT_OFFSET;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
        long extendedMantissa = doubleMantissa
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
            << (EXTENDED_MANTISSA_LENGTH - DOUBLE_MANTISSA_LENGTH);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
        long extendedSign = sign << EXTENDED_EXPONENT_LENGTH;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
        short extendedBits79To64 = (short) (extendedSign | extendedExponent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
        long extendedBits63To0 = EXTENDED_INTEGER_MASK | extendedMantissa;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
        dos.writeShort(extendedBits79To64);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        dos.writeLong(extendedBits63To0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
}