FileChannelImpl.java revision fe5da19e0e366286cd4d95f7628fe9442b9062c8
1adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project/*
2adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  Licensed to the Apache Software Foundation (ASF) under one or more
3adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  contributor license agreements.  See the NOTICE file distributed with
4adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  this work for additional information regarding copyright ownership.
5adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  The ASF licenses this file to You under the Apache License, Version 2.0
6adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  (the "License"); you may not use this file except in compliance with
7adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  the License.  You may obtain a copy of the License at
8adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *
9adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *     http://www.apache.org/licenses/LICENSE-2.0
10adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *
11adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  Unless required by applicable law or agreed to in writing, software
12adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  distributed under the License is distributed on an "AS IS" BASIS,
13adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  See the License for the specific language governing permissions and
15adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project *  limitations under the License.
16adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */
17adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
18ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughespackage java.nio;
19adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
20adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.io.Closeable;
21adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.io.FileDescriptor;
22adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.io.IOException;
23adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.nio.channels.ClosedChannelException;
24adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.nio.channels.FileChannel;
25adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.nio.channels.FileLock;
266ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughesimport java.nio.channels.NonReadableChannelException;
27adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.nio.channels.NonWritableChannelException;
28ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughesimport java.nio.channels.OverlappingFileLockException;
29adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.nio.channels.ReadableByteChannel;
30adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.nio.channels.WritableByteChannel;
31a1603838fe9e865575c87982e32c6343740e464cElliott Hughesimport java.util.Arrays;
32ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughesimport java.util.Comparator;
33ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughesimport java.util.SortedSet;
34ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughesimport java.util.TreeSet;
3552724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughesimport libcore.io.ErrnoException;
3652724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughesimport libcore.io.Libcore;
37fc549a0b0388987b26dea524894d75a63d14783bElliott Hughesimport libcore.io.StructFlock;
388b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughesimport libcore.util.MutableLong;
396ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughesimport static libcore.io.OsConstants.*;
40adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
416fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes/**
426fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes * Our concrete implementation of the abstract FileChannel class.
43adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project */
446ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughesfinal class FileChannelImpl extends FileChannel {
45ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes    private static final Comparator<FileLock> LOCK_COMPARATOR = new Comparator<FileLock>() {
46ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes        public int compare(FileLock lock1, FileLock lock2) {
47ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes            long position1 = lock1.position();
48ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes            long position2 = lock2.position();
49ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes            return position1 > position2 ? 1 : (position1 < position2 ? -1 : 0);
50ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes        }
51ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes    };
52ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes
536ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes    private final Object stream;
5452724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    private final FileDescriptor fd;
556ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes    private final int mode;
56adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
57ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes    // The set of acquired and pending locks.
58ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes    private final SortedSet<FileLock> locks = new TreeSet<FileLock>(LOCK_COMPARATOR);
59adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
60ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes    /**
616ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes     * Create a new file channel implementation class that wraps the given
626ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes     * fd and operates in the specified mode.
63adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
6452724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    public FileChannelImpl(Object stream, FileDescriptor fd, int mode) {
656ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        this.fd = fd;
66adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        this.stream = stream;
676ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        this.mode = mode;
68adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
69adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
706ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes    private void checkOpen() throws ClosedChannelException {
71adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (!isOpen()) {
72adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            throw new ClosedChannelException();
73adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
74adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
75adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
766ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes    private void checkReadable() {
776ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        if ((mode & O_ACCMODE) == O_WRONLY) {
786ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes            throw new NonReadableChannelException();
796ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        }
806ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes    }
816ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes
826ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes    private void checkWritable() {
836ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        if ((mode & O_ACCMODE) == O_RDONLY) {
846ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes            throw new NonWritableChannelException();
856ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        }
866ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes    }
876ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes
88adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    protected void implCloseChannel() throws IOException {
89adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (stream instanceof Closeable) {
90adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            ((Closeable) stream).close();
91adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
92adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
93adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
946ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes    private FileLock basicLock(long position, long size, boolean shared, boolean wait) throws IOException {
956ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        int accessMode = (mode & O_ACCMODE);
966ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        if (accessMode == O_RDONLY) {
976ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes            if (!shared) {
986ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes                throw new NonWritableChannelException();
996ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes            }
1006ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        } else if (accessMode == O_WRONLY) {
1016ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes            if (shared) {
1026ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes                throw new NonReadableChannelException();
1036ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes            }
1046ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        }
1056ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes
10603c0a8e681c776fdba0389ab8593282139afc6d6Elliott Hughes        if (position < 0 || size < 0) {
1076ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes            throw new IllegalArgumentException("position=" + position + " size=" + size);
108adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
109fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
110adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        FileLock pendingLock = new FileLockImpl(this, position, size, shared);
111ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes        addLock(pendingLock);
112adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
113fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        StructFlock flock = new StructFlock();
114fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        flock.l_type = (short) (shared ? F_RDLCK : F_WRLCK);
115fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        flock.l_whence = (short) SEEK_SET;
116fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        flock.l_start = position;
117fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        flock.l_len = translateLockLength(size);
118adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
1199b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes        boolean success = false;
1209b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes        try {
1219b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes            success = (Libcore.os.fcntlFlock(fd, wait ? F_SETLKW64 : F_SETLK64, flock) != -1);
1229b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes        } catch (ErrnoException errnoException) {
1239b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes            throw errnoException.rethrowAsIOException();
1249b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes        } finally {
1259b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes            if (!success) {
1269b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes                removeLock(pendingLock);
1279b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes            }
1289b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes        }
1299b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes        return success ? pendingLock : null;
130fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    }
131fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
132fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    private static long translateLockLength(long byteCount) {
133fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        // FileChannel uses Long.MAX_VALUE to mean "lock the whole file" where POSIX uses 0.
134fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        return (byteCount == Long.MAX_VALUE) ? 0 : byteCount;
135adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
136adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
137ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes    private static final class FileLockImpl extends FileLock {
138ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes        private boolean isReleased = false;
139ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes
140ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes        public FileLockImpl(FileChannel channel, long position, long size, boolean shared) {
141ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes            super(channel, position, size, shared);
142ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes        }
143ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes
1446ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        public boolean isValid() {
145ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes            return !isReleased && channel().isOpen();
146ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes        }
147ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes
1486ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        public void release() throws IOException {
149ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes            if (!channel().isOpen()) {
150ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes                throw new ClosedChannelException();
151ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes            }
152ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes            if (!isReleased) {
153ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes                ((FileChannelImpl) channel()).release(this);
154ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes                isReleased = true;
155ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes            }
156ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes        }
157ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes    }
158ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes
159ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes    public final FileLock lock(long position, long size, boolean shared) throws IOException {
1606ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkOpen();
161adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        FileLock resultLock = null;
162adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        {
163adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            boolean completed = false;
164adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            try {
165adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                begin();
166adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                resultLock = basicLock(position, size, shared, true);
167adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                completed = true;
168adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            } finally {
169adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                end(completed);
170adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            }
171adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
172adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return resultLock;
173adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
174adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
175ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes    public final FileLock tryLock(long position, long size, boolean shared) throws IOException {
1766ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkOpen();
177adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return basicLock(position, size, shared, false);
178adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
179adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
180ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes    /**
181adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Non-API method to release a given lock on a file channel. Assumes that
182adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * the lock will mark itself invalid after successful unlocking.
183adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
1846ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes    public void release(FileLock lock) throws IOException {
1856ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkOpen();
186fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
187fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        StructFlock flock = new StructFlock();
188fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        flock.l_type = (short) F_UNLCK;
189fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        flock.l_whence = (short) SEEK_SET;
190fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        flock.l_start = lock.position();
191fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        flock.l_len = translateLockLength(lock.size());
192fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        try {
193fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes            Libcore.os.fcntlFlock(fd, F_SETLKW64, flock);
194fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        } catch (ErrnoException errnoException) {
195fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes            throw errnoException.rethrowAsIOException();
196fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        }
197fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
198ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes        removeLock(lock);
199adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
200adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
2016ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes    public void force(boolean metadata) throws IOException {
2026ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkOpen();
2036ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        if ((mode & O_ACCMODE) != O_RDONLY) {
20452724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes            try {
20552724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes                if (metadata) {
20652724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes                    Libcore.os.fsync(fd);
20752724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes                } else {
20852724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes                    Libcore.os.fdatasync(fd);
20952724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes                }
21052724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes            } catch (ErrnoException errnoException) {
211f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes                throw errnoException.rethrowAsIOException();
21252724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes            }
2136ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        }
214adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
215adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
2166ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes    public final MappedByteBuffer map(MapMode mapMode, long position, long size) throws IOException {
2176ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkOpen();
2186ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        if (mapMode == null) {
2196ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes            throw new NullPointerException("mapMode == null");
2206ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        }
2216ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        if (position < 0 || size < 0 || size > Integer.MAX_VALUE) {
2226ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes            throw new IllegalArgumentException("position=" + position + " size=" + size);
2236ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        }
2246ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        int accessMode = (mode & O_ACCMODE);
2256ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        if (accessMode == O_RDONLY) {
2266ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes            if (mapMode != MapMode.READ_ONLY) {
2276ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes                throw new NonWritableChannelException();
2286ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes            }
2296ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        } else if (accessMode == O_WRONLY) {
2306ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes            throw new NonReadableChannelException();
2316ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        }
232adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (position + size > size()) {
233c03e4ba8cd93513aabda061b00d516b54717c5fbElliott Hughes            // We can't defer to FileChannel.truncate because that will only make a file shorter,
234c03e4ba8cd93513aabda061b00d516b54717c5fbElliott Hughes            // and we only care about making our backing file longer here.
235c03e4ba8cd93513aabda061b00d516b54717c5fbElliott Hughes            try {
236c03e4ba8cd93513aabda061b00d516b54717c5fbElliott Hughes                Libcore.os.ftruncate(fd, position + size);
237c03e4ba8cd93513aabda061b00d516b54717c5fbElliott Hughes            } catch (ErrnoException errnoException) {
238385c6f4303341beb9b091b6d252811b7ca3b9f42Nick Kralevich                // EINVAL can be thrown if we're dealing with non-regular
239385c6f4303341beb9b091b6d252811b7ca3b9f42Nick Kralevich                // files, for example, character devices such as /dev/zero.
240385c6f4303341beb9b091b6d252811b7ca3b9f42Nick Kralevich                // In those cases, we ignore the failed truncation and
241385c6f4303341beb9b091b6d252811b7ca3b9f42Nick Kralevich                // continue on.
242385c6f4303341beb9b091b6d252811b7ca3b9f42Nick Kralevich                if (errnoException.errno != EINVAL) {
243385c6f4303341beb9b091b6d252811b7ca3b9f42Nick Kralevich                    throw errnoException.rethrowAsIOException();
244385c6f4303341beb9b091b6d252811b7ca3b9f42Nick Kralevich                }
245c03e4ba8cd93513aabda061b00d516b54717c5fbElliott Hughes            }
246adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
2476fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes        long alignment = position - position % Libcore.os.sysconf(_SC_PAGE_SIZE);
248adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        int offset = (int) (position - alignment);
2497e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        MemoryBlock block = MemoryBlock.mmap(fd, alignment, size + offset, mapMode);
250fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes        return new DirectByteBuffer(block, (int) size, offset, (mapMode == MapMode.READ_ONLY), mapMode);
251adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
252adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
253adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public long position() throws IOException {
2546ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkOpen();
255dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes        try {
256dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes            return Libcore.os.lseek(fd, 0L, SEEK_CUR);
257dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes        } catch (ErrnoException errnoException) {
258dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes            throw errnoException.rethrowAsIOException();
259dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes        }
260adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
261adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
262adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public FileChannel position(long newPosition) throws IOException {
2636ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkOpen();
264adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (newPosition < 0) {
2656ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes            throw new IllegalArgumentException("position: " + newPosition);
266eaa2ff09069424b0f7a95c7cd831cef1b744fe67Jesse Wilson        }
267e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        try {
268e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            Libcore.os.lseek(fd, newPosition, SEEK_SET);
269e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        } catch (ErrnoException errnoException) {
270e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            throw errnoException.rethrowAsIOException();
271adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
272adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return this;
273adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
274adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
275adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public int read(ByteBuffer buffer, long position) throws IOException {
276eaa2ff09069424b0f7a95c7cd831cef1b744fe67Jesse Wilson        if (position < 0) {
2776ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes            throw new IllegalArgumentException("position: " + position);
278adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
279e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        return readImpl(buffer, position);
280adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
281adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
282adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public int read(ByteBuffer buffer) throws IOException {
283e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        return readImpl(buffer, -1);
284e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes    }
285e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes
286e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes    private int readImpl(ByteBuffer buffer, long position) throws IOException {
287e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        buffer.checkWritable();
2886ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkOpen();
2896ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkReadable();
290eaa2ff09069424b0f7a95c7cd831cef1b744fe67Jesse Wilson        if (!buffer.hasRemaining()) {
291adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            return 0;
292adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
293e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        int bytesRead = 0;
294e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        boolean completed = false;
295e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        try {
296e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            begin();
29726c7025a7a919044771fb89031161bd26fe03032Elliott Hughes            try {
298e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                if (position == -1) {
29926c7025a7a919044771fb89031161bd26fe03032Elliott Hughes                    bytesRead = Libcore.os.read(fd, buffer);
300e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                } else {
301e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                    bytesRead = Libcore.os.pread(fd, buffer, position);
302e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                }
303e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                if (bytesRead == 0) {
304e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                    bytesRead = -1;
305e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                }
306e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            } catch (ErrnoException errnoException) {
307e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                if (errnoException.errno == EAGAIN) {
308e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                    // We don't throw if we try to read from an empty non-blocking pipe.
309e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                    bytesRead = 0;
310e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                } else {
311e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                    throw errnoException.rethrowAsIOException();
312adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                }
313adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            }
314e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            completed = true;
315e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        } finally {
316e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            end(completed && bytesRead >= 0);
317e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        }
318e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        if (bytesRead > 0) {
319e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            buffer.position(buffer.position() + bytesRead);
320adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
321e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        return bytesRead;
322adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
323adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
324bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    private int transferIoVec(IoVec ioVec) throws IOException {
325bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        if (ioVec.init() == 0) {
326adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            return 0;
327adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
328bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        int bytesTransferred = 0;
329bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        boolean completed = false;
330bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        try {
331bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            begin();
332e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            bytesTransferred = ioVec.doTransfer(fd);
333bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            completed = true;
334bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        } finally {
335bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            end(completed);
336adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
337bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        ioVec.didTransfer(bytesTransferred);
338bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return bytesTransferred;
339bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
340adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
341bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    public long read(ByteBuffer[] buffers, int offset, int length) throws IOException {
342bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        Arrays.checkOffsetAndCount(buffers.length, offset, length);
343bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        checkOpen();
344bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        checkReadable();
345bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return transferIoVec(new IoVec(buffers, offset, length, IoVec.Direction.READV));
346adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
347adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
348adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public long size() throws IOException {
3496ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkOpen();
35047cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes        try {
35147cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes            return Libcore.os.fstat(fd).st_size;
35247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes        } catch (ErrnoException errnoException) {
353f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes            throw errnoException.rethrowAsIOException();
35447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes        }
355adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
356adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
3576ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes    public long transferFrom(ReadableByteChannel src, long position, long count) throws IOException {
3586ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkOpen();
359adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (!src.isOpen()) {
360adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            throw new ClosedChannelException();
361adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
3626ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkWritable();
363eaa2ff09069424b0f7a95c7cd831cef1b744fe67Jesse Wilson        if (position < 0 || count < 0 || count > Integer.MAX_VALUE) {
3646ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes            throw new IllegalArgumentException("position=" + position + " count=" + count);
365adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
366eaa2ff09069424b0f7a95c7cd831cef1b744fe67Jesse Wilson        if (position > size()) {
367adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            return 0;
368adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
369eaa2ff09069424b0f7a95c7cd831cef1b744fe67Jesse Wilson
370e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes        // Although sendfile(2) originally supported writing to a regular file.
371e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes        // In Linux 2.6 and later, it only supports writing to sockets.
372e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes
373e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes        // If our source is a regular file, mmap(2) rather than reading.
374e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes        // Callers should only be using transferFrom for large transfers,
375e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes        // so the mmap(2) overhead isn't a concern.
376e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes        if (src instanceof FileChannel) {
377e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes            FileChannel fileSrc = (FileChannel) src;
378e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes            long size = fileSrc.size();
379e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes            long filePosition = fileSrc.position();
380e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes            count = Math.min(count, size - filePosition);
381e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes            ByteBuffer buffer = fileSrc.map(MapMode.READ_ONLY, filePosition, count);
382e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes            try {
383adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                fileSrc.position(filePosition + count);
384e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes                return write(buffer, position);
385e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes            } finally {
386e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes                NioUtils.freeDirectBuffer(buffer);
387adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            }
388adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
389e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes
390e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes        // For non-file channels, all we can do is read and write via userspace.
391e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes        ByteBuffer buffer = ByteBuffer.allocate((int) count);
392e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes        src.read(buffer);
393e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes        buffer.flip();
394e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes        return write(buffer, position);
395adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
396adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
3972cff86c0c10588a35036fe5bbca83b07f53e1b1dElliott Hughes    public long transferTo(long position, long count, WritableByteChannel target) throws IOException {
3986ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkOpen();
399adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (!target.isOpen()) {
400adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            throw new ClosedChannelException();
401adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
4022cff86c0c10588a35036fe5bbca83b07f53e1b1dElliott Hughes        checkReadable();
4036ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        if (target instanceof FileChannelImpl) {
4046ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes            ((FileChannelImpl) target).checkWritable();
405adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
406eaa2ff09069424b0f7a95c7cd831cef1b744fe67Jesse Wilson        if (position < 0 || count < 0) {
4076ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes            throw new IllegalArgumentException("position=" + position + " count=" + count);
408adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
409eaa2ff09069424b0f7a95c7cd831cef1b744fe67Jesse Wilson
410adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (count == 0 || position >= size()) {
411adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            return 0;
412adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
413adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        count = Math.min(count, size() - position);
4148b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes
4158b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        // Try sendfile(2) first...
4168b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        boolean completed = false;
417adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (target instanceof SocketChannelImpl) {
4188b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes            FileDescriptor outFd = ((SocketChannelImpl) target).getFD();
4198b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes            try {
4208b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                begin();
4218b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                try {
4228b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                    MutableLong offset = new MutableLong(position);
4238b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                    long rc = Libcore.os.sendfile(outFd, fd, offset, count);
4248b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                    completed = true;
4258b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                    return rc;
4268b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                } catch (ErrnoException errnoException) {
4278b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                    // If the OS doesn't support what we asked for, we want to fall through and
4288b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                    // try a different approach. If it does support it, but it failed, we're done.
4298b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                    if (errnoException.errno != ENOSYS && errnoException.errno != EINVAL) {
4308b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                        throw errnoException.rethrowAsIOException();
4318b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                    }
4328b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                }
4338b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes            } finally {
4348b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                end(completed);
4358b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes            }
436adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
4378b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        // ...fall back to write(2).
4388b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        ByteBuffer buffer = null;
439adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        try {
440adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            buffer = map(MapMode.READ_ONLY, position, count);
441adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            return target.write(buffer);
442adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        } finally {
443c73a5be50cdd804ff3c12e7b43da08c33cdd6f21Elliott Hughes            NioUtils.freeDirectBuffer(buffer);
444adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
445adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
446adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
447adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public FileChannel truncate(long size) throws IOException {
4486ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkOpen();
449adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (size < 0) {
450126ab1b546c71137a97cef68cc89267e7f7be634Elliott Hughes            throw new IllegalArgumentException("size < 0: " + size);
451adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
4526ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkWritable();
453adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (size < size()) {
454f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes            try {
455f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes                Libcore.os.ftruncate(fd, size);
456f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes            } catch (ErrnoException errnoException) {
457f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes                throw errnoException.rethrowAsIOException();
458adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            }
459adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
460adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return this;
461adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
462adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
463adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public int write(ByteBuffer buffer, long position) throws IOException {
464eaa2ff09069424b0f7a95c7cd831cef1b744fe67Jesse Wilson        if (position < 0) {
465126ab1b546c71137a97cef68cc89267e7f7be634Elliott Hughes            throw new IllegalArgumentException("position < 0: " + position);
466adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
467e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        return writeImpl(buffer, position);
468e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes    }
469e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes
470e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes    public int write(ByteBuffer buffer) throws IOException {
471e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        return writeImpl(buffer, -1);
472e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes    }
473e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes
474e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes    private int writeImpl(ByteBuffer buffer, long position) throws IOException {
4756ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkOpen();
4766ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkWritable();
477e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        if (buffer == null) {
478e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            throw new NullPointerException("buffer == null");
479e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        }
480eaa2ff09069424b0f7a95c7cd831cef1b744fe67Jesse Wilson        if (!buffer.hasRemaining()) {
481adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            return 0;
482adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
483adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        int bytesWritten = 0;
484e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        boolean completed = false;
485e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        try {
486e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            begin();
48778c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes            try {
488e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                if (position == -1) {
48978c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes                    bytesWritten = Libcore.os.write(fd, buffer);
490e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                } else {
491e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                    bytesWritten = Libcore.os.pwrite(fd, buffer, position);
492adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                }
493e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            } catch (ErrnoException errnoException) {
494e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                throw errnoException.rethrowAsIOException();
495adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            }
496e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            completed = true;
497e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        } finally {
498e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            end(completed);
499adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
500e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        if (bytesWritten > 0) {
501e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            buffer.position(buffer.position() + bytesWritten);
502e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        }
503e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        return bytesWritten;
504adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
505adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
5062620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes    public long write(ByteBuffer[] buffers, int offset, int length) throws IOException {
507a1603838fe9e865575c87982e32c6343740e464cElliott Hughes        Arrays.checkOffsetAndCount(buffers.length, offset, length);
5086ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkOpen();
5096ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkWritable();
510bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return transferIoVec(new IoVec(buffers, offset, length, IoVec.Direction.WRITEV));
511adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
512eaa2ff09069424b0f7a95c7cd831cef1b744fe67Jesse Wilson
5132620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes    /**
5142620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes     * @param copyingIn true if we're copying data into the buffers (typically
5152620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes     * because the caller is a file/network read operation), false if we're
5162620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes     * copying data out of the buffers (for a file/network write operation).
5172620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes     */
5182620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes    static int calculateTotalRemaining(ByteBuffer[] buffers, int offset, int length, boolean copyingIn) {
519d85168bd730e513cb0bd561030da3f7ea5044725Elliott Hughes        int count = 0;
520d85168bd730e513cb0bd561030da3f7ea5044725Elliott Hughes        for (int i = offset; i < offset + length; ++i) {
521d85168bd730e513cb0bd561030da3f7ea5044725Elliott Hughes            count += buffers[i].remaining();
5222620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes            if (copyingIn) {
523e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                buffers[i].checkWritable();
5242620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes            }
525d85168bd730e513cb0bd561030da3f7ea5044725Elliott Hughes        }
526d85168bd730e513cb0bd561030da3f7ea5044725Elliott Hughes        return count;
527d85168bd730e513cb0bd561030da3f7ea5044725Elliott Hughes    }
528d85168bd730e513cb0bd561030da3f7ea5044725Elliott Hughes
52952724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    public FileDescriptor getFD() {
5306ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        return fd;
531adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
532ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes
533ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes    /**
534ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes     * Add a new pending lock to the manager. Throws an exception if the lock
535ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes     * would overlap an existing lock. Once the lock is acquired it remains in
536ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes     * this set as an acquired lock.
537ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes     */
538ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes    private synchronized void addLock(FileLock lock) throws OverlappingFileLockException {
539ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes        long lockEnd = lock.position() + lock.size();
540ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes        for (FileLock existingLock : locks) {
541ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes            if (existingLock.position() > lockEnd) {
542ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes                // This, and all remaining locks, start beyond our end (so
543ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes                // cannot overlap).
544ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes                break;
545ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes            }
546ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes            if (existingLock.overlaps(lock.position(), lock.size())) {
547ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes                throw new OverlappingFileLockException();
548ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes            }
549ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes        }
550ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes        locks.add(lock);
551ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes    }
552ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes
553ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes    /**
554ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes     * Removes an acquired lock from the lock manager. If the lock did not exist
555ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes     * in the lock manager the operation is a no-op.
556ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes     */
557ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes    private synchronized void removeLock(FileLock lock) {
558ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes        locks.remove(lock);
559ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes    }
560adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project}
561