All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.nervousync.commons.zip.io.input.PartInputStream Maven / Gradle / Ivy

The newest version!
/*
 * Licensed to the Nervousync Studio (NSYC) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.
 * The ASF licenses this file to You 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.
 */
package com.nervousync.commons.zip.io.input;

import java.io.IOException;
import java.io.InputStream;

import com.nervousync.commons.core.Globals;
import com.nervousync.commons.core.zip.ZipConstants;
import com.nervousync.commons.io.NervousyncRandomAccessFile;
import com.nervousync.commons.zip.ZipFile;
import com.nervousync.commons.zip.crypto.Decryptor;
import com.nervousync.commons.zip.crypto.impl.AESDecryptor;
import com.nervousync.exceptions.zip.ZipException;

/**
 * @author Steven Wee	[email protected]
 * @version $Revision: 1.0 $ $Date: Dec 2, 2017 10:30:23 AM $
 */
public class PartInputStream extends InputStream {

	private ZipFile zipFile = null;
	private NervousyncRandomAccessFile input = null;
	private long readBytes = Globals.DEFAULT_VALUE_LONG;
	private long length = Globals.DEFAULT_VALUE_LONG;
	private Decryptor decryptor = null;
	private byte[] oneByteBuffer = new byte[1];
	private byte[] aesBlockBuffer = new byte[ZipConstants.AES_BLOCK_SIZE];
	private int aesBytesReturned = 0;
	private boolean isAESEncryptedFile = Globals.DEFAULT_VALUE_BOOLEAN;
	private int count = Globals.DEFAULT_VALUE_INT;
	
	public PartInputStream(ZipFile zipFile, NervousyncRandomAccessFile input, 
			long length, Decryptor decryptor, boolean isAESEncryptedFile) {
		this.zipFile = zipFile;
		this.input = input;
		this.readBytes = 0L;
		this.length = length;
		this.decryptor = decryptor;
		this.isAESEncryptedFile = isAESEncryptedFile;
	}
	
	@Override
	public int read() throws IOException {
		if (this.readBytes >= this.length) {
			return Globals.DEFAULT_VALUE_INT;
		}
		
		if (this.isAESEncryptedFile) {
			if (this.aesBytesReturned == 0 || this.aesBytesReturned == 16) {
				if (this.read(this.aesBlockBuffer) == Globals.DEFAULT_VALUE_INT) {
					return Globals.DEFAULT_VALUE_INT;
				}
				this.aesBytesReturned = 0;
			}
			return (this.aesBlockBuffer[this.aesBytesReturned++] & 0xFF);
		} else {
			return this.read(this.oneByteBuffer, 0, 1) == Globals.DEFAULT_VALUE_INT ? 
					Globals.DEFAULT_VALUE_INT : (this.oneByteBuffer[0] & 0xFF);
		}
	}

	@Override
	public int read(byte[] b, int off, int len) throws IOException {
		if (len > (this.length - this.readBytes)) {
			len = (int)(this.length - this.readBytes);
			
			if (len == 0) {
				this.checkAndReadAESMacBytes();
				return Globals.DEFAULT_VALUE_INT;
			}
		}
		
		if (this.decryptor instanceof AESDecryptor) {
			if ((this.readBytes + len) < this.length
					&& (len % 16 != 0)) {
				len -= (len % 16);
			}
		}
		
		synchronized (this.input) {
			this.count = this.input.read(b, off, len);
			if ((this.count < len) && this.zipFile.isSplitArchive()) {
				this.input.close();
				this.input = this.zipFile.startNextSplitFile();
				
				if (this.count < 0) {
					this.count = 0;
				}
				
				int readCount = this.input.read(b, this.count, len - this.count);
				if (readCount > 0) {
					this.count += readCount;
				}
			}
		}
		
		if (this.count > 0) {
			if (this.decryptor != null) {
				try {
					this.decryptor.decryptData(b, off, this.count);
				} catch (ZipException e) {
					throw new IOException(e);
				}
			}
			
			this.readBytes += this.count;
		}
		
		if (this.readBytes >= this.length) {
			this.checkAndReadAESMacBytes();
		}
		
		return this.count;
	}
	
	public int available() {
		long amount = this.length - this.readBytes;
		if (amount > Integer.MAX_VALUE) {
			return Integer.MAX_VALUE;
		}
		return (int)amount;
	}
	
	public long skip(long length) throws IOException {
		if (length < 0L) {
			throw new IllegalArgumentException();
		}
		
		if (length > (this.length - this.readBytes)) {
			length = this.length - this.readBytes;
		}
		
		this.readBytes += length;
		return length;
	}
	
	public void seek(long pos) throws IOException {
		this.input.seek(pos);
	}
	
	public void close() throws IOException {
		this.input.close();
	}
	
	protected void seekToEnd() throws IOException {
		this.seek(this.length);
	}
	
	protected void checkAndReadAESMacBytes() throws IOException {
		if (this.isAESEncryptedFile && this.decryptor != null 
				&& (this.decryptor instanceof AESDecryptor)) {
			if (((AESDecryptor)this.decryptor).getStoredMac() != null) {
				//	Store mac already set
				return;
			}
			
			byte[] storedMac = new byte[ZipConstants.AES_AUTH_LENGTH];
			int readLength = this.input.read(storedMac);
			
			if (readLength != ZipConstants.AES_AUTH_LENGTH) {
				if (this.zipFile.isSplitArchive()) {
					this.input.close();
					this.input = this.zipFile.startNextSplitFile();
					int newReadLength = this.input.read(storedMac, 
							readLength, ZipConstants.AES_AUTH_LENGTH - readLength);
					
					readLength += newReadLength;
				} else {
					throw new ZipException("Error occured while reading stored AES authentication bytes");
				}
			}
			
			((AESDecryptor)this.decryptor).setStoredMac(storedMac);
		}
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy