blob: 66e12c75cda5a583f5d5a735f6058478f56e8ab5 [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
36 FbsConnection(String fbsLocation, String indexLocationPrefix, Applet applet)
37 throws MalformedURLException {
38
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +000039 // Construct URLs from strings.
Constantin Kaplinsky2859fdc2008-06-19 16:07:52 +000040 URL base = null;
41 if (applet != null) {
42 base = applet.getCodeBase();
43 }
44 fbsURL = new URL(base, fbsLocation);
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +000045 fbiURL = fbkURL = null;
Constantin Kaplinsky2859fdc2008-06-19 16:07:52 +000046 if (indexLocationPrefix != null) {
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +000047 try {
48 fbiURL = new URL(base, indexLocationPrefix + ".fbi");
49 fbkURL = new URL(base, indexLocationPrefix + ".fbk");
50 } catch (MalformedURLException e) {
51 fbiURL = fbkURL = null;
52 }
Constantin Kaplinsky2859fdc2008-06-19 16:07:52 +000053 }
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +000054
55 // Try to load the .fbi index file.
56 loadIndex();
Constantin Kaplinsky2859fdc2008-06-19 16:07:52 +000057 }
58
59 FbsInputStream connect(long timeOffset) throws IOException {
60 URLConnection connection = fbsURL.openConnection();
61 FbsInputStream fbs = new FbsInputStream(connection.getInputStream());
62 fbs.setTimeOffset(timeOffset);
63
64 return fbs;
65 }
66
Constantin Kaplinsky5f1f8862008-06-20 05:17:39 +000067 private void loadIndex() {
68 // Loading .fbi makes sense only if both .fbi and .fbk files are available.
69 if (fbiURL != null && fbkURL != null) {
70 try {
71 // Connect.
72 URLConnection connection = fbiURL.openConnection();
73 connection.connect();
74 DataInputStream is = new DataInputStream(connection.getInputStream());
75
76 // Check file signature.
77 byte[] b = new byte[12];
78 is.readFully(b);
79 if (b[0] != 'F' || b[1] != 'B' || b[2] != 'I' || b[3] != ' ' ||
80 b[4] != '0' || b[5] != '0' || b[6] != '1' || b[7] != '.' ||
81 b[8] < '0' || b[8] > '9' || b[9] < '0' || b[9] > '9' ||
82 b[10] < '0' || b[10] > '9' || b[11] != '\n') {
83 System.err.println("Warning: bad .fbi file data, not using index");
84 fbiURL = null;
85 }
86
87 // Load index from the .fbi file.
88 // FIXME: Real loading is not implemented yet.
89 int numRecords = 0;
90 try {
91 while (true) {
92 is.readInt();
93 is.readInt();
94 is.readInt();
95 is.readInt();
96 is.readInt();
97 numRecords++;
98 }
99 } catch (EOFException e) {
100 } catch (IOException e) {
101 System.err.println("Warning: Index data may be incomplete");
102 }
103 System.err.println("Loaded index data, " + numRecords + " records");
104 } catch (IOException e) {
105 System.err.println("Warning: I/O exception while loading index: " + e);
106 System.err.println("Warning: failed to load .fbi, not using index");
107 }
108 }
109 }
110
Constantin Kaplinsky2859fdc2008-06-19 16:07:52 +0000111}