/*
 * Copyright 2008-2010 Microarray Informatics Team, EMBL-European Bioinformatics Institute
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 *
 * For further details of the Gene Expression Atlas project, including source code,
 * downloads and documentation, please see:
 *
 * http://gxa.github.com/gxa
 */

package uk.ac.ebi.gxa.requesthandlers.wiggle.bam;

import java.io.*;

class BAIReader {
    private final File file;

    BAIReader(File file) {
        this.file = file;
    }

    ReferenceData getReferenceData(int chromosomeIndex) {
        try {
            final InputStream stream = new BufferedInputStream(new FileInputStream(file));
            if (!"BAI\001".equals(FileTools.readString(stream, 4))) {
                throw new BAMException("Invalid BAI file signature");
            }
            // TODO: do we REALLY need to ignore it?
            final int n_ref = FileTools.readInt32(stream);
            for (int i = 0; i < chromosomeIndex; ++i) {
                new ReferenceData(stream);
            }
                
            return new ReferenceData(stream);
        } catch (IOException e) {
            return null;
        }
    }
}

@@ -1411,32 +1411,244 @@
 dInt32(stream);%0A
+            if (n_ref %3C= chromosomeIndex) %7B%0A                throw new BAMException(%22There are only %22 + n_ref + %22 references in the file;%22 +%0A                        %22 requested %22 + chromosomeIndex);%0A            %7D%0A
             for 
@@ -1683,24 +1683,24 @@
 dex; ++i) %7B%0A
-
             
@@ -1744,32 +1744,16 @@
       %7D%0A
-                
 %0A       

Validating the chromosomeIndex