Constantin Kaplinsky | 2859fdc | 2008-06-19 16:07:52 +0000 | [diff] [blame] | 1 | // |
| 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 | |
| 24 | package com.tightvnc.rfbplayer; |
| 25 | |
| 26 | import java.io.*; |
| 27 | import java.net.*; |
Constantin Kaplinsky | ade425a | 2008-06-20 06:44:03 +0000 | [diff] [blame] | 28 | import java.util.Vector; |
Constantin Kaplinsky | 2859fdc | 2008-06-19 16:07:52 +0000 | [diff] [blame] | 29 | import java.applet.Applet; |
| 30 | |
| 31 | public class FbsConnection { |
| 32 | |
| 33 | URL fbsURL; |
| 34 | URL fbiURL; |
| 35 | URL fbkURL; |
| 36 | |
Constantin Kaplinsky | ade425a | 2008-06-20 06:44:03 +0000 | [diff] [blame] | 37 | /** Index data read from the .fbi file. */ |
| 38 | Vector idx; |
| 39 | |
Constantin Kaplinsky | 2859fdc | 2008-06-19 16:07:52 +0000 | [diff] [blame] | 40 | FbsConnection(String fbsLocation, String indexLocationPrefix, Applet applet) |
| 41 | throws MalformedURLException { |
| 42 | |
Constantin Kaplinsky | 5f1f886 | 2008-06-20 05:17:39 +0000 | [diff] [blame] | 43 | // Construct URLs from strings. |
Constantin Kaplinsky | 2859fdc | 2008-06-19 16:07:52 +0000 | [diff] [blame] | 44 | URL base = null; |
| 45 | if (applet != null) { |
| 46 | base = applet.getCodeBase(); |
| 47 | } |
| 48 | fbsURL = new URL(base, fbsLocation); |
Constantin Kaplinsky | 5f1f886 | 2008-06-20 05:17:39 +0000 | [diff] [blame] | 49 | fbiURL = fbkURL = null; |
Constantin Kaplinsky | 2859fdc | 2008-06-19 16:07:52 +0000 | [diff] [blame] | 50 | if (indexLocationPrefix != null) { |
Constantin Kaplinsky | 5f1f886 | 2008-06-20 05:17:39 +0000 | [diff] [blame] | 51 | 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 Kaplinsky | 2859fdc | 2008-06-19 16:07:52 +0000 | [diff] [blame] | 57 | } |
Constantin Kaplinsky | 5f1f886 | 2008-06-20 05:17:39 +0000 | [diff] [blame] | 58 | |
| 59 | // Try to load the .fbi index file. |
Constantin Kaplinsky | ade425a | 2008-06-20 06:44:03 +0000 | [diff] [blame] | 60 | idx = null; |
Constantin Kaplinsky | 5f1f886 | 2008-06-20 05:17:39 +0000 | [diff] [blame] | 61 | loadIndex(); |
Constantin Kaplinsky | 2859fdc | 2008-06-19 16:07:52 +0000 | [diff] [blame] | 62 | } |
| 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 Kaplinsky | 5f1f886 | 2008-06-20 05:17:39 +0000 | [diff] [blame] | 72 | 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 Kaplinsky | ade425a | 2008-06-20 06:44:03 +0000 | [diff] [blame] | 93 | Vector newIndex = new Vector(); |
| 94 | FbsEntryPoint record = new FbsEntryPoint(); |
Constantin Kaplinsky | 5f1f886 | 2008-06-20 05:17:39 +0000 | [diff] [blame] | 95 | try { |
| 96 | while (true) { |
Constantin Kaplinsky | ade425a | 2008-06-20 06:44:03 +0000 | [diff] [blame] | 97 | 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 Kaplinsky | 5f1f886 | 2008-06-20 05:17:39 +0000 | [diff] [blame] | 103 | } |
| 104 | } catch (EOFException e) { |
| 105 | } catch (IOException e) { |
| 106 | System.err.println("Warning: Index data may be incomplete"); |
| 107 | } |
Constantin Kaplinsky | ade425a | 2008-06-20 06:44:03 +0000 | [diff] [blame] | 108 | idx = newIndex; |
| 109 | System.err.println("Loaded index data, " + idx.size() + " records"); |
Constantin Kaplinsky | 5f1f886 | 2008-06-20 05:17:39 +0000 | [diff] [blame] | 110 | } 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 Kaplinsky | 2859fdc | 2008-06-19 16:07:52 +0000 | [diff] [blame] | 117 | } |