src/share/classes/com/sun/media/sound/StandardMidiFileWriter.java

Print this page


   1 /*
   2  * Copyright (c) 1999, 2013, 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


 124         return false;
 125     }
 126 
 127     public int write(Sequence in, int type, OutputStream out) throws IOException {
 128         byte [] buffer = null;
 129 
 130         int bytesRead = 0;
 131         long bytesWritten = 0;
 132 
 133         if( !isFileTypeSupported(type,in) ) {
 134             throw new IllegalArgumentException("Could not write MIDI file");
 135         }
 136         // First get the fileStream from this sequence
 137         InputStream fileStream = getFileStream(type,in);
 138         if (fileStream == null) {
 139             throw new IllegalArgumentException("Could not write MIDI file");
 140         }
 141         buffer = new byte[bufferSize];
 142 
 143         while( (bytesRead = fileStream.read( buffer )) >= 0 ) {
 144             out.write( buffer, 0, (int)bytesRead );
 145             bytesWritten += bytesRead;
 146         }
 147         // Done....return bytesWritten
 148         return (int) bytesWritten;
 149     }
 150 
 151     public int write(Sequence in, int type, File out) throws IOException {
 152         FileOutputStream fos = new FileOutputStream(out); // throws IOException
 153         int bytesWritten = write( in, type, fos );
 154         fos.close();
 155         return bytesWritten;
 156     }
 157 
 158     //=================================================================================
 159 
 160 
 161     private InputStream getFileStream(int type, Sequence sequence) throws IOException {
 162         Track tracks[] = sequence.getTracks();
 163         int bytesBuilt = 0;
 164         int headerLength = 14;


   1 /*
   2  * Copyright (c) 1999, 2014, 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


 124         return false;
 125     }
 126 
 127     public int write(Sequence in, int type, OutputStream out) throws IOException {
 128         byte [] buffer = null;
 129 
 130         int bytesRead = 0;
 131         long bytesWritten = 0;
 132 
 133         if( !isFileTypeSupported(type,in) ) {
 134             throw new IllegalArgumentException("Could not write MIDI file");
 135         }
 136         // First get the fileStream from this sequence
 137         InputStream fileStream = getFileStream(type,in);
 138         if (fileStream == null) {
 139             throw new IllegalArgumentException("Could not write MIDI file");
 140         }
 141         buffer = new byte[bufferSize];
 142 
 143         while( (bytesRead = fileStream.read( buffer )) >= 0 ) {
 144             out.write( buffer, 0, bytesRead );
 145             bytesWritten += bytesRead;
 146         }
 147         // Done....return bytesWritten
 148         return (int) bytesWritten;
 149     }
 150 
 151     public int write(Sequence in, int type, File out) throws IOException {
 152         FileOutputStream fos = new FileOutputStream(out); // throws IOException
 153         int bytesWritten = write( in, type, fos );
 154         fos.close();
 155         return bytesWritten;
 156     }
 157 
 158     //=================================================================================
 159 
 160 
 161     private InputStream getFileStream(int type, Sequence sequence) throws IOException {
 162         Track tracks[] = sequence.getTracks();
 163         int bytesBuilt = 0;
 164         int headerLength = 14;