blob: 96b3c44a57ef4c45614ae70e2eba97e097e59545 [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 Kaplinskyade425a2008-06-20 06:44:03 +000037 /** Index data read from the .fbi file. */
38 Vector idx;
39
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 Kaplinsky5f1f8862008-06-20 05:17:39 +000072 private void loadIndex() {
73 // Loading .fbi makes sense only if both .fbi and .fbk files are available.
74 if (fbiURL != null && fbkURL != null) {
75 try {
76 // Connect.
77 URLConnection connection = fbiURL.openConnection();
78 connection.connect();
79 DataInputStream is = new DataInputStream(connection.getInputStream());
80
81 // Check file signature.
82 byte[] b = new byte[12];
83 is.readFully(b);
84 if (b[0] != 'F' || b[1] != 'B' || b[2] != 'I' || b[3] != ' ' ||
85 b[4] != '0' || b[5] != '0' || b[6] != '1' || b[7] != '.' ||
86 b[8] < '0' || b[8] > '9' || b[9] < '0' || b[9] > '9' ||
87 b[10] < '0' || b[10] > '9' || b[11] != '\n') {
88 System.err.println("Warning: bad .fbi file data, not using index");
89 fbiURL = null;
90 }
91
92 // Load index from the .fbi file.
Constantin Kaplinskyade425a2008-06-20 06:44:03 +000093 Vector newIndex = new Vector();
94 FbsEntryPoint record = new FbsEntryPoint();
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +000095 try {
96 while (true) {
Constantin Kaplinskyade425a2008-06-20 06:44:03 +000097 record.timestamp = is.readInt();
98 record.key_fpos = is.readInt();
99 record.key_size = is.readInt();
100 record.fbs_fpos = is.readInt();
101 record.fbs_skip = is.readInt();
102 newIndex.add(record);
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +0000103 }
104 } catch (EOFException e) {
105 } catch (IOException e) {
106 System.err.println("Warning: Index data may be incomplete");
107 }
Constantin Kaplinskyade425a2008-06-20 06:44:03 +0000108 idx = newIndex;
109 System.err.println("Loaded index data, " + idx.size() + " records");
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +0000110 } catch (IOException e) {
111 System.err.println("Warning: I/O exception while loading index: " + e);
112 System.err.println("Warning: failed to load .fbi, not using index");
113 }
114 }
115 }
116
Constantin Kaplinsky2859fdc2008-06-19 16:07:52 +0000117}