Search in sources :

Example 26 with FileDescriptor

use of java.io.FileDescriptor in project Conversations by siacs.

the class FileBackend method weOwnFileLollipop.

@TargetApi(Build.VERSION_CODES.LOLLIPOP)
private static boolean weOwnFileLollipop(Uri uri) {
    try {
        File file = new File(uri.getPath());
        FileDescriptor fd = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY).getFileDescriptor();
        StructStat st = Os.fstat(fd);
        return st.st_uid == android.os.Process.myUid();
    } catch (FileNotFoundException e) {
        return false;
    } catch (Exception e) {
        return true;
    }
}
Also used : StructStat(android.system.StructStat) FileNotFoundException(java.io.FileNotFoundException) File(java.io.File) DownloadableFile(eu.siacs.conversations.entities.DownloadableFile) ParcelFileDescriptor(android.os.ParcelFileDescriptor) FileDescriptor(java.io.FileDescriptor) FileWriterException(eu.siacs.conversations.utils.FileWriterException) FileNotFoundException(java.io.FileNotFoundException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) IOException(java.io.IOException) TargetApi(android.annotation.TargetApi)

Example 27 with FileDescriptor

use of java.io.FileDescriptor in project platform_frameworks_base by android.

the class MediaPlayer method addTimedTextSource.

/* TODO: Limit the total number of external timed text source to a reasonable number.
     */
/**
     * Adds an external timed text source file.
     *
     * Currently supported format is SubRip with the file extension .srt, case insensitive.
     * Note that a single external timed text source may contain multiple tracks in it.
     * One can find the total number of available tracks using {@link #getTrackInfo()} to see what
     * additional tracks become available after this method call.
     *
     * @param path The file path of external timed text source file.
     * @param mimeType The mime type of the file. Must be one of the mime types listed above.
     * @throws IOException if the file cannot be accessed or is corrupted.
     * @throws IllegalArgumentException if the mimeType is not supported.
     * @throws IllegalStateException if called in an invalid state.
     */
public void addTimedTextSource(String path, String mimeType) throws IOException, IllegalArgumentException, IllegalStateException {
    if (!availableMimeTypeForExternalSource(mimeType)) {
        final String msg = "Illegal mimeType for timed text source: " + mimeType;
        throw new IllegalArgumentException(msg);
    }
    File file = new File(path);
    if (file.exists()) {
        FileInputStream is = new FileInputStream(file);
        FileDescriptor fd = is.getFD();
        addTimedTextSource(fd, mimeType);
        is.close();
    } else {
        // We do not support the case where the path is not a file.
        throw new IOException(path);
    }
}
Also used : IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) AssetFileDescriptor(android.content.res.AssetFileDescriptor) FileDescriptor(java.io.FileDescriptor)

Example 28 with FileDescriptor

use of java.io.FileDescriptor in project platform_frameworks_base by android.

the class DrmManagerClient method getOriginalMimeType.

/**
     * Retrieves the MIME type embedded in the original content.
     *
     * @param path Path to the rights-protected content.
     *
     * @return The MIME type of the original content, such as <code>video/mpeg</code>.
     */
public String getOriginalMimeType(String path) {
    if (null == path || path.equals("")) {
        throw new IllegalArgumentException("Given path should be non null");
    }
    String mime = null;
    FileInputStream is = null;
    try {
        FileDescriptor fd = null;
        File file = new File(path);
        if (file.exists()) {
            is = new FileInputStream(file);
            fd = is.getFD();
        }
        mime = _getOriginalMimeType(mUniqueId, path, fd);
    } catch (IOException ioe) {
    } finally {
        if (is != null) {
            try {
                is.close();
            } catch (IOException e) {
            }
        }
    }
    return mime;
}
Also used : IOException(java.io.IOException) File(java.io.File) FileInputStream(java.io.FileInputStream) FileDescriptor(java.io.FileDescriptor)

Example 29 with FileDescriptor

use of java.io.FileDescriptor in project platform_frameworks_base by android.

the class NativeDaemonConnector method listenToSocket.

