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.*; |
| 28 | import java.applet.Applet; |
| 29 | |
| 30 | public class FbsConnection { |
| 31 | |
| 32 | URL fbsURL; |
| 33 | URL fbiURL; |
| 34 | URL fbkURL; |
| 35 | |
Constantin Kaplinsky | f338d2c | 2008-06-20 11:53:19 +0000 | [diff] [blame] | 36 | /** Index data loaded from the .fbi file. */ |
Constantin Kaplinsky | b0f71f0 | 2008-06-20 11:59:07 +0000 | [diff] [blame^] | 37 | FbsEntryPoint[] idx; |
Constantin Kaplinsky | ade425a | 2008-06-20 06:44:03 +0000 | [diff] [blame] | 38 | |
Constantin Kaplinsky | 2859fdc | 2008-06-19 16:07:52 +0000 | [diff] [blame] | 39 | FbsConnection(String fbsLocation, String indexLocationPrefix, Applet applet) |
| 40 | throws MalformedURLException { |
| 41 | |
Constantin Kaplinsky | 5f1f886 | 2008-06-20 05:17:39 +0000 | [diff] [blame] | 42 | // Construct URLs from strings. |
Constantin Kaplinsky | 2859fdc | 2008-06-19 16:07:52 +0000 | [diff] [blame] | 43 | URL base = null; |
| 44 | if (applet != null) { |
| 45 | base = applet.getCodeBase(); |
| 46 | } |
| 47 | fbsURL = new URL(base, fbsLocation); |
Constantin Kaplinsky | 5f1f886 | 2008-06-20 05:17:39 +0000 | [diff] [blame] | 48 | fbiURL = fbkURL = null; |
Constantin Kaplinsky | 2859fdc | 2008-06-19 16:07:52 +0000 | [diff] [blame] | 49 | if (indexLocationPrefix != null) { |
Constantin Kaplinsky | 5f1f886 | 2008-06-20 05:17:39 +0000 | [diff] [blame] | 50 | 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 Kaplinsky | 2859fdc | 2008-06-19 16:07:52 +0000 | [diff] [blame] | 56 | } |
Constantin Kaplinsky | 5f1f886 | 2008-06-20 05:17:39 +0000 | [diff] [blame] | 57 | |
| 58 | // Try to load the .fbi index file. |
Constantin Kaplinsky | ade425a | 2008-06-20 06:44:03 +0000 | [diff] [blame] | 59 | idx = null; |
Constantin Kaplinsky | 5f1f886 | 2008-06-20 05:17:39 +0000 | [diff] [blame] | 60 | loadIndex(); |
Constantin Kaplinsky | 2859fdc | 2008-06-19 16:07:52 +0000 | [diff] [blame] | 61 | } |
| 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 Kaplinsky | f338d2c | 2008-06-20 11:53:19 +0000 | [diff] [blame] | 71 | /** |
| 72 | * Load index data from .fbi file to {@link #idx idx}. |
| 73 | */ |
Constantin Kaplinsky | 5f1f886 | 2008-06-20 05:17:39 +0000 | [diff] [blame] | 74 | private void loadIndex() { |
| 75 | // Loading .fbi makes sense only if both .fbi and .fbk files are available. |
| 76 | if (fbiURL != null && fbkURL != null) { |
Constantin Kaplinsky | f338d2c | 2008-06-20 11:53:19 +0000 | [diff] [blame] | 77 | FbsEntryPoint[] newIndex; |
| 78 | int numRecordsRead = 0; |
Constantin Kaplinsky | 5f1f886 | 2008-06-20 05:17:39 +0000 | [diff] [blame] | 79 | 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 Kaplinsky | f338d2c | 2008-06-20 11:53:19 +0000 | [diff] [blame] | 92 | System.err.println("Could not load index: bad .fbi file signature"); |
| 93 | return; |
Constantin Kaplinsky | 5f1f886 | 2008-06-20 05:17:39 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Constantin Kaplinsky | f338d2c | 2008-06-20 11:53:19 +0000 | [diff] [blame] | 96 | // 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 Kaplinsky | 5f1f886 | 2008-06-20 05:17:39 +0000 | [diff] [blame] | 104 | // Load index from the .fbi file. |
Constantin Kaplinsky | 5f1f886 | 2008-06-20 05:17:39 +0000 | [diff] [blame] | 105 | try { |
Constantin Kaplinsky | f338d2c | 2008-06-20 11:53:19 +0000 | [diff] [blame] | 106 | 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 Kaplinsky | 5f1f886 | 2008-06-20 05:17:39 +0000 | [diff] [blame] | 115 | } |
| 116 | } catch (EOFException e) { |
Constantin Kaplinsky | f338d2c | 2008-06-20 11:53:19 +0000 | [diff] [blame] | 117 | System.err.println("Preliminary end of .fbi file"); |
Constantin Kaplinsky | 5f1f886 | 2008-06-20 05:17:39 +0000 | [diff] [blame] | 118 | } catch (IOException e) { |
Constantin Kaplinsky | f338d2c | 2008-06-20 11:53:19 +0000 | [diff] [blame] | 119 | System.err.println("Ignored exception: " + e); |
Constantin Kaplinsky | 5f1f886 | 2008-06-20 05:17:39 +0000 | [diff] [blame] | 120 | } |
Constantin Kaplinsky | f338d2c | 2008-06-20 11:53:19 +0000 | [diff] [blame] | 121 | 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 Kaplinsky | 5f1f886 | 2008-06-20 05:17:39 +0000 | [diff] [blame] | 131 | } catch (IOException e) { |
Constantin Kaplinsky | f338d2c | 2008-06-20 11:53:19 +0000 | [diff] [blame] | 132 | System.err.println(e); |
| 133 | System.err.println("Could not load index: failed to load .fbi file"); |
| 134 | return; |
Constantin Kaplinsky | 5f1f886 | 2008-06-20 05:17:39 +0000 | [diff] [blame] | 135 | } |
Constantin Kaplinsky | f338d2c | 2008-06-20 11:53:19 +0000 | [diff] [blame] | 136 | idx = newIndex; |
| 137 | System.err.println("Loaded index data, " + numRecordsRead + " records"); |
Constantin Kaplinsky | 5f1f886 | 2008-06-20 05:17:39 +0000 | [diff] [blame] | 138 | } |
| 139 | } |
| 140 | |
Constantin Kaplinsky | 2859fdc | 2008-06-19 16:07:52 +0000 | [diff] [blame] | 141 | } |