blob: 66d9e54b50aa55ea94908ae833c2966754e97fdb [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.*;
28import java.applet.Applet;
29
30public class FbsConnection {
31
32 URL fbsURL;
33 URL fbiURL;
34 URL fbkURL;
35
Constantin Kaplinskyf338d2c2008-06-20 11:53:19 +000036 /** Index data loaded from the .fbi file. */
Constantin Kaplinskyb0f71f02008-06-20 11:59:07 +000037 FbsEntryPoint[] idx;
Constantin Kaplinskyade425a2008-06-20 06:44:03 +000038
Constantin Kaplinsky2859fdc2008-06-19 16:07:52 +000039 FbsConnection(String fbsLocation, String indexLocationPrefix, Applet applet)
40 throws MalformedURLException {
41
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +000042 // Construct URLs from strings.
Constantin Kaplinsky2859fdc2008-06-19 16:07:52 +000043 URL base = null;
44 if (applet != null) {
45 base = applet.getCodeBase();
46 }
47 fbsURL = new URL(base, fbsLocation);
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +000048 fbiURL = fbkURL = null;
Constantin Kaplinsky2859fdc2008-06-19 16:07:52 +000049 if (indexLocationPrefix != null) {
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +000050 try {
51 fbiURL = new URL(base, indexLocationPrefix + ".fbi");
52 fbkURL = new URL(base, indexLocationPrefix + ".fbk");
53 } catch (MalformedURLException e) {
54 fbiURL = fbkURL = null;
55 }
Constantin Kaplinsky2859fdc2008-06-19 16:07:52 +000056 }
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +000057
58 // Try to load the .fbi index file.
Constantin Kaplinskyade425a2008-06-20 06:44:03 +000059 idx = null;
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +000060 loadIndex();
Constantin Kaplinsky2859fdc2008-06-19 16:07:52 +000061 }
62
63 FbsInputStream connect(long timeOffset) throws IOException {
64 URLConnection connection = fbsURL.openConnection();
65 FbsInputStream fbs = new FbsInputStream(connection.getInputStream());
66 fbs.setTimeOffset(timeOffset);
67
68 return fbs;
69 }
70
Constantin Kaplinskyf338d2c2008-06-20 11:53:19 +000071 /**
72 * Load index data from .fbi file to {@link #idx idx}.
73 */
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +000074 private void loadIndex() {
75 // Loading .fbi makes sense only if both .fbi and .fbk files are available.
76 if (fbiURL != null && fbkURL != null) {
Constantin Kaplinskyf338d2c2008-06-20 11:53:19 +000077 FbsEntryPoint[] newIndex;
78 int numRecordsRead = 0;
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +000079 try {
80 // Connect.
81 URLConnection connection = fbiURL.openConnection();
82 connection.connect();
83 DataInputStream is = new DataInputStream(connection.getInputStream());
84
85 // Check file signature.
86 byte[] b = new byte[12];
87 is.readFully(b);
88 if (b[0] != 'F' || b[1] != 'B' || b[2] != 'I' || b[3] != ' ' ||
89 b[4] != '0' || b[5] != '0' || b[6] != '1' || b[7] != '.' ||
90 b[8] < '0' || b[8] > '9' || b[9] < '0' || b[9] > '9' ||
91 b[10] < '0' || b[10] > '9' || b[11] != '\n') {
Constantin Kaplinskyf338d2c2008-06-20 11:53:19 +000092 System.err.println("Could not load index: bad .fbi file signature");
93 return;
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +000094 }
95
Constantin Kaplinskyf338d2c2008-06-20 11:53:19 +000096 // Read the record counter and allocate index array.
97 int numRecords = is.readInt();
98 if (numRecords <= 0) {
99 System.err.println("Could not load index: bad .fbi record counter");
100 return;
101 }
102 newIndex = new FbsEntryPoint[numRecords];
103
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +0000104 // Load index from the .fbi file.
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +0000105 try {
Constantin Kaplinskyf338d2c2008-06-20 11:53:19 +0000106 for (int i = 0; i < numRecords; i++) {
107 FbsEntryPoint record = new FbsEntryPoint();
108 record.timestamp = (long)is.readInt() & 0xFFFFFFFFL;
109 record.key_fpos = (long)is.readInt() & 0xFFFFFFFFL;
110 record.key_size = (long)is.readInt() & 0xFFFFFFFFL;
111 record.fbs_fpos = (long)is.readInt() & 0xFFFFFFFFL;
112 record.fbs_skip = (long)is.readInt() & 0xFFFFFFFFL;
113 newIndex[i] = record;
114 numRecordsRead++;
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +0000115 }
116 } catch (EOFException e) {
Constantin Kaplinskyf338d2c2008-06-20 11:53:19 +0000117 System.err.println("Preliminary end of .fbi file");
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +0000118 } catch (IOException e) {
Constantin Kaplinskyf338d2c2008-06-20 11:53:19 +0000119 System.err.println("Ignored exception: " + e);
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +0000120 }
Constantin Kaplinskyf338d2c2008-06-20 11:53:19 +0000121 if (numRecordsRead == 0) {
122 System.err.println("Could not load index: failed to read .fbi data");
123 return;
124 } else if (numRecordsRead != numRecords) {
125 System.err.println("Warning: read not as much .fbi data as expected");
126 }
127 } catch (FileNotFoundException e) {
128 System.err.println("Could not load index: .fbi file not found: " +
129 e.getMessage());
130 return;
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +0000131 } catch (IOException e) {
Constantin Kaplinskyf338d2c2008-06-20 11:53:19 +0000132 System.err.println(e);
133 System.err.println("Could not load index: failed to load .fbi file");
134 return;
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +0000135 }
Constantin Kaplinskyf338d2c2008-06-20 11:53:19 +0000136 idx = newIndex;
137 System.err.println("Loaded index data, " + numRecordsRead + " records");
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +0000138 }
139 }
140
Constantin Kaplinsky2859fdc2008-06-19 16:07:52 +0000141}