Advertisement
Guest User

Untitled

a guest
Jan 7th, 2012
357
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.06 KB | None | 0 0
  1. /*
  2.  
  3.  * $ProjectName$
  4.  
  5.  * $ProjectRevision$
  6.  
  7.  * -----------------------------------------------------------
  8.  
  9.  * $Id: CachedUrlStream.java,v 1.1 2003/04/10 19:48:22 jarnbjo Exp $
  10.  
  11.  * -----------------------------------------------------------
  12.  
  13.  *
  14.  
  15.  * $Author: jarnbjo $
  16.  
  17.  *
  18.  
  19.  * Description:
  20.  
  21.  *
  22.  
  23.  * Copyright 2002-2003 Tor-Einar Jarnbjo
  24.  
  25.  * -----------------------------------------------------------
  26.  
  27.  *
  28.  
  29.  * Change History
  30.  
  31.  * -----------------------------------------------------------
  32.  
  33.  * $Log: CachedUrlStream.java,v $
  34.  
  35.  * Revision 1.1  2003/04/10 19:48:22  jarnbjo
  36.  
  37.  * no message
  38.  
  39.  *
  40.  
  41.  *
  42.  
  43.  */
  44.  
  45.  
  46.  
  47. package de.jarnbjo.ogg;
  48.  
  49.  
  50.  
  51. import java.io.*;
  52.  
  53. import java.net.*;
  54.  
  55. import java.util.*;
  56.  
  57.  
  58.  
  59. /**
  60.  
  61.  *  Implementation of the <code>PhysicalOggStream</code> interface for reading
  62.  
  63.  *  and caching an Ogg stream from a URL. This class reads the data as fast as
  64.  
  65.  *  possible from the URL, caches it locally either in memory or on disk, and
  66.  
  67.  *  supports seeking within the available data.
  68.  
  69.  */
  70.  
  71.  
  72.  
  73. public class CachedUrlStream implements PhysicalOggStream {
  74.  
  75.  
  76.  
  77.    private boolean closed=false;
  78.  
  79.    private URLConnection source;
  80.  
  81.    private InputStream sourceStream;
  82.  
  83.    private Object drainLock=new Object();
  84.  
  85.    private RandomAccessFile drain;
  86.  
  87.    private byte[] memoryCache;
  88.  
  89.    private ArrayList pageOffsets=new ArrayList();
  90.  
  91.    private ArrayList pageLengths=new ArrayList();
  92.  
  93.    private long numberOfSamples=-1;
  94.  
  95.    private long cacheLength;
  96.  
  97.  
  98.  
  99.    private HashMap logicalStreams=new HashMap();
  100.  
  101.  
  102.  
  103.    private LoaderThread loaderThread;
  104.  
  105.  
  106.  
  107.     /**
  108.  
  109.      *  Creates an instance of this class, using a memory cache.
  110.  
  111.      */
  112.  
  113.  
  114.  
  115.    public CachedUrlStream(URL source) throws OggFormatException, IOException {
  116.  
  117.       this(source, null);
  118.  
  119.    }
  120.  
  121.  
  122.  
  123.     /**
  124.  
  125.      *  Creates an instance of this class, using the specified file as cache. The
  126.  
  127.      *  file is not automatically deleted when this class is disposed.
  128.  
  129.      */
  130.  
  131.  
  132.  
  133.    public CachedUrlStream(URL source, RandomAccessFile drain) throws OggFormatException, IOException {
  134.  
  135.  
  136.  
  137.       this.source=source.openConnection();
  138.  
  139.  
  140.  
  141.       if(drain==null) {
  142.  
  143.          int contentLength=this.source.getContentLength();
  144.  
  145.          if(contentLength==-1) {
  146.  
  147.             throw new IOException("The URLConncetion's content length must be set when operating with a in-memory cache.");
  148.  
  149.          }
  150.  
  151.          memoryCache=new byte[contentLength];
  152.  
  153.       }
  154.  
  155.  
  156.  
  157.       this.drain=drain;
  158.  
  159.       this.sourceStream=this.source.getInputStream();
  160.  
  161.  
  162.  
  163.       loaderThread=new LoaderThread(sourceStream, drain, memoryCache);
  164.  
  165.       new Thread(loaderThread).start();
  166.  
  167.  
  168.  
  169.       while(!loaderThread.isBosDone() || pageOffsets.size()<5) {
  170.  
  171.          System.out.print("pageOffsets.size(): "+pageOffsets.size()+"\r");
  172.  
  173.          try {
  174.  
  175.             Thread.sleep(5);
  176.  
  177.          }
  178.  
  179.          catch (InterruptedException ex) {
  180.  
  181.          }
  182.  
  183.       }
  184.  
  185.       System.out.println();
  186.  
  187.       System.out.println("caching "+pageOffsets.size()+"/20 pages\r");
  188.  
  189.    }
  190.  
  191.  
  192.  
  193.    public Collection getLogicalStreams() {
  194.  
  195.       return logicalStreams.values();
  196.  
  197.    }
  198.  
  199.  
  200.  
  201.    public boolean isOpen() {
  202.  
  203.       return !closed;
  204.  
  205.    }
  206.  
  207.  
  208.  
  209.    public void close() throws IOException {
  210.  
  211.       closed=true;
  212.  
  213.       sourceStream.close();
  214.  
  215.    }
  216.  
  217.  
  218.  
  219.    public long getCacheLength() {
  220.  
  221.       return cacheLength;
  222.  
  223.    }
  224.  
  225.  
  226.  
  227.    /*
  228.  
  229.    private OggPage getNextPage() throws EndOfOggStreamException, IOException, OggFormatException  {
  230.  
  231.       return getNextPage(false);
  232.  
  233.    }
  234.  
  235.  
  236.  
  237.    private OggPage getNextPage(boolean skipData) throws EndOfOggStreamException, IOException, OggFormatException  {
  238.  
  239.       return OggPage.create(sourceStream, skipData);
  240.  
  241.    }
  242.  
  243.    */
  244.  
  245.  
  246.  
  247.    public OggPage getOggPage(int index) throws IOException {
  248.  
  249.       synchronized(drainLock) {
  250.  
  251.          Long offset=(Long)pageOffsets.get(index);
  252.  
  253.          Long length=(Long)pageLengths.get(index);
  254.  
  255.          if(offset!=null) {
  256.  
  257.             if(drain!=null) {
  258.  
  259.                drain.seek(offset.longValue());
  260.  
  261.                return OggPage.create(drain);
  262.  
  263.             }
  264.  
  265.             else {
  266.  
  267.                byte[] tmpArray=new byte[length.intValue()];
  268.  
  269.                System.arraycopy(memoryCache, offset.intValue(), tmpArray, 0, length.intValue());
  270.  
  271.                return OggPage.create(tmpArray);
  272.  
  273.             }
  274.  
  275.          }
  276.  
  277.          else {
  278.  
  279.             return null;
  280.  
  281.          }
  282.  
  283.       }
  284.  
  285.    }
  286.  
  287.  
  288.  
  289.    private LogicalOggStream getLogicalStream(int serialNumber) {
  290.  
  291.       return (LogicalOggStream)logicalStreams.get(new Integer(serialNumber));
  292.  
  293.    }
  294.  
  295.  
  296.  
  297.    public void setTime(long granulePosition) throws IOException {
  298.  
  299.       for(Iterator iter=logicalStreams.values().iterator(); iter.hasNext(); ) {
  300.  
  301.          LogicalOggStream los=(LogicalOggStream)iter.next();
  302.  
  303.          los.setTime(granulePosition);
  304.  
  305.       }
  306.  
  307.    }
  308.  
  309.  
  310.  
  311.    public class LoaderThread implements Runnable {
  312.  
  313.  
  314.  
  315.       private InputStream source;
  316.  
  317.       private RandomAccessFile drain;
  318.  
  319.       private byte[] memoryCache;
  320.  
  321.  
  322.  
  323.       private boolean bosDone=false;
  324.  
  325.  
  326.  
  327.       private int pageNumber;
  328.  
  329.  
  330.  
  331.       public LoaderThread(InputStream source, RandomAccessFile drain, byte[] memoryCache) {
  332.  
  333.          this.source=source;
  334.  
  335.          this.drain=drain;
  336.  
  337.          this.memoryCache=memoryCache;
  338.  
  339.       }
  340.  
  341.  
  342.  
  343.       public void run() {
  344.  
  345.          try {
  346.  
  347.             boolean eos=false;
  348.  
  349.             byte[] buffer=new byte[8192];
  350.  
  351.             while(!eos) {
  352.  
  353.                OggPage op=OggPage.create(source);
  354.  
  355.                synchronized (drainLock) {
  356.  
  357.                   int listSize=pageOffsets.size();
  358.  
  359.  
  360.  
  361.                   long pos=
  362.  
  363.                      listSize>0?
  364.  
  365.                         ((Long)pageOffsets.get(listSize-1)).longValue()+
  366.  
  367.                         ((Long)pageLengths.get(listSize-1)).longValue():
  368.  
  369.                         0;
  370.  
  371.  
  372.  
  373.                   byte[] arr1=op.getHeader();
  374.  
  375.                   byte[] arr2=op.getSegmentTable();
  376.  
  377.                   byte[] arr3=op.getData();
  378.  
  379.  
  380.  
  381.                   if(drain!=null) {
  382.  
  383.                      drain.seek(pos);
  384.  
  385.                      drain.write(arr1);
  386.  
  387.                      drain.write(arr2);
  388.  
  389.                      drain.write(arr3);
  390.  
  391.                   }
  392.  
  393.                   else {
  394.  
  395.                      System.arraycopy(arr1, 0, memoryCache, (int)pos, arr1.length);
  396.  
  397.                      System.arraycopy(arr2, 0, memoryCache, (int)pos+arr1.length, arr2.length);
  398.  
  399.                      System.arraycopy(arr3, 0, memoryCache, (int)pos+arr1.length+arr2.length, arr3.length);
  400.  
  401.                   }
  402.  
  403.  
  404.  
  405.                   pageOffsets.add(new Long(pos));
  406.  
  407.                   pageLengths.add(new Long(arr1.length+arr2.length+arr3.length));
  408.  
  409.                }
  410.  
  411.  
  412.  
  413.                if(!op.isBos()) {
  414.  
  415.                   bosDone=true;
  416.  
  417.                   //System.out.println("bosDone=true;");
  418.  
  419.                }
  420.  
  421.                if(op.isEos()) {
  422.  
  423.                   eos=true;
  424.  
  425.                }
  426.  
  427.  
  428.  
  429.                LogicalOggStreamImpl los=(LogicalOggStreamImpl)getLogicalStream(op.getStreamSerialNumber());
  430.  
  431.                if(los==null) {
  432.  
  433.                   los=new LogicalOggStreamImpl(CachedUrlStream.this, op.getStreamSerialNumber());
  434.  
  435.                   logicalStreams.put(new Integer(op.getStreamSerialNumber()), los);
  436.  
  437.                   los.checkFormat(op);
  438.  
  439.                }
  440.  
  441.  
  442.  
  443.                los.addPageNumberMapping(pageNumber);
  444.  
  445.                los.addGranulePosition(op.getAbsoluteGranulePosition());
  446.  
  447.  
  448.  
  449.                pageNumber++;
  450.  
  451.                cacheLength=op.getAbsoluteGranulePosition();
  452.  
  453.                //System.out.println("read page: "+pageNumber);
  454.  
  455.             }
  456.  
  457.          }
  458.  
  459.          catch(EndOfOggStreamException e) {
  460.  
  461.             // ok
  462.  
  463.          }
  464.  
  465.          catch(IOException e) {
  466.  
  467.             e.printStackTrace();
  468.  
  469.          }
  470.  
  471.       }
  472.  
  473.  
  474.  
  475.       public boolean isBosDone() {
  476.  
  477.          return bosDone;
  478.  
  479.       }
  480.  
  481.    }
  482.  
  483.  
  484.  
  485.    public boolean isSeekable() {
  486.  
  487.       return true;
  488.  
  489.    }
  490.  
  491. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement