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