private void listenToSocket() throws IOException {
    LocalSocket socket = null;
    try {
        socket = new LocalSocket();
        LocalSocketAddress address = determineSocketAddress();
        socket.connect(address);
        InputStream inputStream = socket.getInputStream();
        synchronized (mDaemonLock) {
            mOutputStream = socket.getOutputStream();
        }
        mCallbacks.onDaemonConnected();
        FileDescriptor[] fdList = null;
        byte[] buffer = new byte[BUFFER_SIZE];
        int start = 0;
        while (true) {
            int count = inputStream.read(buffer, start, BUFFER_SIZE - start);
            if (count < 0) {
                loge("got " + count + " reading with start = " + start);
                break;
            }
            fdList = socket.getAncillaryFileDescriptors();
            // Add our starting point to the count and reset the start.
            count += start;
            start = 0;
            for (int i = 0; i < count; i++) {
                if (buffer[i] == 0) {
                    // Note - do not log this raw message since it may contain
                    // sensitive data
                    final String rawEvent = new String(buffer, start, i - start, StandardCharsets.UTF_8);
                    boolean releaseWl = false;
                    try {
                        final NativeDaemonEvent event = NativeDaemonEvent.parseRawEvent(rawEvent, fdList);
                        log("RCV <- {" + event + "}");
                        if (event.isClassUnsolicited()) {
                            // TODO: migrate to sending NativeDaemonEvent instances
                            if (mCallbacks.onCheckHoldWakeLock(event.getCode()) && mWakeLock != null) {
                                mWakeLock.acquire();
                                releaseWl = true;
                            }
                            Message msg = mCallbackHandler.obtainMessage(event.getCode(), uptimeMillisInt(), 0, event.getRawEvent());
                            if (mCallbackHandler.sendMessage(msg)) {
                                releaseWl = false;
                            }
                        } else {
                            mResponseQueue.add(event.getCmdNumber(), event);
                        }
                    } catch (IllegalArgumentException e) {
                        log("Problem parsing message " + e);
                    } finally {
                        if (releaseWl) {
                            mWakeLock.release();
                        }
                    }
                    start = i + 1;
                }
            }
            if (start == 0) {
                log("RCV incomplete");
            }
            // buffer and read again.
            if (start != count) {
                final int remaining = BUFFER_SIZE - start;
                System.arraycopy(buffer, start, buffer, 0, remaining);
                start = remaining;
            } else {
                start = 0;
            }
        }
    } catch (IOException ex) {
        loge("Communications error: " + ex);
        throw ex;
    } finally {
        synchronized (mDaemonLock) {
            if (mOutputStream != null) {
                try {
                    loge("closing stream for " + mSocket);
                    mOutputStream.close();
                } catch (IOException e) {
                    loge("Failed closing output stream: " + e);
                }
                mOutputStream = null;
            }
        }
        try {
            if (socket != null) {
                socket.close();
            }
        } catch (IOException ex) {
            loge("Failed closing socket: " + ex);
        }
    }
}
Also used : Message(android.os.Message) InputStream(java.io.InputStream) LocalSocketAddress(android.net.LocalSocketAddress) LocalSocket(android.net.LocalSocket) IOException(java.io.IOException) FileDescriptor(java.io.FileDescriptor)

Example 30 with FileDescriptor

use of java.io.FileDescriptor in project platform_frameworks_base by android.

the class PinnerService method pinFile.

/** mlock length bytes of fileToPin in memory, starting at offset
     *  length == 0 means pin from offset to end of file
     *  maxSize == 0 means infinite
     */
private static PinnedFile pinFile(String fileToPin, long offset, long length, long maxSize) {
    FileDescriptor fd = new FileDescriptor();
    try {
        fd = Os.open(fileToPin, OsConstants.O_RDONLY | OsConstants.O_CLOEXEC | OsConstants.O_NOFOLLOW, OsConstants.O_RDONLY);
        StructStat sb = Os.fstat(fd);
        if (offset + length > sb.st_size) {
            Os.close(fd);
            Slog.e(TAG, "Failed to pin file " + fileToPin + ", request extends beyond end of file.  offset + length =  " + (offset + length) + ", file length = " + sb.st_size);
            return null;
        }
        if (length == 0) {
            length = sb.st_size - offset;
        }
        if (maxSize > 0 && length > maxSize) {
            Slog.e(TAG, "Could not pin file " + fileToPin + ", size = " + length + ", maxSize = " + maxSize);
            Os.close(fd);
            return null;
        }
        long address = Os.mmap(0, length, OsConstants.PROT_READ, OsConstants.MAP_PRIVATE, fd, offset);
        Os.close(fd);
        Os.mlock(address, length);
        return new PinnedFile(address, length, fileToPin);
    } catch (ErrnoException e) {
        Slog.e(TAG, "Could not pin file " + fileToPin + " with error " + e.getMessage());
        if (fd.valid()) {
            try {
                Os.close(fd);
            } catch (ErrnoException eClose) {
                Slog.e(TAG, "Failed to close fd, error = " + eClose.getMessage());
            }
        }
        return null;
    }
}
Also used : StructStat(android.system.StructStat) ErrnoException(android.system.ErrnoException) FileDescriptor(java.io.FileDescriptor)

Aggregations

FileDescriptor (java.io.FileDescriptor)361 IOException (java.io.IOException)161 ParcelFileDescriptor (android.os.ParcelFileDescriptor)95 ErrnoException (android.system.ErrnoException)95 FileInputStream (java.io.FileInputStream)82 File (java.io.File)51 AssetFileDescriptor (android.content.res.AssetFileDescriptor)37 FileOutputStream (java.io.FileOutputStream)35 Bitmap (android.graphics.Bitmap)30 LocalSocket (android.net.LocalSocket)26 FileNotFoundException (java.io.FileNotFoundException)26 SmallTest (android.test.suitebuilder.annotation.SmallTest)17 InputStream (java.io.InputStream)15 Socket (java.net.Socket)14 ErrnoException (libcore.io.ErrnoException)14 Test (org.junit.Test)14 ServerSocket (java.net.ServerSocket)13 SSLHandshakeCallbacks (org.conscrypt.NativeCrypto.SSLHandshakeCallbacks)13 RemoteException (android.os.RemoteException)12 BitmapFactory (android.graphics.BitmapFactory)11