< prev index next >

src/java.desktop/share/classes/com/sun/media/sound/ModelByteBuffer.java

Print this page




   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 package com.sun.media.sound;
  26 
  27 import java.io.ByteArrayInputStream;
  28 import java.io.DataInputStream;
  29 import java.io.File;
  30 import java.io.IOException;
  31 import java.io.InputStream;
  32 import java.io.OutputStream;
  33 import java.io.RandomAccessFile;
  34 import java.util.Collection;
  35 
  36 /**
  37  * This class is a pointer to a binary array either in memory or on disk.
  38  *
  39  * @author Karl Helgason
  40  */
  41 public final class ModelByteBuffer {
  42 
  43     private ModelByteBuffer root = this;
  44     private File file;
  45     private long fileoffset;
  46     private byte[] buffer;
  47     private long offset;
  48     private final long len;
  49 
  50     private class RandomFileInputStream extends InputStream {
  51 
  52         private final RandomAccessFile raf;
  53         private long left;
  54         private long mark = 0;
  55         private long markleft = 0;
  56 
  57         RandomFileInputStream() throws IOException {
  58             raf = new RandomAccessFile(root.file, "r");
  59             raf.seek(root.fileoffset + arrayOffset());
  60             left = capacity();
  61         }
  62 

  63         public int available() throws IOException {
  64             if (left > Integer.MAX_VALUE)
  65                 return Integer.MAX_VALUE;
  66             return (int)left;
  67         }
  68 

  69         public synchronized void mark(int readlimit) {
  70             try {
  71                 mark = raf.getFilePointer();
  72                 markleft = left;
  73             } catch (IOException e) {
  74                 //e.printStackTrace();
  75             }
  76         }
  77 

  78         public boolean markSupported() {
  79             return true;
  80         }
  81 

  82         public synchronized void reset() throws IOException {
  83             raf.seek(mark);
  84             left = markleft;
  85         }
  86 

  87         public long skip(long n) throws IOException {
  88             if( n < 0)
  89                 return 0;
  90             if (n > left)
  91                 n = left;
  92             long p = raf.getFilePointer();
  93             raf.seek(p + n);
  94             left -= n;
  95             return n;
  96         }
  97 

  98         public int read(byte b[], int off, int len) throws IOException {
  99             if (len > left)
 100                 len = (int)left;
 101             if (left == 0)
 102                 return -1;
 103             len = raf.read(b, off, len);
 104             if (len == -1)
 105                 return -1;
 106             left -= len;
 107             return len;
 108         }
 109 

 110         public int read(byte[] b) throws IOException {
 111             int len = b.length;
 112             if (len > left)
 113                 len = (int)left;
 114             if (left == 0)
 115                 return -1;
 116             len = raf.read(b, 0, len);
 117             if (len == -1)
 118                 return -1;
 119             left -= len;
 120             return len;
 121         }
 122 

 123         public int read() throws IOException {
 124             if (left == 0)
 125                 return -1;
 126             int b = raf.read();
 127             if (b == -1)
 128                 return -1;
 129             left--;
 130             return b;
 131         }
 132 

 133         public void close() throws IOException {
 134             raf.close();
 135         }
 136     }
 137 
 138     private ModelByteBuffer(ModelByteBuffer parent,
 139             long beginIndex, long endIndex, boolean independent) {
 140         this.root = parent.root;
 141         this.offset = 0;
 142         long parent_len = parent.len;
 143         if (beginIndex < 0)
 144             beginIndex = 0;
 145         if (beginIndex > parent_len)
 146             beginIndex = parent_len;
 147         if (endIndex < 0)
 148             endIndex = 0;
 149         if (endIndex > parent_len)
 150             endIndex = parent_len;
 151         if (beginIndex > endIndex)
 152             beginIndex = endIndex;




   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.ByteArrayInputStream;
  29 import java.io.DataInputStream;
  30 import java.io.File;
  31 import java.io.IOException;
  32 import java.io.InputStream;
  33 import java.io.OutputStream;
  34 import java.io.RandomAccessFile;
  35 import java.util.Collection;
  36 
  37 /**
  38  * This class is a pointer to a binary array either in memory or on disk.
  39  *
  40  * @author Karl Helgason
  41  */
  42 public final class ModelByteBuffer {
  43 
  44     private ModelByteBuffer root = this;
  45     private File file;
  46     private long fileoffset;
  47     private byte[] buffer;
  48     private long offset;
  49     private final long len;
  50 
  51     private class RandomFileInputStream extends InputStream {
  52 
  53         private final RandomAccessFile raf;
  54         private long left;
  55         private long mark = 0;
  56         private long markleft = 0;
  57 
  58         RandomFileInputStream() throws IOException {
  59             raf = new RandomAccessFile(root.file, "r");
  60             raf.seek(root.fileoffset + arrayOffset());
  61             left = capacity();
  62         }
  63 
  64         @Override
  65         public int available() throws IOException {
  66             if (left > Integer.MAX_VALUE)
  67                 return Integer.MAX_VALUE;
  68             return (int)left;
  69         }
  70 
  71         @Override
  72         public synchronized void mark(int readlimit) {
  73             try {
  74                 mark = raf.getFilePointer();
  75                 markleft = left;
  76             } catch (IOException e) {
  77                 //e.printStackTrace();
  78             }
  79         }
  80 
  81         @Override
  82         public boolean markSupported() {
  83             return true;
  84         }
  85 
  86         @Override
  87         public synchronized void reset() throws IOException {
  88             raf.seek(mark);
  89             left = markleft;
  90         }
  91 
  92         @Override
  93         public long skip(long n) throws IOException {
  94             if( n < 0)
  95                 return 0;
  96             if (n > left)
  97                 n = left;
  98             long p = raf.getFilePointer();
  99             raf.seek(p + n);
 100             left -= n;
 101             return n;
 102         }
 103 
 104         @Override
 105         public int read(byte b[], int off, int len) throws IOException {
 106             if (len > left)
 107                 len = (int)left;
 108             if (left == 0)
 109                 return -1;
 110             len = raf.read(b, off, len);
 111             if (len == -1)
 112                 return -1;
 113             left -= len;
 114             return len;
 115         }
 116 
 117         @Override
 118         public int read(byte[] b) throws IOException {
 119             int len = b.length;
 120             if (len > left)
 121                 len = (int)left;
 122             if (left == 0)
 123                 return -1;
 124             len = raf.read(b, 0, len);
 125             if (len == -1)
 126                 return -1;
 127             left -= len;
 128             return len;
 129         }
 130 
 131         @Override
 132         public int read() throws IOException {
 133             if (left == 0)
 134                 return -1;
 135             int b = raf.read();
 136             if (b == -1)
 137                 return -1;
 138             left--;
 139             return b;
 140         }
 141 
 142         @Override
 143         public void close() throws IOException {
 144             raf.close();
 145         }
 146     }
 147 
 148     private ModelByteBuffer(ModelByteBuffer parent,
 149             long beginIndex, long endIndex, boolean independent) {
 150         this.root = parent.root;
 151         this.offset = 0;
 152         long parent_len = parent.len;
 153         if (beginIndex < 0)
 154             beginIndex = 0;
 155         if (beginIndex > parent_len)
 156             beginIndex = parent_len;
 157         if (endIndex < 0)
 158             endIndex = 0;
 159         if (endIndex > parent_len)
 160             endIndex = parent_len;
 161         if (beginIndex > endIndex)
 162             beginIndex = endIndex;


< prev index next >