FileChannelImpl.java revision 5d930cadc8f62aee5f18e7921296fe66a54f18ab
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
205d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughesimport android.system.ErrnoException;
215d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughesimport android.system.StructFlock;
225d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughesimport android.util.MutableLong;
23adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.io.Closeable;
24adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.io.FileDescriptor;
25adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.io.IOException;
26adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.nio.channels.ClosedChannelException;
27adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.nio.channels.FileChannel;
28adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.nio.channels.FileLock;
296ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughesimport java.nio.channels.NonReadableChannelException;
30adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.nio.channels.NonWritableChannelException;
31ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughesimport java.nio.channels.OverlappingFileLockException;
32adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.nio.channels.ReadableByteChannel;
33adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Projectimport java.nio.channels.WritableByteChannel;
34a1603838fe9e865575c87982e32c6343740e464cElliott Hughesimport java.util.Arrays;
35ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughesimport java.util.Comparator;
36ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughesimport java.util.SortedSet;
37ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughesimport java.util.TreeSet;
3852724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughesimport libcore.io.Libcore;
395d930cadc8f62aee5f18e7921296fe66a54f18abElliott Hughesimport static android.system.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
53f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller    private final Closeable ioObject;
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     */
64f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller    public FileChannelImpl(Closeable ioObject, FileDescriptor fd, int mode) {
656ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        this.fd = fd;
66f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller        this.ioObject = ioObject;
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 {
89f0d40d662d9dfdb04215c718961765837d2cf00cNeil Fuller        ioObject.close();
90adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
91adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
926ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes    private FileLock basicLock(long position, long size, boolean shared, boolean wait) throws IOException {
936ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        int accessMode = (mode & O_ACCMODE);
946ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        if (accessMode == O_RDONLY) {
956ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes            if (!shared) {
966ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes                throw new NonWritableChannelException();
976ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes            }
986ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        } else if (accessMode == O_WRONLY) {
996ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes            if (shared) {
1006ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes                throw new NonReadableChannelException();
1016ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes            }
1026ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        }
1036ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes
10403c0a8e681c776fdba0389ab8593282139afc6d6Elliott Hughes        if (position < 0 || size < 0) {
1056ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes            throw new IllegalArgumentException("position=" + position + " size=" + size);
106adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
107fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
108adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        FileLock pendingLock = new FileLockImpl(this, position, size, shared);
109ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes        addLock(pendingLock);
110adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
111fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        StructFlock flock = new StructFlock();
112fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        flock.l_type = (short) (shared ? F_RDLCK : F_WRLCK);
113fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        flock.l_whence = (short) SEEK_SET;
114fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        flock.l_start = position;
115fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        flock.l_len = translateLockLength(size);
116adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
1179b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes        boolean success = false;
1189b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes        try {
1199b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes            success = (Libcore.os.fcntlFlock(fd, wait ? F_SETLKW64 : F_SETLK64, flock) != -1);
1209b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes        } catch (ErrnoException errnoException) {
1219b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes            throw errnoException.rethrowAsIOException();
1229b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes        } finally {
1239b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes            if (!success) {
1249b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes                removeLock(pendingLock);
1259b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes            }
1269b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes        }
1279b510df35b57946d843ffc34cf23fdcfc84c5220Elliott Hughes        return success ? pendingLock : null;
128fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    }
129fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
130fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes    private static long translateLockLength(long byteCount) {
131fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        // FileChannel uses Long.MAX_VALUE to mean "lock the whole file" where POSIX uses 0.
132fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        return (byteCount == Long.MAX_VALUE) ? 0 : byteCount;
133adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
134adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
135ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes    private static final class FileLockImpl extends FileLock {
136ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes        private boolean isReleased = false;
137ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes
138ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes        public FileLockImpl(FileChannel channel, long position, long size, boolean shared) {
139ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes            super(channel, position, size, shared);
140ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes        }
141ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes
1426ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        public boolean isValid() {
143ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes            return !isReleased && channel().isOpen();
144ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes        }
145ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes
1466ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        public void release() throws IOException {
147ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes            if (!channel().isOpen()) {
148ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes                throw new ClosedChannelException();
149ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes            }
150ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes            if (!isReleased) {
151ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes                ((FileChannelImpl) channel()).release(this);
152ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes                isReleased = true;
153ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes            }
154ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes        }
155ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes    }
156ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes
157ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes    public final FileLock lock(long position, long size, boolean shared) throws IOException {
1586ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkOpen();
159adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        FileLock resultLock = null;
160adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        {
161adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            boolean completed = false;
162adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            try {
163adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                begin();
164adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                resultLock = basicLock(position, size, shared, true);
165adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                completed = true;
166adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            } finally {
167adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                end(completed);
168adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            }
169adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
170adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return resultLock;
171adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
172adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
173ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes    public final FileLock tryLock(long position, long size, boolean shared) throws IOException {
1746ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkOpen();
175adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return basicLock(position, size, shared, false);
176adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
177adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
178ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes    /**
179adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * Non-API method to release a given lock on a file channel. Assumes that
180adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     * the lock will mark itself invalid after successful unlocking.
181adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project     */
1826ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes    public void release(FileLock lock) throws IOException {
1836ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkOpen();
184fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
185fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        StructFlock flock = new StructFlock();
186fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        flock.l_type = (short) F_UNLCK;
187fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        flock.l_whence = (short) SEEK_SET;
188fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        flock.l_start = lock.position();
189fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        flock.l_len = translateLockLength(lock.size());
190fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        try {
191fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes            Libcore.os.fcntlFlock(fd, F_SETLKW64, flock);
192fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        } catch (ErrnoException errnoException) {
193fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes            throw errnoException.rethrowAsIOException();
194fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes        }
195fc549a0b0388987b26dea524894d75a63d14783bElliott Hughes
196ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes        removeLock(lock);
197adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
198adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
1996ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes    public void force(boolean metadata) throws IOException {
2006ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkOpen();
2016ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        if ((mode & O_ACCMODE) != O_RDONLY) {
20252724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes            try {
20352724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes                if (metadata) {
20452724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes                    Libcore.os.fsync(fd);
20552724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes                } else {
20652724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes                    Libcore.os.fdatasync(fd);
20752724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes                }
20852724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes            } catch (ErrnoException errnoException) {
209f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes                throw errnoException.rethrowAsIOException();
21052724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes            }
2116ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        }
212adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
213adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
2146ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes    public final MappedByteBuffer map(MapMode mapMode, long position, long size) throws IOException {
2156ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkOpen();
2166ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        if (mapMode == null) {
2176ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes            throw new NullPointerException("mapMode == null");
2186ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        }
2196ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        if (position < 0 || size < 0 || size > Integer.MAX_VALUE) {
2206ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes            throw new IllegalArgumentException("position=" + position + " size=" + size);
2216ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        }
2226ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        int accessMode = (mode & O_ACCMODE);
2236ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        if (accessMode == O_RDONLY) {
2246ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes            if (mapMode != MapMode.READ_ONLY) {
2256ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes                throw new NonWritableChannelException();
2266ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes            }
2276ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        } else if (accessMode == O_WRONLY) {
2286ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes            throw new NonReadableChannelException();
2296ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        }
230adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (position + size > size()) {
231c03e4ba8cd93513aabda061b00d516b54717c5fbElliott Hughes            // We can't defer to FileChannel.truncate because that will only make a file shorter,
232c03e4ba8cd93513aabda061b00d516b54717c5fbElliott Hughes            // and we only care about making our backing file longer here.
233c03e4ba8cd93513aabda061b00d516b54717c5fbElliott Hughes            try {
234c03e4ba8cd93513aabda061b00d516b54717c5fbElliott Hughes                Libcore.os.ftruncate(fd, position + size);
235105a9405b2e059352185f9ca1138cc8480ccb9bcElliott Hughes            } catch (ErrnoException ftruncateException) {
236385c6f4303341beb9b091b6d252811b7ca3b9f42Nick Kralevich                // EINVAL can be thrown if we're dealing with non-regular
237385c6f4303341beb9b091b6d252811b7ca3b9f42Nick Kralevich                // files, for example, character devices such as /dev/zero.
238385c6f4303341beb9b091b6d252811b7ca3b9f42Nick Kralevich                // In those cases, we ignore the failed truncation and
239385c6f4303341beb9b091b6d252811b7ca3b9f42Nick Kralevich                // continue on.
240105a9405b2e059352185f9ca1138cc8480ccb9bcElliott Hughes                try {
241105a9405b2e059352185f9ca1138cc8480ccb9bcElliott Hughes                    if (S_ISREG(Libcore.os.fstat(fd).st_mode) || ftruncateException.errno != EINVAL) {
242105a9405b2e059352185f9ca1138cc8480ccb9bcElliott Hughes                        throw ftruncateException.rethrowAsIOException();
243105a9405b2e059352185f9ca1138cc8480ccb9bcElliott Hughes                    }
244105a9405b2e059352185f9ca1138cc8480ccb9bcElliott Hughes                } catch (ErrnoException fstatException) {
245105a9405b2e059352185f9ca1138cc8480ccb9bcElliott Hughes                    throw fstatException.rethrowAsIOException();
246385c6f4303341beb9b091b6d252811b7ca3b9f42Nick Kralevich                }
247c03e4ba8cd93513aabda061b00d516b54717c5fbElliott Hughes            }
248adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
2496fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes        long alignment = position - position % Libcore.os.sysconf(_SC_PAGE_SIZE);
250adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        int offset = (int) (position - alignment);
2517e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        MemoryBlock block = MemoryBlock.mmap(fd, alignment, size + offset, mapMode);
252fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes        return new DirectByteBuffer(block, (int) size, offset, (mapMode == MapMode.READ_ONLY), mapMode);
253adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
254adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
255adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public long position() throws IOException {
2566ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkOpen();
257dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes        try {
258dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes            return Libcore.os.lseek(fd, 0L, SEEK_CUR);
259dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes        } catch (ErrnoException errnoException) {
260dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes            throw errnoException.rethrowAsIOException();
261dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes        }
262adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
263adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
264adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public FileChannel position(long newPosition) throws IOException {
2656ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkOpen();
266adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (newPosition < 0) {
2676ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes            throw new IllegalArgumentException("position: " + newPosition);
268eaa2ff09069424b0f7a95c7cd831cef1b744fe67Jesse Wilson        }
269e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        try {
270e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            Libcore.os.lseek(fd, newPosition, SEEK_SET);
271e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        } catch (ErrnoException errnoException) {
272e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            throw errnoException.rethrowAsIOException();
273adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
274adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return this;
275adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
276adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
277adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public int read(ByteBuffer buffer, long position) throws IOException {
278eaa2ff09069424b0f7a95c7cd831cef1b744fe67Jesse Wilson        if (position < 0) {
2796ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes            throw new IllegalArgumentException("position: " + position);
280adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
281e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        return readImpl(buffer, position);
282adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
283adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
284adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public int read(ByteBuffer buffer) throws IOException {
285e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        return readImpl(buffer, -1);
286e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes    }
287e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes
288e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes    private int readImpl(ByteBuffer buffer, long position) throws IOException {
289e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        buffer.checkWritable();
2906ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkOpen();
2916ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkReadable();
292eaa2ff09069424b0f7a95c7cd831cef1b744fe67Jesse Wilson        if (!buffer.hasRemaining()) {
293adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            return 0;
294adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
295e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        int bytesRead = 0;
296e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        boolean completed = false;
297e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        try {
298e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            begin();
29926c7025a7a919044771fb89031161bd26fe03032Elliott Hughes            try {
300e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                if (position == -1) {
30126c7025a7a919044771fb89031161bd26fe03032Elliott Hughes                    bytesRead = Libcore.os.read(fd, buffer);
302e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                } else {
303e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                    bytesRead = Libcore.os.pread(fd, buffer, position);
304e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                }
305e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                if (bytesRead == 0) {
306e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                    bytesRead = -1;
307e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                }
308e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            } catch (ErrnoException errnoException) {
309e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                if (errnoException.errno == EAGAIN) {
310e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                    // We don't throw if we try to read from an empty non-blocking pipe.
311e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                    bytesRead = 0;
312e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                } else {
313e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                    throw errnoException.rethrowAsIOException();
314adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                }
315adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            }
316e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            completed = true;
317e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        } finally {
318e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            end(completed && bytesRead >= 0);
319e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        }
320e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        if (bytesRead > 0) {
321e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            buffer.position(buffer.position() + bytesRead);
322adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
323e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        return bytesRead;
324adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
325adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
326bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    private int transferIoVec(IoVec ioVec) throws IOException {
327bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        if (ioVec.init() == 0) {
328adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            return 0;
329adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
330bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        int bytesTransferred = 0;
331bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        boolean completed = false;
332bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        try {
333bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            begin();
334e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            bytesTransferred = ioVec.doTransfer(fd);
335bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            completed = true;
336bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        } finally {
337bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            end(completed);
338adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
339bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        ioVec.didTransfer(bytesTransferred);
340bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return bytesTransferred;
341bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
342adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
343bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    public long read(ByteBuffer[] buffers, int offset, int length) throws IOException {
344bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        Arrays.checkOffsetAndCount(buffers.length, offset, length);
345bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        checkOpen();
346bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        checkReadable();
347bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return transferIoVec(new IoVec(buffers, offset, length, IoVec.Direction.READV));
348adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
349adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
350adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public long size() throws IOException {
3516ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkOpen();
35247cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes        try {
35347cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes            return Libcore.os.fstat(fd).st_size;
35447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes        } catch (ErrnoException errnoException) {
355f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes            throw errnoException.rethrowAsIOException();
35647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes        }
357adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
358adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
3596ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes    public long transferFrom(ReadableByteChannel src, long position, long count) throws IOException {
3606ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkOpen();
361adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (!src.isOpen()) {
362adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            throw new ClosedChannelException();
363adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
3646ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkWritable();
365eaa2ff09069424b0f7a95c7cd831cef1b744fe67Jesse Wilson        if (position < 0 || count < 0 || count > Integer.MAX_VALUE) {
3666ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes            throw new IllegalArgumentException("position=" + position + " count=" + count);
367adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
368eaa2ff09069424b0f7a95c7cd831cef1b744fe67Jesse Wilson        if (position > size()) {
369adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            return 0;
370adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
371eaa2ff09069424b0f7a95c7cd831cef1b744fe67Jesse Wilson
372e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes        // Although sendfile(2) originally supported writing to a regular file.
373e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes        // In Linux 2.6 and later, it only supports writing to sockets.
374e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes
375e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes        // If our source is a regular file, mmap(2) rather than reading.
376e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes        // Callers should only be using transferFrom for large transfers,
377e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes        // so the mmap(2) overhead isn't a concern.
378e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes        if (src instanceof FileChannel) {
379e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes            FileChannel fileSrc = (FileChannel) src;
380e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes            long size = fileSrc.size();
381e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes            long filePosition = fileSrc.position();
382e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes            count = Math.min(count, size - filePosition);
383e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes            ByteBuffer buffer = fileSrc.map(MapMode.READ_ONLY, filePosition, count);
384e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes            try {
385adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                fileSrc.position(filePosition + count);
386e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes                return write(buffer, position);
387e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes            } finally {
388e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes                NioUtils.freeDirectBuffer(buffer);
389adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            }
390adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
391e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes
392e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes        // For non-file channels, all we can do is read and write via userspace.
393e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes        ByteBuffer buffer = ByteBuffer.allocate((int) count);
394e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes        src.read(buffer);
395e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes        buffer.flip();
396e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes        return write(buffer, position);
397adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
398adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
3992cff86c0c10588a35036fe5bbca83b07f53e1b1dElliott Hughes    public long transferTo(long position, long count, WritableByteChannel target) throws IOException {
4006ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkOpen();
401adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (!target.isOpen()) {
402adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            throw new ClosedChannelException();
403adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
4042cff86c0c10588a35036fe5bbca83b07f53e1b1dElliott Hughes        checkReadable();
4056ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        if (target instanceof FileChannelImpl) {
4066ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes            ((FileChannelImpl) target).checkWritable();
407adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
408eaa2ff09069424b0f7a95c7cd831cef1b744fe67Jesse Wilson        if (position < 0 || count < 0) {
4096ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes            throw new IllegalArgumentException("position=" + position + " count=" + count);
410adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
411eaa2ff09069424b0f7a95c7cd831cef1b744fe67Jesse Wilson
412adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (count == 0 || position >= size()) {
413adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            return 0;
414adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
415adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        count = Math.min(count, size() - position);
4168b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes
4178b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        // Try sendfile(2) first...
4188b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        boolean completed = false;
419adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (target instanceof SocketChannelImpl) {
4208b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes            FileDescriptor outFd = ((SocketChannelImpl) target).getFD();
4218b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes            try {
4228b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                begin();
4238b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                try {
4248b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                    MutableLong offset = new MutableLong(position);
4258b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                    long rc = Libcore.os.sendfile(outFd, fd, offset, count);
4268b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                    completed = true;
4278b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                    return rc;
4288b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                } catch (ErrnoException errnoException) {
4298b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                    // If the OS doesn't support what we asked for, we want to fall through and
4308b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                    // try a different approach. If it does support it, but it failed, we're done.
4318b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                    if (errnoException.errno != ENOSYS && errnoException.errno != EINVAL) {
4328b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                        throw errnoException.rethrowAsIOException();
4338b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                    }
4348b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                }
4358b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes            } finally {
4368b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                end(completed);
4378b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes            }
438adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
4398b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        // ...fall back to write(2).
4408b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        ByteBuffer buffer = null;
441adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        try {
442adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            buffer = map(MapMode.READ_ONLY, position, count);
443adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            return target.write(buffer);
444adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        } finally {
445c73a5be50cdd804ff3c12e7b43da08c33cdd6f21Elliott Hughes            NioUtils.freeDirectBuffer(buffer);
446adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
447adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
448adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
449adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public FileChannel truncate(long size) throws IOException {
4506ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkOpen();
451adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (size < 0) {
452126ab1b546c71137a97cef68cc89267e7f7be634Elliott Hughes            throw new IllegalArgumentException("size < 0: " + size);
453adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
4546ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkWritable();
455adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (size < size()) {
456f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes            try {
457f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes                Libcore.os.ftruncate(fd, size);
458f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes            } catch (ErrnoException errnoException) {
459f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes                throw errnoException.rethrowAsIOException();
460adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            }
461adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
462a990ad5c834bd3292f8f4c5e45c7ff117ffae5ecNeil Fuller        if (position() > size) {
463a990ad5c834bd3292f8f4c5e45c7ff117ffae5ecNeil Fuller            position(size);
464a990ad5c834bd3292f8f4c5e45c7ff117ffae5ecNeil Fuller        }
465adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return this;
466adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
467adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
468adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public int write(ByteBuffer buffer, long position) throws IOException {
469eaa2ff09069424b0f7a95c7cd831cef1b744fe67Jesse Wilson        if (position < 0) {
470126ab1b546c71137a97cef68cc89267e7f7be634Elliott Hughes            throw new IllegalArgumentException("position < 0: " + position);
471adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
472e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        return writeImpl(buffer, position);
473e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes    }
474e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes
475e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes    public int write(ByteBuffer buffer) throws IOException {
476e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        return writeImpl(buffer, -1);
477e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes    }
478e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes
479e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes    private int writeImpl(ByteBuffer buffer, long position) throws IOException {
4806ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkOpen();
4816ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkWritable();
482e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        if (buffer == null) {
483e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            throw new NullPointerException("buffer == null");
484e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        }
485eaa2ff09069424b0f7a95c7cd831cef1b744fe67Jesse Wilson        if (!buffer.hasRemaining()) {
486adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            return 0;
487adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
488adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        int bytesWritten = 0;
489e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        boolean completed = false;
490e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        try {
491e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            begin();
49278c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes            try {
493e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                if (position == -1) {
49478c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes                    bytesWritten = Libcore.os.write(fd, buffer);
495e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                } else {
496e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                    bytesWritten = Libcore.os.pwrite(fd, buffer, position);
497adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                }
498e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            } catch (ErrnoException errnoException) {
499e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                throw errnoException.rethrowAsIOException();
500adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            }
501e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            completed = true;
502e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        } finally {
503e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            end(completed);
504adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
505e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        if (bytesWritten > 0) {
506e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            buffer.position(buffer.position() + bytesWritten);
507e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        }
508e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        return bytesWritten;
509adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
510adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
5112620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes    public long write(ByteBuffer[] buffers, int offset, int length) throws IOException {
512a1603838fe9e865575c87982e32c6343740e464cElliott Hughes        Arrays.checkOffsetAndCount(buffers.length, offset, length);
5136ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkOpen();
5146ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkWritable();
515bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return transferIoVec(new IoVec(buffers, offset, length, IoVec.Direction.WRITEV));
516adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
517eaa2ff09069424b0f7a95c7cd831cef1b744fe67Jesse Wilson
5182620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes    /**
5192620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes     * @param copyingIn true if we're copying data into the buffers (typically
5202620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes     * because the caller is a file/network read operation), false if we're
5212620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes     * copying data out of the buffers (for a file/network write operation).
5222620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes     */
5232620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes    static int calculateTotalRemaining(ByteBuffer[] buffers, int offset, int length, boolean copyingIn) {
524d85168bd730e513cb0bd561030da3f7ea5044725Elliott Hughes        int count = 0;
525d85168bd730e513cb0bd561030da3f7ea5044725Elliott Hughes        for (int i = offset; i < offset + length; ++i) {
526d85168bd730e513cb0bd561030da3f7ea5044725Elliott Hughes            count += buffers[i].remaining();
5272620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes            if (copyingIn) {
528e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                buffers[i].checkWritable();
5292620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes            }
530d85168bd730e513cb0bd561030da3f7ea5044725Elliott Hughes        }
531d85168bd730e513cb0bd561030da3f7ea5044725Elliott Hughes        return count;
532d85168bd730e513cb0bd561030da3f7ea5044725Elliott Hughes    }
533d85168bd730e513cb0bd561030da3f7ea5044725Elliott Hughes
53452724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    public FileDescriptor getFD() {
5356ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        return fd;
536adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
537ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes
538ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes    /**
539ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes     * Add a new pending lock to the manager. Throws an exception if the lock
540ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes     * would overlap an existing lock. Once the lock is acquired it remains in
541ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes     * this set as an acquired lock.
542ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes     */
543ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes    private synchronized void addLock(FileLock lock) throws OverlappingFileLockException {
544ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes        long lockEnd = lock.position() + lock.size();
545ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes        for (FileLock existingLock : locks) {
546ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes            if (existingLock.position() > lockEnd) {
547ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes                // This, and all remaining locks, start beyond our end (so
548ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes                // cannot overlap).
549ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes                break;
550ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes            }
551ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes            if (existingLock.overlaps(lock.position(), lock.size())) {
552ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes                throw new OverlappingFileLockException();
553ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes            }
554ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes        }
555ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes        locks.add(lock);
556ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes    }
557ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes
558ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes    /**
559ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes     * Removes an acquired lock from the lock manager. If the lock did not exist
560ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes     * in the lock manager the operation is a no-op.
561ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes     */
562ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes    private synchronized void removeLock(FileLock lock) {
563ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes        locks.remove(lock);
564ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes    }
565adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project}
566