1 /* 2 * Copyright (C) 2003-2009 JNode.org 3 * 2009,2010 Matthias Treydte <mt@waldheinz.de> 4 * 5 * This library is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU Lesser General Public License as published 7 * by the Free Software Foundation; either version 2.1 of the License, or 8 * (at your option) any later version. 9 * 10 * This library is distributed in the hope that it will be useful, but 11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY 12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public 13 * License for more details. 14 * 15 * You should have received a copy of the GNU Lesser General Public License 16 * along with this library; If not, write to the Free Software Foundation, Inc., 17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 18 */ 19 20 package de.waldheinz.fs; 21 22 import java.io.IOException; 23 24 /** 25 * Abstract class with common things in different FileSystem implementations. 26 * 27 * @author Fabien DUMINY 28 * @author Matthias Treydte <waldheinz at gmail.com> 29 */ 30 public abstract class AbstractFileSystem implements FileSystem { 31 private final boolean readOnly; 32 private boolean closed; 33 34 /** 35 * Creates a new {@code AbstractFileSystem}. 36 * 37 * @param readOnly if the file system should be read-only 38 */ AbstractFileSystem(boolean readOnly)39 public AbstractFileSystem(boolean readOnly) { 40 this.closed = false; 41 this.readOnly = readOnly; 42 } 43 44 @Override close()45 public void close() throws IOException { 46 if (!isClosed()) { 47 if (!isReadOnly()) { 48 flush(); 49 } 50 51 closed = true; 52 } 53 } 54 55 @Override isClosed()56 public final boolean isClosed() { 57 return closed; 58 } 59 60 @Override isReadOnly()61 public final boolean isReadOnly() { 62 return readOnly; 63 } 64 65 /** 66 * Checks if this {@code FileSystem} was already closed, and throws an 67 * exception if it was. 68 * 69 * @throws IllegalStateException if this {@code FileSystem} was 70 * already closed 71 * @see #isClosed() 72 * @see #close() 73 */ checkClosed()74 protected final void checkClosed() throws IllegalStateException { 75 if (isClosed()) { 76 throw new IllegalStateException("file system was already closed"); 77 } 78 } 79 80 /** 81 * Checks if this {@code FileSystem} is read-only, and throws an 82 * exception if it is. 83 * 84 * @throws ReadOnlyException if this {@code FileSystem} is read-only 85 * @see #isReadOnly() 86 */ checkReadOnly()87 protected final void checkReadOnly() throws ReadOnlyException { 88 if (isReadOnly()) { 89 throw new ReadOnlyException(); 90 } 91 } 92 } 93