summaryrefslogtreecommitdiff
path: root/songdbj/de/jarnbjo/ogg/FileStream.java
blob: 5a526300bfd11774e8e41b6d41626c9f8f41e9a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
/*
 * $ProjectName$
 * $ProjectRevision$
 * -----------------------------------------------------------
 * $Id$
 * -----------------------------------------------------------
 *
 * $Author$
 *
 * Description:
 *
 * Copyright 2002-2003 Tor-Einar Jarnbjo
 * -----------------------------------------------------------
 *
 * Change History
 * -----------------------------------------------------------
 * $Log$
 * Revision 1.1  2005/07/11 15:42:36  hcl
 * Songdb java version, source. only 1.5 compatible
 *
 * Revision 1.1.1.1  2004/04/04 22:09:12  shred
 * First Import
 *
 * Revision 1.1  2003/04/10 19:48:22  jarnbjo
 * no message
 *
 *
 */

package de.jarnbjo.ogg;

import java.io.*;
import java.util.*;

/**
 * Implementation of the <code>PhysicalOggStream</code> interface for accessing
 * normal disk files.
 */

public class FileStream implements PhysicalOggStream {

   private boolean closed=false;
   private RandomAccessFile source;
   private long[] pageOffsets;
   private long numberOfSamples=-1;

   private HashMap logicalStreams=new HashMap();

   /**
    * Creates access to the specified file through the <code>PhysicalOggStream</code> interface.
    * The specified source file must have been opened for reading.
    *
    * @param source the file to read from
    *
    * @throws OggFormatException if the stream format is incorrect
    * @throws IOException if some other IO error occurs when reading the file
    */

   public FileStream(RandomAccessFile source) throws OggFormatException, IOException {
      this.source=source;

      ArrayList po=new ArrayList();
      int pageNumber=0;
      try {
         while(true) {
            po.add(new Long(this.source.getFilePointer()));

            // skip data if pageNumber>0
            OggPage op=getNextPage(pageNumber>0);
            if(op==null) {
               break;
            }

            LogicalOggStreamImpl los=(LogicalOggStreamImpl)getLogicalStream(op.getStreamSerialNumber());
            if(los==null) {
               los=new LogicalOggStreamImpl(this, op.getStreamSerialNumber());
               logicalStreams.put(new Integer(op.getStreamSerialNumber()), los);
            }

            if(pageNumber==0) {
               los.checkFormat(op);
            }

            los.addPageNumberMapping(pageNumber);
            los.addGranulePosition(op.getAbsoluteGranulePosition());

            if(pageNumber>0) {
               this.source.seek(this.source.getFilePointer()+op.getTotalLength());
            }

            pageNumber++;
         }
      }
      catch(EndOfOggStreamException e) {
         // ok
      }
      catch(IOException e) {
         throw e;
      }
      //System.out.println("pageNumber: "+pageNumber);
      this.source.seek(0L);
      pageOffsets=new long[po.size()];
      int i=0;
      Iterator iter=po.iterator();
      while(iter.hasNext()) {
         pageOffsets[i++]=((Long)iter.next()).longValue();
      }
   }

   public Collection getLogicalStreams() {
      return logicalStreams.values();
   }

   public boolean isOpen() {
      return !closed;
   }

   public void close() throws IOException {
      closed=true;
      source.close();
   }

   private OggPage getNextPage() throws EndOfOggStreamException, IOException, OggFormatException  {
      return getNextPage(false);
   }

   private OggPage getNextPage(boolean skipData) throws EndOfOggStreamException, IOException, OggFormatException  {
      return OggPage.create(source, skipData);
   }

   public OggPage getOggPage(int index) throws IOException {
      source.seek(pageOffsets[index]);
      return OggPage.create(source);
   }

   private LogicalOggStream getLogicalStream(int serialNumber) {
      return (LogicalOggStream)logicalStreams.get(new Integer(serialNumber));
   }

   public void setTime(long granulePosition) throws IOException {
      for(Iterator iter=logicalStreams.values().iterator(); iter.hasNext(); ) {
         LogicalOggStream los=(LogicalOggStream)iter.next();
         los.setTime(granulePosition);
      }
   }

	/**
	 *  @return always <code>true</code>
	 */

   public boolean isSeekable() {
      return true;
   }
}