151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/* 22c87ad3a45cecf9e344487cad1abfdebe79f2c7cNarayan Kamath * Copyright (C) 2014 The Android Open Source Project 351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved. 451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is free software; you can redistribute it and/or modify it 751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * under the terms of the GNU General Public License version 2 only, as 851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * published by the Free Software Foundation. Oracle designates this 951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * particular file as subject to the "Classpath" exception as provided 1051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * by Oracle in the LICENSE file that accompanied this code. 1151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 1251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This code is distributed in the hope that it will be useful, but WITHOUT 1351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * version 2 for more details (a copy is included in the LICENSE file that 1651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * accompanied this code). 1751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 1851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * You should have received a copy of the GNU General Public License version 1951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 2 along with this work; if not, write to the Free Software Foundation, 2051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 2151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 2251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 2351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or visit www.oracle.com if you need additional information or have any 2451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * questions. 2551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 2651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 2751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipackage java.io; 2851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 29e31b37859051d3902e06e4ba384995df7188917fHans Boehmimport dalvik.annotation.optimization.ReachabilitySensitive; 3051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport java.nio.channels.FileChannel; 3151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskiimport sun.nio.ch.FileChannelImpl; 3253446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniakimport android.system.Os; 338d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebskiimport android.system.ErrnoException; 348d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebskiimport dalvik.system.CloseGuard; 358d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebskiimport libcore.io.IoBridge; 36118de16ee0529955eaca8cc5ce864cac49598c86Shubham Ajmeraimport libcore.io.IoTracker; 378d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebskiimport libcore.io.Libcore; 388d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebskiimport static android.system.OsConstants.*; 3951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 4051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 4151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski/** 4251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Instances of this class support both reading and writing to a 4351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * random access file. A random access file behaves like a large 4451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array of bytes stored in the file system. There is a kind of cursor, 4551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or index into the implied array, called the <em>file pointer</em>; 4651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * input operations read bytes starting at the file pointer and advance 4751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the file pointer past the bytes read. If the random access file is 4851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * created in read/write mode, then output operations are also available; 4951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * output operations write bytes starting at the file pointer and advance 5051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the file pointer past the bytes written. Output operations that write 5151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * past the current end of the implied array cause the array to be 5251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * extended. The file pointer can be read by the 531777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code getFilePointer} method and set by the {@code seek} 5451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method. 5551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 5651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * It is generally true of all the reading routines in this class that 5751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if end-of-file is reached before the desired number of bytes has been 581777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * read, an {@code EOFException} (which is a kind of 591777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code IOException}) is thrown. If any byte cannot be read for 601777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * any reason other than end-of-file, an {@code IOException} other 611777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * than {@code EOFException} is thrown. In particular, an 621777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code IOException} may be thrown if the stream has been closed. 6351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 6451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @author unascribed 6551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since JDK1.0 6651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 6751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 6851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebskipublic class RandomAccessFile implements DataOutput, DataInput, Closeable { 6951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 70b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer // BEGIN Android-added: CloseGuard and some helper fields for Android changes in this file. 71e31b37859051d3902e06e4ba384995df7188917fHans Boehm @ReachabilitySensitive 728d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski private final CloseGuard guard = CloseGuard.get(); 738d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski private final byte[] scratch = new byte[8]; 7453446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak 7553446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak private static final int FLUSH_NONE = 0; 7653446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak private static final int FLUSH_FSYNC = 1; 7753446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak private static final int FLUSH_FDATASYNC = 2; 7853446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak private int flushAfterWrite = FLUSH_NONE; 7953446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak 808d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski private int mode; 81b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer // END Android-added: CloseGuard and some helper fields for Android changes in this file. 828d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski 83e31b37859051d3902e06e4ba384995df7188917fHans Boehm // Android-added: @ReachabilitySensitive 84e31b37859051d3902e06e4ba384995df7188917fHans Boehm @ReachabilitySensitive 8551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private FileDescriptor fd; 8651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private FileChannel channel = null; 8751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private boolean rw; 8851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 891777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak /** 901777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * The path of the referenced file 911777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * (null if the stream is created with a file descriptor) 921777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak */ 9351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private final String path; 9451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 9551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private Object closeLock = new Object(); 9651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private volatile boolean closed = false; 97b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer 98b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer // BEGIN Android-added: IoTracker. 99118de16ee0529955eaca8cc5ce864cac49598c86Shubham Ajmera /** 100118de16ee0529955eaca8cc5ce864cac49598c86Shubham Ajmera * A single tracker to track both read and write. The tracker resets when the operation 101118de16ee0529955eaca8cc5ce864cac49598c86Shubham Ajmera * performed is different from the operation last performed. 102118de16ee0529955eaca8cc5ce864cac49598c86Shubham Ajmera */ 103118de16ee0529955eaca8cc5ce864cac49598c86Shubham Ajmera private final IoTracker ioTracker = new IoTracker(); 104b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer // END Android-added: IoTracker. 10551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 10651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 10751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Creates a random access file stream to read from, and optionally 10851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to write to, a file with the specified name. A new 10951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * {@link FileDescriptor} object is created to represent the 11051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * connection to the file. 11151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 11251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The <tt>mode</tt> argument specifies the access mode with which the 11351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * file is to be opened. The permitted values and their meanings are as 11451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * specified for the <a 11551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * href="#mode"><tt>RandomAccessFile(File,String)</tt></a> constructor. 11651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 11751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 1181777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * If there is a security manager, its {@code checkRead} method 1191777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * is called with the {@code name} argument 12051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * as its argument to see if read access to the file is allowed. 12151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If the mode allows writing, the security manager's 1221777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code checkWrite} method 1231777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * is also called with the {@code name} argument 12451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * as its argument to see if write access to the file is allowed. 12551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 12651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param name the system-dependent filename 12751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param mode the access <a href="#mode">mode</a> 12851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException if the mode argument is not equal 12951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to one of <tt>"r"</tt>, <tt>"rw"</tt>, <tt>"rws"</tt>, or 13051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>"rwd"</tt> 13151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception FileNotFoundException 13251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if the mode is <tt>"r"</tt> but the given string does not 13351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * denote an existing regular file, or if the mode begins with 13451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>"rw"</tt> but the given string does not denote an 13551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * existing, writable regular file and a new regular file of 13651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that name cannot be created, or if some other error occurs 13751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * while opening or creating the file 13851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException if a security manager exists and its 1391777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code checkRead} method denies read access to the file 14051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or the mode is "rw" and the security manager's 1411777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code checkWrite} method denies write access to the file 14251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.SecurityException 14351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.SecurityManager#checkRead(java.lang.String) 14451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.SecurityManager#checkWrite(java.lang.String) 14551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @revised 1.4 14651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @spec JSR-51 14751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 14851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public RandomAccessFile(String name, String mode) 14951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws FileNotFoundException 15051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski { 15151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski this(name != null ? new File(name) : null, mode); 15251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 15351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 15451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 15551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Creates a random access file stream to read from, and optionally to 15651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * write to, the file specified by the {@link File} argument. A new {@link 15751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * FileDescriptor} object is created to represent this file connection. 15851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 1591777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * <p>The <a name="mode"><tt>mode</tt></a> argument specifies the access mode 16051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * in which the file is to be opened. The permitted values and their 16151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * meanings are: 16251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 1631777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * <table summary="Access mode permitted values and meanings"> 1641777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * <tr><th align="left">Value</th><th align="left">Meaning</th></tr> 16551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td valign="top"><tt>"r"</tt></td> 16651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <td> Open for reading only. Invoking any of the <tt>write</tt> 16751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * methods of the resulting object will cause an {@link 16851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.io.IOException} to be thrown. </td></tr> 16951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td valign="top"><tt>"rw"</tt></td> 17051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <td> Open for reading and writing. If the file does not already 17151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * exist then an attempt will be made to create it. </td></tr> 17251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td valign="top"><tt>"rws"</tt></td> 17351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <td> Open for reading and writing, as with <tt>"rw"</tt>, and also 17451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * require that every update to the file's content or metadata be 17551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * written synchronously to the underlying storage device. </td></tr> 17651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tr><td valign="top"><tt>"rwd" </tt></td> 17751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <td> Open for reading and writing, as with <tt>"rw"</tt>, and also 17851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * require that every update to the file's content be written 17951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * synchronously to the underlying storage device. </td></tr> 1801777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * </table> 18151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 18251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The <tt>"rws"</tt> and <tt>"rwd"</tt> modes work much like the {@link 18351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * java.nio.channels.FileChannel#force(boolean) force(boolean)} method of 18451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the {@link java.nio.channels.FileChannel} class, passing arguments of 18551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>true</tt> and <tt>false</tt>, respectively, except that they always 18651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * apply to every I/O operation and are therefore often more efficient. If 18751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the file resides on a local storage device then when an invocation of a 18851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method of this class returns it is guaranteed that all changes made to 18951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the file by that invocation will have been written to that device. This 19051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is useful for ensuring that critical information is not lost in the 19151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * event of a system crash. If the file does not reside on a local device 19251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * then no such guarantee is made. 19351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 1941777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * <p>The <tt>"rwd"</tt> mode can be used to reduce the number of I/O 19551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * operations performed. Using <tt>"rwd"</tt> only requires updates to the 19651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * file's content to be written to storage; using <tt>"rws"</tt> requires 19751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * updates to both the file's content and its metadata to be written, which 19851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * generally requires at least one more low-level I/O operation. 19951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 2001777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * <p>If there is a security manager, its {@code checkRead} method is 2011777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * called with the pathname of the {@code file} argument as its 20251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument to see if read access to the file is allowed. If the mode 2031777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * allows writing, the security manager's {@code checkWrite} method is 20451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * also called with the path argument to see if write access to the file is 20551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * allowed. 20651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 20751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param file the file object 20851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param mode the access mode, as described 20951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <a href="#mode">above</a> 21051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IllegalArgumentException if the mode argument is not equal 21151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to one of <tt>"r"</tt>, <tt>"rw"</tt>, <tt>"rws"</tt>, or 21251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <tt>"rwd"</tt> 21351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception FileNotFoundException 21451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * if the mode is <tt>"r"</tt> but the given file object does 21551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not denote an existing regular file, or if the mode begins 21651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * with <tt>"rw"</tt> but the given file object does not denote 21751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * an existing, writable regular file and a new regular file of 21851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * that name cannot be created, or if some other error occurs 21951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * while opening or creating the file 22051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception SecurityException if a security manager exists and its 2211777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code checkRead} method denies read access to the file 22251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or the mode is "rw" and the security manager's 2231777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code checkWrite} method denies write access to the file 22451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.SecurityManager#checkRead(java.lang.String) 22551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.SecurityManager#checkWrite(java.lang.String) 22651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.nio.channels.FileChannel#force(boolean) 22751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @revised 1.4 22851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @spec JSR-51 22951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 23051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public RandomAccessFile(File file, String mode) 23151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throws FileNotFoundException 23251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski { 23351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski String name = (file != null ? file.getPath() : null); 234b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer int imode = -1; 2358d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski if (mode.equals("r")) { 236b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer imode = O_RDONLY; 2378d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski } else if (mode.startsWith("rw")) { 2386975f84c2ed72e1e26d20190b6f318718c849008Tobias Thierer // Android-changed: Added. O_CREAT 239b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer // imode = O_RDWR; 240b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer imode = O_RDWR | O_CREAT; 24151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski rw = true; 24251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (mode.length() > 2) { 2438d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski if (mode.equals("rws")) { 24453446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak // Android-changed: For performance reasons, use fsync after each write. 24553446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak // RandomAccessFile.write may result in multiple write syscalls, 24653446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak // O_SYNC/O_DSYNC flags will cause a blocking wait on each syscall. Replacing 24753446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak // them with single fsync/fdatasync call gives better performance with only 24853446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak // minor decrease in reliability. 24953446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak // imode |= O_SYNC; 25053446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak flushAfterWrite = FLUSH_FSYNC; 251c4ea41c37d2095f819e46d099ef0af0ccddcf33bPrzemyslaw Szczepaniak } else if (mode.equals("rwd")) { 25253446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak // Android-changed: For performance reasons, use fdatasync after each write. 25353446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak // imode |= O_DSYNC; 25453446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak flushAfterWrite = FLUSH_FDATASYNC; 2558d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski } else { 256b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer imode = -1; 2578d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski } 25851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 25951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 260b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer if (imode < 0) { 26151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new IllegalArgumentException("Illegal mode \"" + mode 26251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski + "\" must be one of " 26351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski + "\"r\", \"rw\", \"rws\"," 26451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski + " or \"rwd\""); 26551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 266b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer // Android-removed: do not use legacy security code 267b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer /* 268b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer SecurityManager security = System.getSecurityManager(); 269b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer if (security != null) { 270b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer security.checkRead(name); 271b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer if (rw) { 272b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer security.checkWrite(name); 273b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer } 274b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer } 275b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer */ 27651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (name == null) { 277b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer // Android-changed: different exception message in ctor when file == null. 278b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer // throw new NullPointerException(); 2798d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski throw new NullPointerException("file == null"); 28051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 28151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (file.isInvalid()) { 28251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new FileNotFoundException("Invalid file path"); 28351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 28451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski this.path = name; 285b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer this.mode = imode; 2868d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski 287b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer // BEGIN Android-changed: Use IoBridge.open() instead of open. 288b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer fd = IoBridge.open(name, imode); 28953446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak maybeSync(); 2908d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski guard.open("close"); 291b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer // END Android-changed: Use IoBridge.open() instead of open. 29251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 29351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 29453446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak // BEGIN Android-added: Sync after rws/rwd write 29553446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak private void maybeSync() { 29653446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak if (flushAfterWrite == FLUSH_FSYNC) { 29753446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak try { 29853446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak fd.sync(); 29953446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak } catch (IOException e) { 30053446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak // Ignored 30153446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak } 30253446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak } else if (flushAfterWrite == FLUSH_FDATASYNC) { 30353446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak try { 30453446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak Os.fdatasync(fd); 30553446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak } catch (ErrnoException e) { 30653446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak // Ignored 30753446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak } 30853446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak } 30953446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak } 31053446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak // END Android-added: Sync after rws/rwd write 31153446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak 31251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 31351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the opaque file descriptor object associated with this 3141777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * stream. 31551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 31651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the file descriptor object associated with this stream. 31751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O error occurs. 31851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.io.FileDescriptor 31951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 32051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final FileDescriptor getFD() throws IOException { 3211777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak if (fd != null) { 3221777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak return fd; 3231777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak } 32451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new IOException(); 32551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 32651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 32751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 32851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the unique {@link java.nio.channels.FileChannel FileChannel} 32951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * object associated with this file. 33051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 33151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> The {@link java.nio.channels.FileChannel#position() 3321777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * position} of the returned channel will always be equal to 33351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this object's file-pointer offset as returned by the {@link 33451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * #getFilePointer getFilePointer} method. Changing this object's 33551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * file-pointer offset, whether explicitly or by reading or writing bytes, 33651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * will change the position of the channel, and vice versa. Changing the 33751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * file's length via this object will change the length seen via the file 33851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * channel, and vice versa. 33951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 34051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the file channel associated with this file 34151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 34251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.4 34351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @spec JSR-51 34451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 34551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final FileChannel getChannel() { 34651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski synchronized (this) { 34751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (channel == null) { 3480f7f0c999e751ab883a247e1409dcc8730c87b9fPrzemyslaw Szczepaniak channel = FileChannelImpl.open(fd, path, true, rw, this); 34951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 35051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return channel; 35151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 35251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 35351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 35451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 35551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Reads a byte of data from this file. The byte is returned as an 3561777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * integer in the range 0 to 255 ({@code 0x00-0x0ff}). This 35751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * method blocks if no input is yet available. 35851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 3591777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * Although {@code RandomAccessFile} is not a subclass of 3601777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code InputStream}, this method behaves in exactly the same 36151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * way as the {@link InputStream#read()} method of 3621777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code InputStream}. 36351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 3641777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * @return the next byte of data, or {@code -1} if the end of the 36551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * file has been reached. 36651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O error occurs. Not thrown if 36751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * end-of-file has been reached. 36851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 36951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public int read() throws IOException { 37053446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak // Android-changed: Implement on top of libcore os API. 371b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer // return read0(); 3728d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski return (read(scratch, 0, 1) != -1) ? scratch[0] & 0xff : -1; 37351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 37451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 37551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 37651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Reads a sub array as a sequence of bytes. 37751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param b the buffer into which the data is read. 37851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param off the start offset of the data. 37951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param len the number of bytes to read. 38051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException If an I/O error has occurred. 38151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 38251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private int readBytes(byte b[], int off, int len) throws IOException { 38353446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak // Android-changed: Implement on top of libcore os API. 384118de16ee0529955eaca8cc5ce864cac49598c86Shubham Ajmera ioTracker.trackIo(len, IoTracker.Mode.READ); 3858d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski return IoBridge.read(fd, b, off, len); 38651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 38751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 38851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 3891777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * Reads up to {@code len} bytes of data from this file into an 39051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array of bytes. This method blocks until at least one byte of input 39151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is available. 39251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 3931777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * Although {@code RandomAccessFile} is not a subclass of 3941777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code InputStream}, this method behaves in exactly the 39551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * same way as the {@link InputStream#read(byte[], int, int)} method of 3961777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code InputStream}. 39751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 39851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param b the buffer into which the data is read. 3991777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * @param off the start offset in array {@code b} 40051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * at which the data is written. 40151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param len the maximum number of bytes read. 40251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the total number of bytes read into the buffer, or 4031777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code -1} if there is no more data because the end of 40451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the file has been reached. 40551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException If the first byte cannot be read for any reason 40651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * other than end of file, or if the random access file has been closed, or if 40751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * some other I/O error occurs. 4081777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * @exception NullPointerException If {@code b} is {@code null}. 4091777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * @exception IndexOutOfBoundsException If {@code off} is negative, 4101777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code len} is negative, or {@code len} is greater than 4111777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code b.length - off} 41251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 41351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public int read(byte b[], int off, int len) throws IOException { 41451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return readBytes(b, off, len); 41551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 41651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 41751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 4181777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * Reads up to {@code b.length} bytes of data from this file 41951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * into an array of bytes. This method blocks until at least one byte 42051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of input is available. 42151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 4221777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * Although {@code RandomAccessFile} is not a subclass of 4231777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code InputStream}, this method behaves in exactly the 42451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * same way as the {@link InputStream#read(byte[])} method of 4251777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code InputStream}. 42651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 42751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param b the buffer into which the data is read. 42851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the total number of bytes read into the buffer, or 4291777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code -1} if there is no more data because the end of 43051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * this file has been reached. 43151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException If the first byte cannot be read for any reason 43251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * other than end of file, or if the random access file has been closed, or if 43351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * some other I/O error occurs. 4341777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * @exception NullPointerException If {@code b} is {@code null}. 43551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 43651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public int read(byte b[]) throws IOException { 43751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return readBytes(b, 0, b.length); 43851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 43951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 44051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 4411777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * Reads {@code b.length} bytes from this file into the byte 44251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array, starting at the current file pointer. This method reads 44351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * repeatedly from the file until the requested number of bytes are 44451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * read. This method blocks until the requested number of bytes are 44551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * read, the end of the stream is detected, or an exception is thrown. 44651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 44751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param b the buffer into which the data is read. 44851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception EOFException if this file reaches the end before reading 44951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * all the bytes. 45051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O error occurs. 45151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 45251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final void readFully(byte b[]) throws IOException { 45351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski readFully(b, 0, b.length); 45451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 45551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 45651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 4571777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * Reads exactly {@code len} bytes from this file into the byte 45851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * array, starting at the current file pointer. This method reads 45951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * repeatedly from the file until the requested number of bytes are 46051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * read. This method blocks until the requested number of bytes are 46151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * read, the end of the stream is detected, or an exception is thrown. 46251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 46351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param b the buffer into which the data is read. 46451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param off the start offset of the data. 46551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param len the number of bytes to read. 46651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception EOFException if this file reaches the end before reading 46751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * all the bytes. 46851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O error occurs. 46951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 47051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final void readFully(byte b[], int off, int len) throws IOException { 47151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int n = 0; 47251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski do { 47351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int count = this.read(b, off + n, len - n); 47451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (count < 0) 47551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new EOFException(); 47651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski n += count; 47751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } while (n < len); 47851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 47951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 48051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 4811777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * Attempts to skip over {@code n} bytes of input discarding the 48251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * skipped bytes. 48351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 48451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 48551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This method may skip over some smaller number of bytes, possibly zero. 48651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This may result from any of a number of conditions; reaching end of 4871777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * file before {@code n} bytes have been skipped is only one 4881777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * possibility. This method never throws an {@code EOFException}. 4891777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * The actual number of bytes skipped is returned. If {@code n} 49051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is negative, no bytes are skipped. 49151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 49251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param n the number of bytes to be skipped. 49351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the actual number of bytes skipped. 49451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O error occurs. 49551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 49651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public int skipBytes(int n) throws IOException { 49751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski long pos; 49851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski long len; 49951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski long newpos; 50051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 50151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (n <= 0) { 50251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return 0; 50351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 50451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski pos = getFilePointer(); 50551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski len = length(); 50651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski newpos = pos + n; 50751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (newpos > len) { 50851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski newpos = len; 50951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 51051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski seek(newpos); 51151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 51251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /* return the actual number of bytes skipped */ 51351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (int) (newpos - pos); 51451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 51551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 51651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // 'Write' primitives 51751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 51851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 51951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Writes the specified byte to this file. The write starts at 52051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the current file pointer. 52151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 5221777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * @param b the {@code byte} to be written. 52351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O error occurs. 52451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 52551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public void write(int b) throws IOException { 52653446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak // BEGIN Android-changed: Implement on top of libcore os API. 527b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer // write0(b); 5288d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski scratch[0] = (byte) (b & 0xff); 5298d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski write(scratch, 0, 1); 53053446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak // END Android-changed: Implement on top of libcore os API. 53151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 53251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 53351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 53451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Writes a sub array as a sequence of bytes. 53551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param b the data to be written 536b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer 53751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param off the start offset in the data 53851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param len the number of bytes that are written 53951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException If an I/O error has occurred. 54051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 54151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski private void writeBytes(byte b[], int off, int len) throws IOException { 54253446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak // BEGIN Android-changed: Implement on top of libcore os API. 543118de16ee0529955eaca8cc5ce864cac49598c86Shubham Ajmera ioTracker.trackIo(len, IoTracker.Mode.WRITE); 5448d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski IoBridge.write(fd, b, off, len); 54553446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak maybeSync(); 54653446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak // END Android-changed: Implement on top of libcore os API. 54751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 54851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 54951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 5501777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * Writes {@code b.length} bytes from the specified byte array 55151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * to this file, starting at the current file pointer. 55251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 55351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param b the data. 55451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O error occurs. 55551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 55651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public void write(byte b[]) throws IOException { 55751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski writeBytes(b, 0, b.length); 55851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 55951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 56051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 5611777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * Writes {@code len} bytes from the specified byte array 5621777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * starting at offset {@code off} to this file. 56351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 56451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param b the data. 56551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param off the start offset in the data. 56651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param len the number of bytes to write. 56751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O error occurs. 56851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 56951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public void write(byte b[], int off, int len) throws IOException { 57051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski writeBytes(b, off, len); 57151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 57251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 57351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // 'Random access' stuff 57451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 57551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 57651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the current offset in this file. 57751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 57851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the offset from the beginning of the file, in bytes, 57951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * at which the next read or write occurs. 58051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O error occurs. 58151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 5828d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski public long getFilePointer() throws IOException { 58353446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak // BEGIN Android-changed: Implement on top of libcore os API. 5848d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski try { 5858d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski return Libcore.os.lseek(fd, 0L, SEEK_CUR); 5868d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski } catch (ErrnoException errnoException) { 5878d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski throw errnoException.rethrowAsIOException(); 5888d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski } 58953446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak // END Android-changed: Implement on top of libcore os API. 5908d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski } 59151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 59251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 59351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sets the file-pointer offset, measured from the beginning of this 59451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * file, at which the next read or write occurs. The offset may be 59551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * set beyond the end of the file. Setting the offset beyond the end 59651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the file does not change the file length. The file length will 59751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * change only by writing after the offset has been set beyond the end 59851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the file. 59951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 600b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer * @param pos the offset position, measured in bytes from the 60151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * beginning of the file, at which to set the file 60251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * pointer. 6031777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * @exception IOException if {@code pos} is less than 6041777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code 0} or if an I/O error occurs. 60551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 606b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer public void seek(long pos) throws IOException { 607b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer if (pos < 0) { 608b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer // Android-changed: different exception message for seek(-1). 609b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer // throw new IOException("Negative seek offset"); 610b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer throw new IOException("offset < 0: " + pos); 611b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer } else { 61253446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak // BEGIN Android-changed: Implement on top of libcore os API. 613b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer // seek0(pos); 614b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer try { 615b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer Libcore.os.lseek(fd, pos, SEEK_SET); 616b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer ioTracker.reset(); 617b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer } catch (ErrnoException errnoException) { 618b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer throw errnoException.rethrowAsIOException(); 619b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer } 62053446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak // END Android-changed: Implement on top of libcore os API. 6218d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski } 6228d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski } 62351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 62451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 62551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Returns the length of this file. 62651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 62751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the length of this file, measured in bytes. 62851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O error occurs. 62951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 6308d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski public long length() throws IOException { 63153446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak // BEGIN Android-changed: Implement on top of libcore os API. 6328d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski try { 6338d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski return Libcore.os.fstat(fd).st_size; 6348d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski } catch (ErrnoException errnoException) { 6358d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski throw errnoException.rethrowAsIOException(); 6368d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski } 63753446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak // END Android-changed: Implement on top of libcore os API. 6388d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski } 63951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 64051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 64151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Sets the length of this file. 64251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 64351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If the present length of the file as returned by the 6441777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code length} method is greater than the {@code newLength} 64551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument then the file will be truncated. In this case, if the file 6461777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * offset as returned by the {@code getFilePointer} method is greater 6471777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * than {@code newLength} then after this method returns the offset 6481777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * will be equal to {@code newLength}. 64951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 65051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If the present length of the file as returned by the 6511777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code length} method is smaller than the {@code newLength} 65251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * argument then the file will be extended. In this case, the contents of 65351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the extended portion of the file are not defined. 65451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 65551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param newLength The desired length of the file 65651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException If an I/O error occurs 65751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @since 1.2 65851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 6598d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski public void setLength(long newLength) throws IOException { 66053446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak // BEGIN Android-changed: Implement on top of libcore os API. 6618d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski if (newLength < 0) { 6628d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski throw new IllegalArgumentException("newLength < 0"); 6638d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski } 6648d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski try { 6658d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski Libcore.os.ftruncate(fd, newLength); 6668d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski } catch (ErrnoException errnoException) { 6678d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski throw errnoException.rethrowAsIOException(); 6688d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski } 6698d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski 6708d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski long filePointer = getFilePointer(); 6718d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski if (filePointer > newLength) { 6728d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski seek(newLength); 6738d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski } 67453446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak maybeSync(); 67553446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak // END Android-changed: Implement on top of libcore os API. 6768d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski } 6778d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski 67851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 67951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 68051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Closes this random access file stream and releases any system 68151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * resources associated with the stream. A closed random access 68251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * file cannot perform input or output operations and cannot be 68351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * reopened. 68451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 68551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> If this file has an associated channel then the channel is closed 68651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * as well. 68751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 68851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O error occurs. 68951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 69051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @revised 1.4 69151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @spec JSR-51 69251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 69351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public void close() throws IOException { 6948d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski guard.close(); 69551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski synchronized (closeLock) { 69651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (closed) { 69751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return; 69851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 69951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski closed = true; 70051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 7018d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski 70253446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak // BEGIN Android-changed: Implement on top of libcore os API. 7038d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski if (channel != null && channel.isOpen()) { 70451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski channel.close(); 70551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 7068d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski IoBridge.closeAndSignalBlockedThreads(fd); 70753446a42587eb0559c224523b4ead41820e43d30Przemyslaw Szczepaniak // END Android-changed: Implement on top of libcore os API. 70851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 70951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 71051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // 71151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // Some "reading/writing Java data types" methods stolen from 71251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // DataInputStream and DataOutputStream. 71351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski // 71451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 71551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 7161777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * Reads a {@code boolean} from this file. This method reads a 71751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * single byte from the file, starting at the current file pointer. 7181777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * A value of {@code 0} represents 7191777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code false}. Any other value represents {@code true}. 72051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This method blocks until the byte is read, the end of the stream 72151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is detected, or an exception is thrown. 72251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 7231777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * @return the {@code boolean} value read. 72451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception EOFException if this file has reached the end. 72551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O error occurs. 72651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 72751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final boolean readBoolean() throws IOException { 72851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int ch = this.read(); 72951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (ch < 0) 73051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new EOFException(); 73151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (ch != 0); 73251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 73351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 73451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 73551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Reads a signed eight-bit value from this file. This method reads a 73651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * byte from the file, starting from the current file pointer. 7371777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * If the byte read is {@code b}, where 73851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <code>0 <= b <= 255</code>, 73951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * then the result is: 74051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote><pre> 74151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (byte)(b) 74251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </pre></blockquote> 74351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 74451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This method blocks until the byte is read, the end of the stream 74551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is detected, or an exception is thrown. 74651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 74751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the next byte of this file as a signed eight-bit 7481777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code byte}. 74951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception EOFException if this file has reached the end. 75051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O error occurs. 75151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 75251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final byte readByte() throws IOException { 75351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int ch = this.read(); 75451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (ch < 0) 75551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new EOFException(); 75651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (byte)(ch); 75751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 75851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 75951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 76051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Reads an unsigned eight-bit number from this file. This method reads 76151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * a byte from this file, starting at the current file pointer, 76251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and returns that byte. 76351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 76451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This method blocks until the byte is read, the end of the stream 76551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * is detected, or an exception is thrown. 76651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 76751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the next byte of this file, interpreted as an unsigned 76851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * eight-bit number. 76951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception EOFException if this file has reached the end. 77051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O error occurs. 77151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 77251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final int readUnsignedByte() throws IOException { 77351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int ch = this.read(); 77451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if (ch < 0) 77551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new EOFException(); 77651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return ch; 77751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 77851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 77951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 78051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Reads a signed 16-bit number from this file. The method reads two 78151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * bytes from this file, starting at the current file pointer. 78251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If the two bytes read, in order, are 7831777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code b1} and {@code b2}, where each of the two values is 7841777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * between {@code 0} and {@code 255}, inclusive, then the 78551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * result is equal to: 78651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote><pre> 78751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (short)((b1 << 8) | b2) 78851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </pre></blockquote> 78951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 79051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This method blocks until the two bytes are read, the end of the 79151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * stream is detected, or an exception is thrown. 79251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 79351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the next two bytes of this file, interpreted as a signed 79451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 16-bit number. 79551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception EOFException if this file reaches the end before reading 79651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * two bytes. 79751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O error occurs. 79851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 79951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final short readShort() throws IOException { 80051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int ch1 = this.read(); 80151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int ch2 = this.read(); 80251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if ((ch1 | ch2) < 0) 80351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new EOFException(); 80451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (short)((ch1 << 8) + (ch2 << 0)); 80551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 80651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 80751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 80851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Reads an unsigned 16-bit number from this file. This method reads 80951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * two bytes from the file, starting at the current file pointer. 81051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If the bytes read, in order, are 8111777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code b1} and {@code b2}, where 81251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <code>0 <= b1, b2 <= 255</code>, 81351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * then the result is equal to: 81451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote><pre> 81551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (b1 << 8) | b2 81651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </pre></blockquote> 81751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 81851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This method blocks until the two bytes are read, the end of the 81951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * stream is detected, or an exception is thrown. 82051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 82151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the next two bytes of this file, interpreted as an unsigned 82251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 16-bit integer. 82351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception EOFException if this file reaches the end before reading 82451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * two bytes. 82551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O error occurs. 82651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 82751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final int readUnsignedShort() throws IOException { 82851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int ch1 = this.read(); 82951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int ch2 = this.read(); 83051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if ((ch1 | ch2) < 0) 83151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new EOFException(); 83251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (ch1 << 8) + (ch2 << 0); 83351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 83451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 83551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 83651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Reads a character from this file. This method reads two 83751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * bytes from the file, starting at the current file pointer. 83851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If the bytes read, in order, are 8391777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code b1} and {@code b2}, where 84051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <code>0 <= b1, b2 <= 255</code>, 84151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * then the result is equal to: 84251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote><pre> 84351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (char)((b1 << 8) | b2) 84451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </pre></blockquote> 84551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 84651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This method blocks until the two bytes are read, the end of the 84751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * stream is detected, or an exception is thrown. 84851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 84951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the next two bytes of this file, interpreted as a 8501777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code char}. 85151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception EOFException if this file reaches the end before reading 85251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * two bytes. 85351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O error occurs. 85451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 85551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final char readChar() throws IOException { 85651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int ch1 = this.read(); 85751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int ch2 = this.read(); 85851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if ((ch1 | ch2) < 0) 85951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new EOFException(); 86051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return (char)((ch1 << 8) + (ch2 << 0)); 86151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 86251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 86351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 86451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Reads a signed 32-bit integer from this file. This method reads 4 86551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * bytes from the file, starting at the current file pointer. 8661777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * If the bytes read, in order, are {@code b1}, 8671777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code b2}, {@code b3}, and {@code b4}, where 86851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <code>0 <= b1, b2, b3, b4 <= 255</code>, 86951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * then the result is equal to: 87051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote><pre> 87151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * (b1 << 24) | (b2 << 16) + (b3 << 8) + b4 87251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </pre></blockquote> 87351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 87451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This method blocks until the four bytes are read, the end of the 87551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * stream is detected, or an exception is thrown. 87651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 87751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the next four bytes of this file, interpreted as an 8781777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code int}. 87951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception EOFException if this file reaches the end before reading 88051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * four bytes. 88151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O error occurs. 88251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 88351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final int readInt() throws IOException { 88451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int ch1 = this.read(); 88551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int ch2 = this.read(); 88651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int ch3 = this.read(); 88751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int ch4 = this.read(); 88851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if ((ch1 | ch2 | ch3 | ch4) < 0) 88951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski throw new EOFException(); 89051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return ((ch1 << 24) + (ch2 << 16) + (ch3 << 8) + (ch4 << 0)); 89151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 89251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 89351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 89451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Reads a signed 64-bit integer from this file. This method reads eight 89551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * bytes from the file, starting at the current file pointer. 89651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * If the bytes read, in order, are 8971777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code b1}, {@code b2}, {@code b3}, 8981777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code b4}, {@code b5}, {@code b6}, 8991777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code b7}, and {@code b8,} where: 90051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <blockquote><pre> 90151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 0 <= b1, b2, b3, b4, b5, b6, b7, b8 <=255, 90251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </pre></blockquote> 90351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 90451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * then the result is equal to: 9051777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * <blockquote><pre> 90651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * ((long)b1 << 56) + ((long)b2 << 48) 90751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * + ((long)b3 << 40) + ((long)b4 << 32) 90851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * + ((long)b5 << 24) + ((long)b6 << 16) 90951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * + ((long)b7 << 8) + b8 91051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * </pre></blockquote> 91151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 91251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This method blocks until the eight bytes are read, the end of the 91351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * stream is detected, or an exception is thrown. 91451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 91551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the next eight bytes of this file, interpreted as a 9161777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code long}. 91751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception EOFException if this file reaches the end before reading 91851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * eight bytes. 91951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O error occurs. 92051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 92151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final long readLong() throws IOException { 92251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return ((long)(readInt()) << 32) + (readInt() & 0xFFFFFFFFL); 92351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 92451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 92551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 9261777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * Reads a {@code float} from this file. This method reads an 9271777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code int} value, starting at the current file pointer, 9281777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * as if by the {@code readInt} method 9291777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * and then converts that {@code int} to a {@code float} 9301777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * using the {@code intBitsToFloat} method in class 9311777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code Float}. 93251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 93351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This method blocks until the four bytes are read, the end of the 93451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * stream is detected, or an exception is thrown. 93551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 93651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the next four bytes of this file, interpreted as a 9371777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code float}. 93851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception EOFException if this file reaches the end before reading 93951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * four bytes. 94051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O error occurs. 94151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.io.RandomAccessFile#readInt() 94251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Float#intBitsToFloat(int) 94351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 94451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final float readFloat() throws IOException { 94551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return Float.intBitsToFloat(readInt()); 94651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 94751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 94851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 9491777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * Reads a {@code double} from this file. This method reads a 9501777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code long} value, starting at the current file pointer, 9511777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * as if by the {@code readLong} method 9521777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * and then converts that {@code long} to a {@code double} 9531777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * using the {@code longBitsToDouble} method in 9541777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * class {@code Double}. 95551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 95651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This method blocks until the eight bytes are read, the end of the 95751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * stream is detected, or an exception is thrown. 95851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 95951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the next eight bytes of this file, interpreted as a 9601777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code double}. 96151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception EOFException if this file reaches the end before reading 96251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * eight bytes. 96351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O error occurs. 96451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.io.RandomAccessFile#readLong() 96551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Double#longBitsToDouble(long) 96651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 96751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final double readDouble() throws IOException { 96851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return Double.longBitsToDouble(readLong()); 96951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 97051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 97151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 97251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Reads the next line of text from this file. This method successively 97351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * reads bytes from the file, starting at the current file pointer, 97451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * until it reaches a line terminator or the end 97551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the file. Each byte is converted into a character by taking the 97651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * byte's value for the lower eight bits of the character and setting the 97751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * high eight bits of the character to zero. This method does not, 97851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * therefore, support the full Unicode character set. 97951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 98051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> A line of text is terminated by a carriage-return character 9811777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * ({@code '\u005Cr'}), a newline character ({@code '\u005Cn'}), a 98251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * carriage-return character immediately followed by a newline character, 98351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * or the end of the file. Line-terminating characters are discarded and 98451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * are not included as part of the string returned. 98551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 98651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> This method blocks until a newline character is read, a carriage 98751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * return and the byte following it are read (to see if it is a newline), 98851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the end of the file is reached, or an exception is thrown. 98951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 99051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return the next line of text from this file, or null if end 99151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of file is encountered before even one byte is read. 99251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O error occurs. 99351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 99451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 99551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final String readLine() throws IOException { 99651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski StringBuffer input = new StringBuffer(); 99751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int c = -1; 99851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski boolean eol = false; 99951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 100051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski while (!eol) { 100151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski switch (c = read()) { 100251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski case -1: 100351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski case '\n': 100451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski eol = true; 100551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski break; 100651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski case '\r': 100751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski eol = true; 100851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski long cur = getFilePointer(); 100951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if ((read()) != '\n') { 101051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski seek(cur); 101151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 101251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski break; 101351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski default: 101451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski input.append((char)c); 101551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski break; 101651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 101751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 101851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 101951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski if ((c == -1) && (input.length() == 0)) { 102051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return null; 102151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 102251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return input.toString(); 102351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 102451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 102551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 102651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Reads in a string from this file. The string has been encoded 102751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * using a 102851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <a href="DataInput.html#modified-utf-8">modified UTF-8</a> 102951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * format. 103051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 103151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The first two bytes are read, starting from the current file 103251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * pointer, as if by 10331777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code readUnsignedShort}. This value gives the number of 103451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * following bytes that are in the encoded string, not 103551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the length of the resulting string. The following bytes are then 103651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * interpreted as bytes encoding characters in the modified UTF-8 format 103751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * and are converted into characters. 103851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 103951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * This method blocks until all the bytes are read, the end of the 104051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * stream is detected, or an exception is thrown. 104151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 104251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @return a Unicode string. 104351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception EOFException if this file reaches the end before 104451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * reading all the bytes. 104551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O error occurs. 104651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception UTFDataFormatException if the bytes do not represent 104751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * valid modified UTF-8 encoding of a Unicode string. 104851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.io.RandomAccessFile#readUnsignedShort() 104951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 105051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final String readUTF() throws IOException { 105151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski return DataInputStream.readUTF(this); 105251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 105351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 105451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 10551777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * Writes a {@code boolean} to the file as a one-byte value. The 10561777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * value {@code true} is written out as the value 10571777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code (byte)1}; the value {@code false} is written out 10581777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * as the value {@code (byte)0}. The write starts at 105951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the current position of the file pointer. 106051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 10611777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * @param v a {@code boolean} value to be written. 106251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O error occurs. 106351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 106451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final void writeBoolean(boolean v) throws IOException { 106551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski write(v ? 1 : 0); 106651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski //written++; 106751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 106851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 106951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 10701777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * Writes a {@code byte} to the file as a one-byte value. The 107151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * write starts at the current position of the file pointer. 107251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 10731777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * @param v a {@code byte} value to be written. 107451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O error occurs. 107551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 107651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final void writeByte(int v) throws IOException { 107751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski write(v); 107851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski //written++; 107951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 108051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 108151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 10821777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * Writes a {@code short} to the file as two bytes, high byte first. 108351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The write starts at the current position of the file pointer. 108451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 10851777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * @param v a {@code short} to be written. 108651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O error occurs. 108751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 108851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final void writeShort(int v) throws IOException { 108951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski write((v >>> 8) & 0xFF); 109051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski write((v >>> 0) & 0xFF); 109151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski //written += 2; 109251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 109351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 109451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 10951777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * Writes a {@code char} to the file as a two-byte value, high 109651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * byte first. The write starts at the current position of the 109751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * file pointer. 109851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 10991777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * @param v a {@code char} value to be written. 110051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O error occurs. 110151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 110251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final void writeChar(int v) throws IOException { 110351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski write((v >>> 8) & 0xFF); 110451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski write((v >>> 0) & 0xFF); 110551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski //written += 2; 110651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 110751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 110851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 11091777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * Writes an {@code int} to the file as four bytes, high byte first. 111051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The write starts at the current position of the file pointer. 111151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 11121777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * @param v an {@code int} to be written. 111351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O error occurs. 111451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 111551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final void writeInt(int v) throws IOException { 111651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski write((v >>> 24) & 0xFF); 111751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski write((v >>> 16) & 0xFF); 111851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski write((v >>> 8) & 0xFF); 111951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski write((v >>> 0) & 0xFF); 112051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski //written += 4; 112151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 112251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 112351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 11241777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * Writes a {@code long} to the file as eight bytes, high byte first. 112551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * The write starts at the current position of the file pointer. 112651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 11271777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * @param v a {@code long} to be written. 112851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O error occurs. 112951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 113051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final void writeLong(long v) throws IOException { 113151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski write((int)(v >>> 56) & 0xFF); 113251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski write((int)(v >>> 48) & 0xFF); 113351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski write((int)(v >>> 40) & 0xFF); 113451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski write((int)(v >>> 32) & 0xFF); 113551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski write((int)(v >>> 24) & 0xFF); 113651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski write((int)(v >>> 16) & 0xFF); 113751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski write((int)(v >>> 8) & 0xFF); 113851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski write((int)(v >>> 0) & 0xFF); 113951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski //written += 8; 114051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 114151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 114251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 11431777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * Converts the float argument to an {@code int} using the 11441777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code floatToIntBits} method in class {@code Float}, 11451777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * and then writes that {@code int} value to the file as a 114651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * four-byte quantity, high byte first. The write starts at the 114751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * current position of the file pointer. 114851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 11491777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * @param v a {@code float} value to be written. 115051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O error occurs. 115151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Float#floatToIntBits(float) 115251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 115351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final void writeFloat(float v) throws IOException { 115451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski writeInt(Float.floatToIntBits(v)); 115551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 115651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 115751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 11581777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * Converts the double argument to a {@code long} using the 11591777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code doubleToLongBits} method in class {@code Double}, 11601777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * and then writes that {@code long} value to the file as an 116151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * eight-byte quantity, high byte first. The write starts at the current 116251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * position of the file pointer. 116351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 11641777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * @param v a {@code double} value to be written. 116551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O error occurs. 116651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.lang.Double#doubleToLongBits(double) 116751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 116851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final void writeDouble(double v) throws IOException { 116951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski writeLong(Double.doubleToLongBits(v)); 117051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 117151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 117251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 117351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Writes the string to the file as a sequence of bytes. Each 117451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * character in the string is written out, in sequence, by discarding 117551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * its high eight bits. The write starts at the current position of 117651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * the file pointer. 117751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 117851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param s a string of bytes to be written. 117951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O error occurs. 118051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 11811777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak @SuppressWarnings("deprecation") 118251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final void writeBytes(String s) throws IOException { 118351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int len = s.length(); 118451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski byte[] b = new byte[len]; 118551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski s.getBytes(0, len, b, 0); 118651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski writeBytes(b, 0, len); 118751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 118851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 118951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 119051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Writes a string to the file as a sequence of characters. Each 119151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * character is written to the data output stream as if by the 11921777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code writeChar} method. The write starts at the current 119351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * position of the file pointer. 119451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 11951777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * @param s a {@code String} value to be written. 119651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O error occurs. 119751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @see java.io.RandomAccessFile#writeChar(int) 119851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 119951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final void writeChars(String s) throws IOException { 120051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int clen = s.length(); 120151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski int blen = 2*clen; 120251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski byte[] b = new byte[blen]; 120351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski char[] c = new char[clen]; 120451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski s.getChars(0, clen, c, 0); 120551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski for (int i = 0, j = 0; i < clen; i++) { 120651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b[j++] = (byte)(c[i] >>> 8); 120751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski b[j++] = (byte)(c[i] >>> 0); 120851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 120951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski writeBytes(b, 0, blen); 121051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 121151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 121251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski /** 121351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * Writes a string to the file using 121451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <a href="DataInput.html#modified-utf-8">modified UTF-8</a> 121551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * encoding in a machine-independent manner. 121651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * <p> 121751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * First, two bytes are written to the file, starting at the 121851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * current file pointer, as if by the 12191777d314864b48924136822b29aabffeb8a367c1Przemyslaw Szczepaniak * {@code writeShort} method giving the number of bytes to 122051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * follow. This value is the number of bytes actually written out, 122151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * not the length of the string. Following the length, each character 122251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * of the string is output, in sequence, using the modified UTF-8 encoding 122351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * for each character. 122451b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * 122551b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @param str a string to be written. 122651b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski * @exception IOException if an I/O error occurs. 122751b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski */ 122851b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski public final void writeUTF(String str) throws IOException { 122951b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski DataOutputStream.writeUTF(str, this); 123051b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 123151b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski 1232b9ea7752d6cfb3e31e66b70db98ac796ee74c8c0Tobias Thierer // Android-added: use finalize() to detect if not close()d. 12338d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski @Override protected void finalize() throws Throwable { 12348d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski try { 12358d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski if (guard != null) { 12368d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski guard.warnIfOpen(); 12378d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski } 12388d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski close(); 12398d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski } finally { 12408d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski super.finalize(); 12418d05e88f57c1ea5543d4012687c70cd64efcada0Piotr Jastrzebski } 124251b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski } 124351b1b6997fd3f980076b8081f7f1165ccc2a4008Piotr Jastrzebski} 1244