blob: 009fc66a97b13a5aced5fdbd41bdd7b5e0a83cdb [file] [log] [blame]
Constantin Kaplinsky2859fdc2008-06-19 16:07:52 +00001//
2// Copyright (C) 2008 Wimba, Inc. All Rights Reserved.
3//
4// This is free software; you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation; either version 2 of the License, or
7// (at your option) any later version.
8//
9// This software is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this software; if not, write to the Free Software
16// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
17// USA.
18//
19
20//
21// FbsConnection.java
22//
23
24package com.tightvnc.rfbplayer;
25
26import java.io.*;
27import java.net.*;
Constantin Kaplinskyade425a2008-06-20 06:44:03 +000028import java.util.Vector;
Constantin Kaplinsky2859fdc2008-06-19 16:07:52 +000029import java.applet.Applet;
30
31public class FbsConnection {
32
33 URL fbsURL;
34 URL fbiURL;
35 URL fbkURL;
36
Constantin Kaplinskyf338d2c2008-06-20 11:53:19 +000037 /** Index data loaded from the .fbi file. */
38 FbsEntryPoint idx[];
Constantin Kaplinskyade425a2008-06-20 06:44:03 +000039
Constantin Kaplinsky2859fdc2008-06-19 16:07:52 +000040 FbsConnection(String fbsLocation, String indexLocationPrefix, Applet applet)
41 throws MalformedURLException {
42
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +000043 // Construct URLs from strings.
Constantin Kaplinsky2859fdc2008-06-19 16:07:52 +000044 URL base = null;
45 if (applet != null) {
46 base = applet.getCodeBase();
47 }
48 fbsURL = new URL(base, fbsLocation);
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +000049 fbiURL = fbkURL = null;
Constantin Kaplinsky2859fdc2008-06-19 16:07:52 +000050 if (indexLocationPrefix != null) {
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +000051 try {
52 fbiURL = new URL(base, indexLocationPrefix + ".fbi");
53 fbkURL = new URL(base, indexLocationPrefix + ".fbk");
54 } catch (MalformedURLException e) {
55 fbiURL = fbkURL = null;
56 }
Constantin Kaplinsky2859fdc2008-06-19 16:07:52 +000057 }
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +000058
59 // Try to load the .fbi index file.
Constantin Kaplinskyade425a2008-06-20 06:44:03 +000060 idx = null;
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +000061 loadIndex();
Constantin Kaplinsky2859fdc2008-06-19 16:07:52 +000062 }
63
64 FbsInputStream connect(long timeOffset) throws IOException {
65 URLConnection connection = fbsURL.openConnection();
66 FbsInputStream fbs = new FbsInputStream(connection.getInputStream());
67 fbs.setTimeOffset(timeOffset);
68
69 return fbs;
70 }
71
Constantin Kaplinskyf338d2c2008-06-20 11:53:19 +000072 /**
73 * Load index data from .fbi file to {@link #idx idx}.
74 */
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +000075 private void loadIndex() {
76 // Loading .fbi makes sense only if both .fbi and .fbk files are available.
77 if (fbiURL != null && fbkURL != null) {
Constantin Kaplinskyf338d2c2008-06-20 11:53:19 +000078 FbsEntryPoint[] newIndex;
79 int numRecordsRead = 0;
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +000080 try {
81 // Connect.
82 URLConnection connection = fbiURL.openConnection();
83 connection.connect();
84 DataInputStream is = new DataInputStream(connection.getInputStream());
85
86 // Check file signature.
87 byte[] b = new byte[12];
88 is.readFully(b);
89 if (b[0] != 'F' || b[1] != 'B' || b[2] != 'I' || b[3] != ' ' ||
90 b[4] != '0' || b[5] != '0' || b[6] != '1' || b[7] != '.' ||
91 b[8] < '0' || b[8] > '9' || b[9] < '0' || b[9] > '9' ||
92 b[10] < '0' || b[10] > '9' || b[11] != '\n') {
Constantin Kaplinskyf338d2c2008-06-20 11:53:19 +000093 System.err.println("Could not load index: bad .fbi file signature");
94 return;
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +000095 }
96
Constantin Kaplinskyf338d2c2008-06-20 11:53:19 +000097 // Read the record counter and allocate index array.
98 int numRecords = is.readInt();
99 if (numRecords <= 0) {
100 System.err.println("Could not load index: bad .fbi record counter");
101 return;
102 }
103 newIndex = new FbsEntryPoint[numRecords];
104
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +0000105 // Load index from the .fbi file.
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +0000106 try {
Constantin Kaplinskyf338d2c2008-06-20 11:53:19 +0000107 for (int i = 0; i < numRecords; i++) {
108 FbsEntryPoint record = new FbsEntryPoint();
109 record.timestamp = (long)is.readInt() & 0xFFFFFFFFL;
110 record.key_fpos = (long)is.readInt() & 0xFFFFFFFFL;
111 record.key_size = (long)is.readInt() & 0xFFFFFFFFL;
112 record.fbs_fpos = (long)is.readInt() & 0xFFFFFFFFL;
113 record.fbs_skip = (long)is.readInt() & 0xFFFFFFFFL;
114 newIndex[i] = record;
115 numRecordsRead++;
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +0000116 }
117 } catch (EOFException e) {
Constantin Kaplinskyf338d2c2008-06-20 11:53:19 +0000118 System.err.println("Preliminary end of .fbi file");
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +0000119 } catch (IOException e) {
Constantin Kaplinskyf338d2c2008-06-20 11:53:19 +0000120 System.err.println("Ignored exception: " + e);
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +0000121 }
Constantin Kaplinskyf338d2c2008-06-20 11:53:19 +0000122 if (numRecordsRead == 0) {
123 System.err.println("Could not load index: failed to read .fbi data");
124 return;
125 } else if (numRecordsRead != numRecords) {
126 System.err.println("Warning: read not as much .fbi data as expected");
127 }
128 } catch (FileNotFoundException e) {
129 System.err.println("Could not load index: .fbi file not found: " +
130 e.getMessage());
131 return;
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +0000132 } catch (IOException e) {
Constantin Kaplinskyf338d2c2008-06-20 11:53:19 +0000133 System.err.println(e);
134 System.err.println("Could not load index: failed to load .fbi file");
135 return;
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +0000136 }
Constantin Kaplinskyf338d2c2008-06-20 11:53:19 +0000137 idx = newIndex;
138 System.err.println("Loaded index data, " + numRecordsRead + " records");
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +0000139 }
140 }
141
Constantin Kaplinsky2859fdc2008-06-19 16:07:52 +0000142}