001/* 002 * Copyright 2011-2012 Stephen Connolly. 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 017package org.jszip.pseudo.io; 018 019import java.io.IOException; 020import java.io.InputStream; 021import java.nio.ByteBuffer; 022 023public class PseudoFileInputStream extends InputStream { 024 private final InputStream delegate; 025 private final PseudoFile file; 026 027 public PseudoFileInputStream(PseudoFile file) throws IOException { 028 this.file = file; 029 this.delegate = file.$newInputStream(); 030 } 031 032 public PseudoFileInputStream(String filename) throws IOException { 033 this.file = PseudoFileSystem.current().getPseudoFile(filename); 034 this.delegate = file.$newInputStream(); 035 } 036 037 @Override 038 public int available() throws IOException { 039 return delegate.available(); 040 } 041 042 @Override 043 public void close() throws IOException { 044 delegate.close(); 045 } 046 047 @Override 048 public void mark(int readlimit) { 049 delegate.mark(readlimit); 050 } 051 052 @Override 053 public boolean markSupported() { 054 return delegate.markSupported(); 055 } 056 057 @Override 058 public int read() throws IOException { 059 return delegate.read(); 060 } 061 062 @Override 063 public int read(byte[] b) throws IOException { 064 return delegate.read(b); 065 } 066 067 @Override 068 public int read(byte[] b, int off, int len) throws IOException { 069 return delegate.read(b, off, len); 070 } 071 072 @Override 073 public void reset() throws IOException { 074 delegate.reset(); 075 } 076 077 @Override 078 public long skip(long n) throws IOException { 079 return delegate.skip(n); 080 } 081 082 public PseudoFileInputChannel getChannel() { 083 return new PseudoFileInputChannel() { 084 @Override 085 public long size() throws IOException { 086 return file.length(); 087 } 088 089 @Override 090 public int read(ByteBuffer dst) throws IOException { 091 int pos = dst.arrayOffset(); 092 int len = dst.remaining(); 093 int read = delegate.read(dst.array(), pos, len); 094 if (read > 0) { 095 dst.position(dst.position() + read); 096 } 097 return read; 098 } 099 100 public void close() throws IOException { 101 PseudoFileInputStream.this.close(); 102 } 103 }; 104 } 105 106}