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;
}
}
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);
}
}
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;
}
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);
}
}
}
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;
}
}
Aggregations