001/*
002 *  Licensed to the Apache Software Foundation (ASF) under one or more
003 *  contributor license agreements.  See the NOTICE file distributed with
004 *  this work for additional information regarding copyright ownership.
005 *  The ASF licenses this file to You under the Apache License, Version 2.0
006 *  (the "License"); you may not use this file except in compliance with
007 *  the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 *  Unless required by applicable law or agreed to in writing, software
012 *  distributed under the License is distributed on an "AS IS" BASIS,
013 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 *  See the License for the specific language governing permissions and
015 *  limitations under the License.
016 */
017package org.apache.commons.compress.harmony.unpack200;
018
019import java.io.IOException;
020import java.io.InputStream;
021
022import org.apache.commons.compress.harmony.pack200.Codec;
023import org.apache.commons.compress.harmony.pack200.Pack200Exception;
024import org.apache.commons.compress.utils.IOUtils;
025
026/**
027 * Parses the file band headers (not including the actual bits themselves). At the end of this parse call, the input
028 * stream will be positioned at the start of the file_bits themselves, and there will be Sum(file_size) bits remaining
029 * in the stream with BYTE1 compression. A decent implementation will probably just stream the bytes out to the
030 * reconstituted Jar rather than caching them.
031 */
032public class FileBands extends BandSet {
033
034    private byte[][] fileBits;
035
036    private int[] fileModtime;
037
038    private String[] fileName;
039
040    private int[] fileOptions;
041
042    private long[] fileSize;
043
044    private final String[] cpUTF8;
045
046    private InputStream in;
047
048    /**
049     * @param segment TODO
050     */
051    public FileBands(final Segment segment) {
052        super(segment);
053        this.cpUTF8 = segment.getCpBands().getCpUTF8();
054    }
055
056    public byte[][] getFileBits() {
057        return fileBits;
058    }
059
060    public int[] getFileModtime() {
061        return fileModtime;
062    }
063
064    public String[] getFileName() {
065        return fileName;
066    }
067
068    public int[] getFileOptions() {
069        return fileOptions;
070    }
071
072    public long[] getFileSize() {
073        return fileSize;
074    }
075
076    // TODO: stream the file bits directly somehow
077    public void processFileBits() throws IOException, Pack200Exception {
078        // now read in the bytes
079        final int numberOfFiles = header.getNumberOfFiles();
080        fileBits = new byte[numberOfFiles][];
081        for (int i = 0; i < numberOfFiles; i++) {
082            final int size = (int) fileSize[i];
083            // TODO This breaks if file_size > 2^32. Probably an array is
084            // not the right choice, and we should just serialize it here?
085            fileBits[i] = new byte[size];
086            final int read = IOUtils.readFully(in, fileBits[i]);
087            if (size != 0 && read < size) {
088                throw new Pack200Exception("Expected to read " + size + " bytes but read " + read);
089            }
090        }
091    }
092
093    /*
094     * (non-Javadoc)
095     *
096     * @see org.apache.commons.compress.harmony.unpack200.BandSet#unpack(java.io.InputStream)
097     */
098    @Override
099    public void read(final InputStream in) throws IOException, Pack200Exception {
100        final int numberOfFiles = header.getNumberOfFiles();
101        final SegmentOptions options = header.getOptions();
102
103        fileName = parseReferences("file_name", in, Codec.UNSIGNED5, numberOfFiles, cpUTF8);
104        fileSize = parseFlags("file_size", in, numberOfFiles, Codec.UNSIGNED5, options.hasFileSizeHi());
105        if (options.hasFileModtime()) {
106            fileModtime = decodeBandInt("file_modtime", in, Codec.DELTA5, numberOfFiles);
107        } else {
108            fileModtime = new int[numberOfFiles];
109        }
110        if (options.hasFileOptions()) {
111            fileOptions = decodeBandInt("file_options", in, Codec.UNSIGNED5, numberOfFiles);
112        } else {
113            fileOptions = new int[numberOfFiles];
114        }
115        this.in = in; // store for use by processFileBits(), which is called
116        // later
117    }
118
119    @Override
120    public void unpack() {
121
122    }
123
124}