Back to index

lightning-sunbird  0.9+nobinonly
FileTable.java
Go to the documentation of this file.
00001 /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * The contents of this file are subject to the Netscape Public
00004  * License Version 1.1 (the "License"); you may not use this file
00005  * except in compliance with the License. You may obtain a copy of
00006  * the License at http://www.mozilla.org/NPL/
00007  *
00008  * Software distributed under the License is distributed on an "AS
00009  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
00010  * implied. See the License for the specific language governing
00011  * rights and limitations under the License.
00012  *
00013  * The Original Code is Mozilla Communicator client code, released
00014  * March 31, 1998.
00015  *
00016  * The Initial Developer of the Original Code is Netscape
00017  * Communications Corporation.  Portions created by Netscape are
00018  * Copyright (C) 1998 Netscape Communications Corporation. All
00019  * Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *
00023  * Patrick C. Beard <beard@netscape.com>
00024  *
00025  * Alternatively, the contents of this file may be used under the
00026  * terms of the GNU Public License (the "GPL"), in which case the
00027  * provisions of the GPL are applicable instead of those above.
00028  * If you wish to allow use of your version of this file only
00029  * under the terms of the GPL and not to allow others to use your
00030  * version of this file under the NPL, indicate your decision by
00031  * deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL.  If you do not delete
00033  * the provisions above, a recipient may use your version of this
00034  * file under either the NPL or the GPL.
00035  */
00036 
00037 import java.io.*;
00038 import java.util.*;
00039 
00043 public class FileTable {
00044        private static class Line {
00045               int mOffset;
00046               int mLength;
00047               
00048               Line(int offset, int length) {
00049                      mOffset = offset;
00050                      mLength = length;
00051               }
00052        }
00053        private Line[] mLines;
00054 
00055        public FileTable(String path) throws IOException {
00056               Vector lines = new Vector();
00057               BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(path)));
00058               int offset = 0;
00059               for (String line = reader.readLine(); line != null; line = reader.readLine()) {
00060                      // always add 1 for the line feed.
00061                      int length = 1 + line.length();
00062                      lines.addElement(new Line(offset, length));
00063                      offset += length;
00064               }
00065               reader.close();
00066               int size = lines.size();
00067               mLines = new Line[size];
00068               lines.copyInto(mLines);
00069        }
00070        
00071        public int getLine(int offset) {
00072               // use binary search to find the line which spans this offset.
00073               int length = mLines.length;
00074               int minIndex = 0, maxIndex = length - 1;
00075               int index = maxIndex / 2;
00076               while (minIndex <= maxIndex) {
00077                      Line line = mLines[index];
00078                      if (offset < line.mOffset) {
00079                             maxIndex = (index - 1);
00080                             index = (minIndex + maxIndex) / 2;
00081                      } else {
00082                             if (offset < (line.mOffset + line.mLength)) {
00083                                    return index;
00084                             }
00085                             minIndex = (index + 1);
00086                             index = (minIndex + maxIndex) / 2;
00087                      }
00088               }
00089               // this case shouldn't happen, but provides a helpful value to detect errors.
00090               return -1;
00091        }
00092 }