Java Code Examples for libcore.io.IoUtils#close()

The following examples show how to use libcore.io.IoUtils#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FileInputStream.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Override
public void close() throws IOException {
    guard.close();
    synchronized (this) {
        if (channel != null) {
            channel.close();
        }
        if (shouldClose) {
            IoUtils.close(fd);
        } else {
            // An owned fd has been invalidated by IoUtils.close, but
            // we need to explicitly stop using an unowned fd (http://b/4361076).
            fd = new FileDescriptor();
        }
    }
}
 
Example 2
Source File: RandomAccessFile.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Closes this file.
 *
 * @throws IOException
 *             if an error occurs while closing this file.
 */
public void close() throws IOException {
    guard.close();
    synchronized (this) {
        if (channel != null && channel.isOpen()) {
            channel.close();

            // j2objc: since a @RetainedWith field cannot be reassigned, the following line is
            // commented out. If a channel is already closed, the predicate above will be false
            // and so this branch will not be taken. FileInputStream and FileOutputStream
            // simply call channel.close() when channel is not null witout even checking if
            // it's still open (and that's ok).
            // channel = null;
        }
        IoUtils.close(fd);
    }
}
 
Example 3
Source File: FileOutputStream.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Override
public void close() throws IOException {
    guard.close();
    synchronized (this) {
        if (channel != null) {
            channel.close();
        }
        if (shouldClose) {
            IoUtils.close(fd);
        } else {
            // An owned fd has been invalidated by IoUtils.close, but
            // we need to explicitly stop using an unowned fd (http://b/4361076).
            fd = new FileDescriptor();
        }
    }
}
 
Example 4
Source File: File.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a new, empty file on the file system according to the path
 * information stored in this file. This method returns true if it creates
 * a file, false if the file already existed. Note that it returns false
 * even if the file is not a file (because it's a directory, say).
 *
 * <p>This method is not generally useful. For creating temporary files,
 * use {@link #createTempFile} instead. For reading/writing files, use {@link FileInputStream},
 * {@link FileOutputStream}, or {@link RandomAccessFile}, all of which can create files.
 *
 * <p>Note that this method does <i>not</i> throw {@code IOException} if the file
 * already exists, even if it's not a regular file. Callers should always check the
 * return value, and may additionally want to call {@link #isFile}.
 *
 * @return true if the file has been created, false if it
 *         already exists.
 * @throws IOException if it's not possible to create the file.
 */
public boolean createNewFile() throws IOException {
    if (0 == path.length()) {
        throw new IOException("No such file or directory");
    }
    if (isDirectory()) {  // true for paths like "dir/..", which can't be files.
        throw new IOException("Cannot create: " + path);
    }
    FileDescriptor fd = null;
    try {
        // On Android, we don't want default permissions to allow global access.
        fd = Libcore.os.open(path, O_RDWR | O_CREAT | O_EXCL, 0600);
        return true;
    } catch (ErrnoException errnoException) {
        if (errnoException.errno == EEXIST) {
            // The file already exists.
            return false;
        }
        throw errnoException.rethrowAsIOException();
    } finally {
        IoUtils.close(fd); // TODO: should we suppress IOExceptions thrown here?
    }
}
 
Example 5
Source File: FileInputStreamTest.java    From j2objc with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    try {
        FileOutputStream fos = new FileOutputStream(mOutFd);
        try {
            byte[] buffer = new byte[TOTAL_SIZE];
            for (int i = 0; i < buffer.length; ++i) {
                buffer[i] = (byte) i;
            }
            fos.write(buffer);
        } finally {
            IoUtils.closeQuietly(fos);
            IoUtils.close(mOutFd);
        }
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
}