FileChannelImpl.java revision 105a9405b2e059352185f9ca1138cc8480ccb9bc
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);
237105a9405b2e059352185f9ca1138cc8480ccb9bcElliott Hughes            } catch (ErrnoException ftruncateException) {
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.
242105a9405b2e059352185f9ca1138cc8480ccb9bcElliott Hughes                try {
243105a9405b2e059352185f9ca1138cc8480ccb9bcElliott Hughes                    if (S_ISREG(Libcore.os.fstat(fd).st_mode) || ftruncateException.errno != EINVAL) {
244105a9405b2e059352185f9ca1138cc8480ccb9bcElliott Hughes                        throw ftruncateException.rethrowAsIOException();
245105a9405b2e059352185f9ca1138cc8480ccb9bcElliott Hughes                    }
246105a9405b2e059352185f9ca1138cc8480ccb9bcElliott Hughes                } catch (ErrnoException fstatException) {
247105a9405b2e059352185f9ca1138cc8480ccb9bcElliott Hughes                    throw fstatException.rethrowAsIOException();
248385c6f4303341beb9b091b6d252811b7ca3b9f42Nick Kralevich                }
249c03e4ba8cd93513aabda061b00d516b54717c5fbElliott Hughes            }
250adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
2516fc1a0e1e68dc2e0d12341548e58fa7f1c5dafc4Elliott Hughes        long alignment = position - position % Libcore.os.sysconf(_SC_PAGE_SIZE);
252adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        int offset = (int) (position - alignment);
2537e25eff38a191d9c19e45093f4fde5102fb09d78Elliott Hughes        MemoryBlock block = MemoryBlock.mmap(fd, alignment, size + offset, mapMode);
254fe5da19e0e366286cd4d95f7628fe9442b9062c8Elliott Hughes        return new DirectByteBuffer(block, (int) size, offset, (mapMode == MapMode.READ_ONLY), mapMode);
255adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
256adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
257adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public long position() throws IOException {
2586ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkOpen();
259dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes        try {
260dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes            return Libcore.os.lseek(fd, 0L, SEEK_CUR);
261dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes        } catch (ErrnoException errnoException) {
262dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes            throw errnoException.rethrowAsIOException();
263dedaccdfa07c370a58cba08b096133ad9eec0ec3Elliott Hughes        }
264adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
265adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
266adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public FileChannel position(long newPosition) throws IOException {
2676ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkOpen();
268adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (newPosition < 0) {
2696ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes            throw new IllegalArgumentException("position: " + newPosition);
270eaa2ff09069424b0f7a95c7cd831cef1b744fe67Jesse Wilson        }
271e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        try {
272e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            Libcore.os.lseek(fd, newPosition, SEEK_SET);
273e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        } catch (ErrnoException errnoException) {
274e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            throw errnoException.rethrowAsIOException();
275adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
276adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return this;
277adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
278adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
279adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public int read(ByteBuffer buffer, long position) throws IOException {
280eaa2ff09069424b0f7a95c7cd831cef1b744fe67Jesse Wilson        if (position < 0) {
2816ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes            throw new IllegalArgumentException("position: " + position);
282adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
283e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        return readImpl(buffer, position);
284adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
285adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
286adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public int read(ByteBuffer buffer) throws IOException {
287e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        return readImpl(buffer, -1);
288e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes    }
289e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes
290e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes    private int readImpl(ByteBuffer buffer, long position) throws IOException {
291e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        buffer.checkWritable();
2926ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkOpen();
2936ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkReadable();
294eaa2ff09069424b0f7a95c7cd831cef1b744fe67Jesse Wilson        if (!buffer.hasRemaining()) {
295adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            return 0;
296adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
297e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        int bytesRead = 0;
298e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        boolean completed = false;
299e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        try {
300e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            begin();
30126c7025a7a919044771fb89031161bd26fe03032Elliott Hughes            try {
302e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                if (position == -1) {
30326c7025a7a919044771fb89031161bd26fe03032Elliott Hughes                    bytesRead = Libcore.os.read(fd, buffer);
304e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                } else {
305e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                    bytesRead = Libcore.os.pread(fd, buffer, position);
306e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                }
307e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                if (bytesRead == 0) {
308e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                    bytesRead = -1;
309e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                }
310e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            } catch (ErrnoException errnoException) {
311e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                if (errnoException.errno == EAGAIN) {
312e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                    // We don't throw if we try to read from an empty non-blocking pipe.
313e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                    bytesRead = 0;
314e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                } else {
315e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                    throw errnoException.rethrowAsIOException();
316adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                }
317adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            }
318e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            completed = true;
319e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        } finally {
320e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            end(completed && bytesRead >= 0);
321e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        }
322e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        if (bytesRead > 0) {
323e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            buffer.position(buffer.position() + bytesRead);
324adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
325e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        return bytesRead;
326adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
327adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
328bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    private int transferIoVec(IoVec ioVec) throws IOException {
329bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        if (ioVec.init() == 0) {
330adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            return 0;
331adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
332bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        int bytesTransferred = 0;
333bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        boolean completed = false;
334bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        try {
335bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            begin();
336e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            bytesTransferred = ioVec.doTransfer(fd);
337bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            completed = true;
338bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        } finally {
339bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes            end(completed);
340adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
341bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        ioVec.didTransfer(bytesTransferred);
342bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return bytesTransferred;
343bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    }
344adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
345bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes    public long read(ByteBuffer[] buffers, int offset, int length) throws IOException {
346bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        Arrays.checkOffsetAndCount(buffers.length, offset, length);
347bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        checkOpen();
348bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        checkReadable();
349bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return transferIoVec(new IoVec(buffers, offset, length, IoVec.Direction.READV));
350adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
351adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
352adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public long size() throws IOException {
3536ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkOpen();
35447cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes        try {
35547cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes            return Libcore.os.fstat(fd).st_size;
35647cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes        } catch (ErrnoException errnoException) {
357f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes            throw errnoException.rethrowAsIOException();
35847cb338d43f75dd998b29caaaa9446c5705217d1Elliott Hughes        }
359adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
360adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
3616ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes    public long transferFrom(ReadableByteChannel src, long position, long count) throws IOException {
3626ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkOpen();
363adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (!src.isOpen()) {
364adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            throw new ClosedChannelException();
365adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
3666ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkWritable();
367eaa2ff09069424b0f7a95c7cd831cef1b744fe67Jesse Wilson        if (position < 0 || count < 0 || count > Integer.MAX_VALUE) {
3686ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes            throw new IllegalArgumentException("position=" + position + " count=" + count);
369adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
370eaa2ff09069424b0f7a95c7cd831cef1b744fe67Jesse Wilson        if (position > size()) {
371adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            return 0;
372adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
373eaa2ff09069424b0f7a95c7cd831cef1b744fe67Jesse Wilson
374e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes        // Although sendfile(2) originally supported writing to a regular file.
375e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes        // In Linux 2.6 and later, it only supports writing to sockets.
376e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes
377e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes        // If our source is a regular file, mmap(2) rather than reading.
378e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes        // Callers should only be using transferFrom for large transfers,
379e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes        // so the mmap(2) overhead isn't a concern.
380e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes        if (src instanceof FileChannel) {
381e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes            FileChannel fileSrc = (FileChannel) src;
382e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes            long size = fileSrc.size();
383e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes            long filePosition = fileSrc.position();
384e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes            count = Math.min(count, size - filePosition);
385e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes            ByteBuffer buffer = fileSrc.map(MapMode.READ_ONLY, filePosition, count);
386e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes            try {
387adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                fileSrc.position(filePosition + count);
388e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes                return write(buffer, position);
389e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes            } finally {
390e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes                NioUtils.freeDirectBuffer(buffer);
391adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            }
392adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
393e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes
394e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes        // For non-file channels, all we can do is read and write via userspace.
395e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes        ByteBuffer buffer = ByteBuffer.allocate((int) count);
396e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes        src.read(buffer);
397e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes        buffer.flip();
398e3d7539b3cb1e763f1ed5832120684468e635d94Elliott Hughes        return write(buffer, position);
399adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
400adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
4012cff86c0c10588a35036fe5bbca83b07f53e1b1dElliott Hughes    public long transferTo(long position, long count, WritableByteChannel target) throws IOException {
4026ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkOpen();
403adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (!target.isOpen()) {
404adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            throw new ClosedChannelException();
405adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
4062cff86c0c10588a35036fe5bbca83b07f53e1b1dElliott Hughes        checkReadable();
4076ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        if (target instanceof FileChannelImpl) {
4086ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes            ((FileChannelImpl) target).checkWritable();
409adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
410eaa2ff09069424b0f7a95c7cd831cef1b744fe67Jesse Wilson        if (position < 0 || count < 0) {
4116ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes            throw new IllegalArgumentException("position=" + position + " count=" + count);
412adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
413eaa2ff09069424b0f7a95c7cd831cef1b744fe67Jesse Wilson
414adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (count == 0 || position >= size()) {
415adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            return 0;
416adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
417adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        count = Math.min(count, size() - position);
4188b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes
4198b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        // Try sendfile(2) first...
4208b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        boolean completed = false;
421adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (target instanceof SocketChannelImpl) {
4228b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes            FileDescriptor outFd = ((SocketChannelImpl) target).getFD();
4238b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes            try {
4248b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                begin();
4258b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                try {
4268b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                    MutableLong offset = new MutableLong(position);
4278b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                    long rc = Libcore.os.sendfile(outFd, fd, offset, count);
4288b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                    completed = true;
4298b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                    return rc;
4308b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                } catch (ErrnoException errnoException) {
4318b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                    // If the OS doesn't support what we asked for, we want to fall through and
4328b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                    // try a different approach. If it does support it, but it failed, we're done.
4338b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                    if (errnoException.errno != ENOSYS && errnoException.errno != EINVAL) {
4348b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                        throw errnoException.rethrowAsIOException();
4358b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                    }
4368b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                }
4378b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes            } finally {
4388b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes                end(completed);
4398b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes            }
440adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
4418b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        // ...fall back to write(2).
4428b15dcc5890963edad4dfcf558cc16027c7985e5Elliott Hughes        ByteBuffer buffer = null;
443adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        try {
444adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            buffer = map(MapMode.READ_ONLY, position, count);
445adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            return target.write(buffer);
446adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        } finally {
447c73a5be50cdd804ff3c12e7b43da08c33cdd6f21Elliott Hughes            NioUtils.freeDirectBuffer(buffer);
448adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
449adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
450adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
451adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public FileChannel truncate(long size) throws IOException {
4526ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkOpen();
453adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (size < 0) {
454126ab1b546c71137a97cef68cc89267e7f7be634Elliott Hughes            throw new IllegalArgumentException("size < 0: " + size);
455adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
4566ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkWritable();
457adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        if (size < size()) {
458f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes            try {
459f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes                Libcore.os.ftruncate(fd, size);
460f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes            } catch (ErrnoException errnoException) {
461f5333fd2094bdac4d6506177b1964b79afa64d77Elliott Hughes                throw errnoException.rethrowAsIOException();
462adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            }
463adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
464adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        return this;
465adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
466adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
467adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    public int write(ByteBuffer buffer, long position) throws IOException {
468eaa2ff09069424b0f7a95c7cd831cef1b744fe67Jesse Wilson        if (position < 0) {
469126ab1b546c71137a97cef68cc89267e7f7be634Elliott Hughes            throw new IllegalArgumentException("position < 0: " + position);
470adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
471e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        return writeImpl(buffer, position);
472e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes    }
473e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes
474e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes    public int write(ByteBuffer buffer) throws IOException {
475e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        return writeImpl(buffer, -1);
476e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes    }
477e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes
478e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes    private int writeImpl(ByteBuffer buffer, long position) throws IOException {
4796ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkOpen();
4806ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkWritable();
481e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        if (buffer == null) {
482e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            throw new NullPointerException("buffer == null");
483e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        }
484eaa2ff09069424b0f7a95c7cd831cef1b744fe67Jesse Wilson        if (!buffer.hasRemaining()) {
485adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            return 0;
486adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
487adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        int bytesWritten = 0;
488e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        boolean completed = false;
489e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        try {
490e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            begin();
49178c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes            try {
492e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                if (position == -1) {
49378c7cc547101002b9f9043cf3845970719d1bda8Elliott Hughes                    bytesWritten = Libcore.os.write(fd, buffer);
494e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                } else {
495e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                    bytesWritten = Libcore.os.pwrite(fd, buffer, position);
496adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project                }
497e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            } catch (ErrnoException errnoException) {
498e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                throw errnoException.rethrowAsIOException();
499adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project            }
500e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            completed = true;
501e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        } finally {
502e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            end(completed);
503adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project        }
504e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        if (bytesWritten > 0) {
505e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes            buffer.position(buffer.position() + bytesWritten);
506e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        }
507e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes        return bytesWritten;
508adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
509adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project
5102620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes    public long write(ByteBuffer[] buffers, int offset, int length) throws IOException {
511a1603838fe9e865575c87982e32c6343740e464cElliott Hughes        Arrays.checkOffsetAndCount(buffers.length, offset, length);
5126ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkOpen();
5136ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        checkWritable();
514bbac92e691de7d570928ddfba639067978e55b06Elliott Hughes        return transferIoVec(new IoVec(buffers, offset, length, IoVec.Direction.WRITEV));
515adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
516eaa2ff09069424b0f7a95c7cd831cef1b744fe67Jesse Wilson
5172620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes    /**
5182620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes     * @param copyingIn true if we're copying data into the buffers (typically
5192620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes     * because the caller is a file/network read operation), false if we're
5202620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes     * copying data out of the buffers (for a file/network write operation).
5212620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes     */
5222620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes    static int calculateTotalRemaining(ByteBuffer[] buffers, int offset, int length, boolean copyingIn) {
523d85168bd730e513cb0bd561030da3f7ea5044725Elliott Hughes        int count = 0;
524d85168bd730e513cb0bd561030da3f7ea5044725Elliott Hughes        for (int i = offset; i < offset + length; ++i) {
525d85168bd730e513cb0bd561030da3f7ea5044725Elliott Hughes            count += buffers[i].remaining();
5262620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes            if (copyingIn) {
527e3b6fa2bf357f2712ab2ee9e8487f157595ea0c7Elliott Hughes                buffers[i].checkWritable();
5282620ff9a08ce7fc6d66b60784b1eecd78eb001baElliott Hughes            }
529d85168bd730e513cb0bd561030da3f7ea5044725Elliott Hughes        }
530d85168bd730e513cb0bd561030da3f7ea5044725Elliott Hughes        return count;
531d85168bd730e513cb0bd561030da3f7ea5044725Elliott Hughes    }
532d85168bd730e513cb0bd561030da3f7ea5044725Elliott Hughes
53352724d3ebd4ccaaa4b9f5576e329d4272cde8ea9Elliott Hughes    public FileDescriptor getFD() {
5346ab5999b58777725b4556e4d81bdec56b6d6c182Elliott Hughes        return fd;
535adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project    }
536ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes
537ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes    /**
538ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes     * Add a new pending lock to the manager. Throws an exception if the lock
539ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes     * would overlap an existing lock. Once the lock is acquired it remains in
540ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes     * this set as an acquired lock.
541ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes     */
542ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes    private synchronized void addLock(FileLock lock) throws OverlappingFileLockException {
543ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes        long lockEnd = lock.position() + lock.size();
544ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes        for (FileLock existingLock : locks) {
545ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes            if (existingLock.position() > lockEnd) {
546ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes                // This, and all remaining locks, start beyond our end (so
547ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes                // cannot overlap).
548ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes                break;
549ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes            }
550ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes            if (existingLock.overlaps(lock.position(), lock.size())) {
551ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes                throw new OverlappingFileLockException();
552ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes            }
553ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes        }
554ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes        locks.add(lock);
555ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes    }
556ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes
557ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes    /**
558ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes     * Removes an acquired lock from the lock manager. If the lock did not exist
559ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes     * in the lock manager the operation is a no-op.
560ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes     */
561ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes    private synchronized void removeLock(FileLock lock) {
562ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes        locks.remove(lock);
563ae704b984c10a63883cc366e823d53902d6ac7a9Elliott Hughes    }
564adc854b798c1cfe3bfd4c27d68d5cee38ca617daThe Android Open Source Project}
565