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.vfs2.util; 018 019import java.io.IOException; 020import java.io.InputStream; 021 022import org.apache.commons.vfs2.RandomAccessContent; 023 024/** 025 * A RandomAccessContent that provides end-of-stream monitoring. 026 */ 027public class MonitorRandomAccessContent implements RandomAccessContent { 028 029 private final RandomAccessContent content; 030 private boolean finished; 031 032 /** 033 * Constructs a new instance. 034 * 035 * @param content The contents. 036 */ 037 public MonitorRandomAccessContent(final RandomAccessContent content) { 038 this.content = content; 039 } 040 041 /** 042 * Closes this content. 043 * 044 * @throws IOException if an error occurs. 045 */ 046 @Override 047 public void close() throws IOException { 048 if (finished) { 049 return; 050 } 051 052 // Close the output stream 053 IOException exc; 054 try { 055 content.close(); 056 } catch (final IOException ioe) { 057 exc = ioe; 058 } 059 060 // Notify of end of output 061 exc = null; 062 try { 063 onClose(); 064 } catch (final IOException ioe) { 065 exc = ioe; 066 } 067 068 finished = true; 069 070 if (exc != null) { 071 throw exc; 072 } 073 } 074 075 @Override 076 public long getFilePointer() throws IOException { 077 return content.getFilePointer(); 078 } 079 080 @Override 081 public InputStream getInputStream() throws IOException { 082 return content.getInputStream(); 083 } 084 085 @Override 086 public long length() throws IOException { 087 return content.length(); 088 } 089 090 /** 091 * Called after this stream is closed. 092 * 093 * @throws IOException if subclass throws it. 094 */ 095 @SuppressWarnings("unused") // IOException is needed because subclasses may need to throw it 096 protected void onClose() throws IOException { 097 } 098 099 @Override 100 public boolean readBoolean() throws IOException { 101 return content.readBoolean(); 102 } 103 104 @Override 105 public byte readByte() throws IOException { 106 return content.readByte(); 107 } 108 109 @Override 110 public char readChar() throws IOException { 111 return content.readChar(); 112 } 113 114 @Override 115 public double readDouble() throws IOException { 116 return content.readDouble(); 117 } 118 119 @Override 120 public float readFloat() throws IOException { 121 return content.readFloat(); 122 } 123 124 @Override 125 public void readFully(final byte[] b) throws IOException { 126 content.readFully(b); 127 } 128 129 @Override 130 public void readFully(final byte[] b, final int off, final int len) throws IOException { 131 content.readFully(b, off, len); 132 } 133 134 @Override 135 public int readInt() throws IOException { 136 return content.readInt(); 137 } 138 139 @Override 140 public String readLine() throws IOException { 141 return content.readLine(); 142 } 143 144 @Override 145 public long readLong() throws IOException { 146 return content.readLong(); 147 } 148 149 @Override 150 public short readShort() throws IOException { 151 return content.readShort(); 152 } 153 154 @Override 155 public int readUnsignedByte() throws IOException { 156 return content.readUnsignedByte(); 157 } 158 159 @Override 160 public int readUnsignedShort() throws IOException { 161 return content.readUnsignedShort(); 162 } 163 164 @Override 165 public String readUTF() throws IOException { 166 return content.readUTF(); 167 } 168 169 @Override 170 public void seek(final long pos) throws IOException { 171 content.seek(pos); 172 } 173 174 @Override 175 public void setLength(final long newLength) throws IOException { 176 content.setLength(newLength); 177 } 178 179 @Override 180 public int skipBytes(final int n) throws IOException { 181 return content.skipBytes(n); 182 } 183 184 @Override 185 public void write(final byte[] b) throws IOException { 186 content.write(b); 187 } 188 189 @Override 190 public void write(final byte[] b, final int off, final int len) throws IOException { 191 content.write(b, off, len); 192 } 193 194 @Override 195 public void write(final int b) throws IOException { 196 content.write(b); 197 } 198 199 @Override 200 public void writeBoolean(final boolean v) throws IOException { 201 content.writeBoolean(v); 202 } 203 204 @Override 205 public void writeByte(final int v) throws IOException { 206 content.writeByte(v); 207 } 208 209 @Override 210 public void writeBytes(final String s) throws IOException { 211 content.writeBytes(s); 212 } 213 214 @Override 215 public void writeChar(final int v) throws IOException { 216 content.writeChar(v); 217 } 218 219 @Override 220 public void writeChars(final String s) throws IOException { 221 content.writeChars(s); 222 } 223 224 @Override 225 public void writeDouble(final double v) throws IOException { 226 content.writeDouble(v); 227 } 228 229 @Override 230 public void writeFloat(final float v) throws IOException { 231 content.writeFloat(v); 232 } 233 234 @Override 235 public void writeInt(final int v) throws IOException { 236 content.writeInt(v); 237 } 238 239 @Override 240 public void writeLong(final long v) throws IOException { 241 content.writeLong(v); 242 } 243 244 @Override 245 public void writeShort(final int v) throws IOException { 246 content.writeShort(v); 247 } 248 249 @Override 250 public void writeUTF(final String str) throws IOException { 251 content.writeUTF(str); 252 } 253 254}