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