001/* 002 * jDTAUS Core API 003 * Copyright (C) 2005 Christian Schulte 004 * <cs@schulte.it> 005 * 006 * This library is free software; you can redistribute it and/or 007 * modify it under the terms of the GNU Lesser General Public 008 * License as published by the Free Software Foundation; either 009 * version 2.1 of the License, or any later version. 010 * 011 * This library is distributed in the hope that it will be useful, 012 * but WITHOUT ANY WARRANTY; without even the implied warranty of 013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 014 * Lesser General Public License for more details. 015 * 016 * You should have received a copy of the GNU Lesser General Public 017 * License along with this library; if not, write to the Free Software 018 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 019 * 020 */ 021package org.jdtaus.core.io; 022 023import java.io.IOException; 024import java.io.InputStream; 025import java.io.OutputStream; 026 027/** 028 * Elementary I/O operations. 029 * 030 * @author <a href="mailto:cs@schulte.it">Christian Schulte</a> 031 * @version $JDTAUS: FileOperations.java 8743 2012-10-07 03:06:20Z schulte $ 032 */ 033public interface FileOperations 034{ 035 //--FileOperations---------------------------------------------------------- 036 037 /** 038 * Constant returned by the {@link #read(byte[],int,int)} method if the 039 * end of the file has been reached. 040 */ 041 int EOF = -1; 042 043 /** 044 * Gets the length of the file. 045 * 046 * @return the length measured in bytes. 047 * 048 * @throws IOException if getting the length fails. 049 */ 050 long getLength() throws IOException; 051 052 /** 053 * Sets the length of the file. 054 * <p>If the present length of the file as returned by the 055 * {@code getLength()} method is greater than the {@code newLength} argument 056 * then the file will be truncated. In this case, if the file offset as 057 * returned by the {@code getFilePointer()} method is greater than 058 * {@code newLength} then after this method returns the offset will be equal 059 * to {@code newLength}.</p> 060 * <p>If the present length of the file as returned by the 061 * {@code getLength()} method is smaller than the {@code newLength} argument 062 * then the file will be extended. In this case, the contents of the 063 * extended portion of the file are not defined.</p> 064 * 065 * @param newLength The desired length of the file. 066 * 067 * @throws IllegalArgumentException if {@code newLength} is negative. 068 * @throws IOException if setting the length fails. 069 */ 070 void setLength( long newLength ) throws IOException; 071 072 /** 073 * Returns the current offset in the file. 074 * 075 * @return the offset from the beginning of the file, in bytes, at which the 076 * next read or write occurs. 077 * 078 * @throws IOException if getting the current offset in the file fails. 079 */ 080 long getFilePointer() throws IOException; 081 082 /** 083 * Sets the file-pointer offset, measured from the beginning of the file, 084 * at which the next read or write occurs. The offset may be set beyond the 085 * end of the file. Setting the offset beyond the end of the file does not 086 * change the file length. The file length will change only by writing after 087 * the offset has been set beyond the end of the file. 088 * 089 * @param pos the offset position, measured in bytes from the beginning of 090 * the file, at which to set the file pointer. 091 * 092 * @throws IllegalArgumentException if {@code pos} is negative. 093 * @throws IOException if setting the file-pointer offset fails. 094 */ 095 void setFilePointer( long pos ) throws IOException; 096 097 /** 098 * Reads up to {@code len} bytes of data from the file into an array of 099 * bytes. An attempt is made to read as many as {@code len} bytes, but a 100 * smaller number may be read. 101 * 102 * @param buf the buffer into which the data is read. 103 * @param off the start offset of the data. 104 * @param len the maximum number of bytes read. 105 * 106 * @return the total number of bytes read into the buffer, or {@link #EOF} 107 * if there is no more data because the end of the file has been reached. 108 * 109 * @throws NullPointerException if {@code buf} is {@code null}. 110 * @throws IndexOutOfBoundsException if either {@code off} or {@code len} 111 * is negative or {@code off + len} is greater than the length of 112 * {@code buf}. 113 * @throws IOException if reading fails. 114 */ 115 int read( byte[] buf, int off, int len ) throws IOException; 116 117 /** 118 * Writes {@code len} bytes from the specified byte array starting at 119 * offset {@code off} to the file. 120 * 121 * @param buf the data. 122 * @param off the start offset in the data. 123 * @param len the number of bytes to write. 124 * 125 * @throws IndexOutOfBoundsException if either {@code off} or {@code len} 126 * is negative or {@code off + len} is greater than the length of 127 * {@code buf}. 128 * @throws IOException if writing fails. 129 */ 130 void write( byte[] buf, int off, int len ) throws IOException; 131 132 /** 133 * Reads all data from the file to some {@code OutputStream}. The stream 134 * will not get closed after all data has been written. 135 * 136 * @param out an output stream to stream to. 137 * 138 * @throws NullPointerException if {@code out} is {@code null}. 139 * @throws IOException if reading fails. 140 */ 141 void read( OutputStream out ) throws IOException; 142 143 /** 144 * Writes all data available from some {@code InputStream}. The stream will 145 * not get closed after all data has been read. 146 * 147 * @param in an input stream providing the data to be written. 148 * 149 * @throws NullPointerException if {@code in} is {@code null}. 150 * @throws IOException if writing fails. 151 */ 152 void write( InputStream in ) throws IOException; 153 154 /** 155 * Releases any system resources associated with an instance. A closed 156 * instance cannot perform input or output operations and cannot be 157 * reopened. 158 * 159 * @throws IOException if releasing system resources fails. 160 */ 161 void close() throws IOException; 162 163 //----------------------------------------------------------FileOperations-- 164}