Java Code Examples for java.io.FileInputStream#getFD()

The following examples show how to use java.io.FileInputStream#getFD() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: CombineBitmap   File: BitmapLoader.java    License: Apache License 2.0 6 votes vote down vote up
private Bitmap loadBitmapFromDiskCache(String url, int reqWidth,
                                       int reqHeight) throws IOException {
    Bitmap bitmap = null;
    String key = Utils.hashKeyFormUrl(url);
    DiskLruCache.Snapshot snapShot = mDiskLruCache.get(key);
    if (snapShot != null) {
        FileInputStream fileInputStream = (FileInputStream) snapShot.getInputStream(0);
        FileDescriptor fileDescriptor = fileInputStream.getFD();
        bitmap = compressHelper.compressDescriptor(fileDescriptor, reqWidth, reqHeight);
        if (bitmap != null) {
            lruCacheHelper.addBitmapToMemoryCache(key, bitmap);
        }
    }

    return bitmap;
}
 
Example 2
@Test(timeout=10000)
public void testReadAndWrite() throws Exception {
  File path = new File(TEST_BASE, "testReadAndWrite");
  path.mkdirs();
  SharedFileDescriptorFactory factory =
      SharedFileDescriptorFactory.create("woot_",
          new String[] { path.getAbsolutePath() });
  FileInputStream inStream =
      factory.createDescriptor("testReadAndWrite", 4096);
  FileOutputStream outStream = new FileOutputStream(inStream.getFD());
  outStream.write(101);
  inStream.getChannel().position(0);
  Assert.assertEquals(101, inStream.read());
  inStream.close();
  outStream.close();
  FileUtil.fullyDelete(path);
}
 
Example 3
@Test(timeout=10000)
public void testReadAndWrite() throws Exception {
  File path = new File(TEST_BASE, "testReadAndWrite");
  path.mkdirs();
  SharedFileDescriptorFactory factory =
      SharedFileDescriptorFactory.create("woot_",
          new String[] { path.getAbsolutePath() });
  FileInputStream inStream =
      factory.createDescriptor("testReadAndWrite", 4096);
  FileOutputStream outStream = new FileOutputStream(inStream.getFD());
  outStream.write(101);
  inStream.getChannel().position(0);
  Assert.assertEquals(101, inStream.read());
  inStream.close();
  outStream.close();
  FileUtil.fullyDelete(path);
}
 
Example 4
Source Project: MyHearts   File: MediaPlayer.java    License: Apache License 2.0 6 votes vote down vote up
/**
  * Sets the data source (file-path or http/rtsp URL) to use.
  *
  * @param path the path of the file, or the http/rtsp URL of the stream you want to play
  * @param keys   AVOption key
  * @param values AVOption value
  * @throws IllegalStateException if it is called in an invalid state
  */
public void setDataSource(String path, String[] keys, String[] values) throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
	final Uri uri = Uri.parse(path);
	if ("file".equals(uri.getScheme())) {
		path = uri.getPath();
	}

	final File file = new File(path);
	if (file.exists()) {
		FileInputStream is = new FileInputStream(file);
		FileDescriptor fd = is.getFD();
		setDataSource(fd);
		is.close();
	} else {
		_setDataSource(path, keys, values);
	}
}
 
Example 5
Source Project: HPlayer   File: MediaPlayer.java    License: Apache License 2.0 6 votes vote down vote up
/**
  * Sets the data source (file-path or http/rtsp URL) to use.
  *
  * @param path the path of the file, or the http/rtsp URL of the stream you want to play
  * @param keys   AVOption key
  * @param values AVOption value
  * @throws IllegalStateException if it is called in an invalid state
  */
public void setDataSource(String path, String[] keys, String[] values) throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
	final Uri uri = Uri.parse(path);
	if ("file".equals(uri.getScheme())) {
		path = uri.getPath();
	}

	final File file = new File(path);
	if (file.exists()) {
		FileInputStream is = new FileInputStream(file);
		FileDescriptor fd = is.getFD();
		setDataSource(fd);
		is.close();
	} else {
		_setDataSource(path, keys, values);
	}
}
 
Example 6
Source Project: video-player   File: MediaPlayer.java    License: MIT License 6 votes vote down vote up
/**
  * Sets the data source (file-path or http/rtsp URL) to use.
  *
  * @param path the path of the file, or the http/rtsp URL of the stream you want to play
  * @param keys   AVOption key
  * @param values AVOption value
  * @throws IllegalStateException if it is called in an invalid state
  */
public void setDataSource(String path, String[] keys, String[] values) throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
	final Uri uri = Uri.parse(path);
	if ("file".equals(uri.getScheme())) {
		path = uri.getPath();
	}

	final File file = new File(path);
	if (file.exists()) {
		FileInputStream is = new FileInputStream(file);
		FileDescriptor fd = is.getFD();
		setDataSource(fd);
		is.close();
	} else {
		_setDataSource(path, keys, values);
	}
}
 
Example 7
@Test
public void testFileStreamWrappers() throws IOException {
    File f = File.createTempFile("aspectjinput", "txt");
    f.deleteOnExit();

    FileOutputStream fos = new FileOutputStream(f);
    assertTrue(fos instanceof FileOutputStreamWrapper);

    FileOutputStream fos2 = new FileOutputStream(f.getAbsolutePath());
    assertTrue(fos2 instanceof FileOutputStreamWrapper);

    FileOutputStream fos3 = new FileOutputStream(fos.getFD());
    assertTrue(fos3 instanceof FileOutputStreamWrapper);

    fos.write(5);
    fos.close();

    FileInputStream fis = new FileInputStream(f);
    assertTrue(fis instanceof FileInputStreamWrapper);

    FileInputStream fis2 = new FileInputStream(f.getAbsolutePath());
    assertTrue(fis2 instanceof FileInputStreamWrapper);

    FileInputStream fis3 = new FileInputStream(fis.getFD());
    assertTrue(fis3 instanceof FileInputStreamWrapper);
}
 
Example 8
/**
  * Sets the data source (file-path or http/rtsp URL) to use.
  *
  * @param path the path of the file, or the http/rtsp URL of the stream you want to play
  * @param keys   AVOption key
  * @param values AVOption value
  * @throws IllegalStateException if it is called in an invalid state
  */
public void setDataSource(String path, String[] keys, String[] values) throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
	final Uri uri = Uri.parse(path);
	if ("file".equals(uri.getScheme())) {
		path = uri.getPath();
	}

	final File file = new File(path);
	if (file.exists()) {
		FileInputStream is = new FileInputStream(file);
		FileDescriptor fd = is.getFD();
		setDataSource(fd);
		is.close();
	} else {
		_setDataSource(path, keys, values);
	}
}
 
Example 9
/**
  * Sets the data source (file-path or http/rtsp URL) to use.
  *
  * @param path the path of the file, or the http/rtsp URL of the stream you want to play
  * @param keys   AVOption key
  * @param values AVOption value
  * @throws IllegalStateException if it is called in an invalid state
  */
public void setDataSource(String path, String[] keys, String[] values) throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
	final Uri uri = Uri.parse(path);
	if ("file".equals(uri.getScheme())) {
		path = uri.getPath();
	}

	final File file = new File(path);
	if (file.exists()) {
		FileInputStream is = new FileInputStream(file);
		FileDescriptor fd = is.getFD();
		setDataSource(fd);
		is.close();
	} else {
		_setDataSource(path, keys, values);
	}
}
 
Example 10
Source Project: Vitamio   File: MediaPlayer.java    License: Apache License 2.0 6 votes vote down vote up
/**
  * Sets the data source (file-path or http/rtsp URL) to use.
  *
  * @param path the path of the file, or the http/rtsp URL of the stream you want to play
  * @param keys   AVOption key
  * @param values AVOption value
  * @throws IllegalStateException if it is called in an invalid state
  */
public void setDataSource(String path, String[] keys, String[] values) throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
	final Uri uri = Uri.parse(path);
	if ("file".equals(uri.getScheme())) {
		path = uri.getPath();
	}

	final File file = new File(path);
	if (file.exists()) {
		FileInputStream is = new FileInputStream(file);
		FileDescriptor fd = is.getFD();
		setDataSource(fd);
		is.close();
	} else {
		_setDataSource(path, keys, values);
	}
}
 
Example 11
Source Project: android-art-res   File: ImageLoader.java    License: Apache License 2.0 6 votes vote down vote up
private Bitmap loadBitmapFromDiskCache(String url, int reqWidth,
        int reqHeight) throws IOException {
    if (Looper.myLooper() == Looper.getMainLooper()) {
        Log.w(TAG, "load bitmap from UI Thread, it's not recommended!");
    }
    if (mDiskLruCache == null) {
        return null;
    }

    Bitmap bitmap = null;
    String key = hashKeyFormUrl(url);
    DiskLruCache.Snapshot snapShot = mDiskLruCache.get(key);
    if (snapShot != null) {
        FileInputStream fileInputStream = (FileInputStream)snapShot.getInputStream(DISK_CACHE_INDEX);
        FileDescriptor fileDescriptor = fileInputStream.getFD();
        bitmap = mImageResizer.decodeSampledBitmapFromFileDescriptor(fileDescriptor,
                reqWidth, reqHeight);
        if (bitmap != null) {
            addBitmapToMemoryCache(key, bitmap);
        }
    }

    return bitmap;
}
 
Example 12
Source Project: droidel   File: MediaPlayer.java    License: Apache License 2.0 6 votes vote down vote up
private void setDataSource(String path, String[] keys, String[] values)
        throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
    disableProxyListener();

    final Uri uri = Uri.parse(path);
    if ("file".equals(uri.getScheme())) {
        path = uri.getPath();
    }

    final File file = new File(path);
    if (file.exists()) {
        FileInputStream is = new FileInputStream(file);
        FileDescriptor fd = is.getFD();
        setDataSource(fd);
        is.close();
    } else {
        _setDataSource(path, keys, values);
    }
}
 
Example 13
Source Project: droidel   File: MediaPlayer.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * 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);
    }
}
 
Example 14
/**
 * Sets the data source (file-path or http/rtsp URL) to use.
 *
 * @param path   the path of the file, or the http/rtsp URL of the stream you want to play
 * @param keys   AVOption key
 * @param values AVOption value
 * @throws IllegalStateException if it is called in an invalid state
 */
public void setDataSource(String path, String[] keys, String[] values) throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
    final Uri uri = Uri.parse(path);
    if ("file".equals(uri.getScheme())) {
        path = uri.getPath();
    }

    final File file = new File(path);
    if (file.exists()) {
        FileInputStream is = new FileInputStream(file);
        FileDescriptor fd = is.getFD();
        setDataSource(fd);
        is.close();
    } else {
        native_set_datasource(path, keys, values);
    }
}
 
Example 15
Source Project: BambooPlayer   File: MediaPlayer.java    License: Apache License 2.0 6 votes vote down vote up
/**
  * Sets the data source (file-path or http/rtsp URL) to use.
  *
  * @param path the path of the file, or the http/rtsp URL of the stream you want to play
  * @param keys   AVOption key
  * @param values AVOption value
  * @throws IllegalStateException if it is called in an invalid state
  */
public void setDataSource(String path, String[] keys, String[] values) throws IOException, IllegalArgumentException, SecurityException, IllegalStateException {
	final Uri uri = Uri.parse(path);
	if ("file".equals(uri.getScheme())) {
		path = uri.getPath();
	}

	final File file = new File(path);
	if (file.exists()) {
		FileInputStream is = new FileInputStream(file);
		FileDescriptor fd = is.getFD();
		setDataSource(fd);
		is.close();
	} else {
		_setDataSource(path, keys, values);
	}
}
 
Example 16
Source Project: Ezalor   File: UtilsTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void getFilePathByFileDescriptor() throws Exception {
    File file = new File(Environment.getExternalStorageDirectory() + "/test.txt");
    if (!file.exists()) {
        file.createNewFile();
    }
    FileInputStream is = new FileInputStream(file);
    FileDescriptor fileDescriptor = is.getFD();
    int fd = ReflectUtils.getObjectByFieldName(fileDescriptor, "descriptor", Integer.class);

    Log.i(TAG, "filePath:" + Utils.readlink(fd));
}
 
Example 17
Source Project: TvLauncher   File: BitmapUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * get Bitmap
 *
 * @param imgFile
 * @param minSideLength
 * @param maxNumOfPixels
 * @return
 */
public static Bitmap tryGetBitmap(String imgFile, int minSideLength, int maxNumOfPixels) {
    if (imgFile == null || imgFile.length() == 0)
        return null;

    try {
        FileInputStream fi = new FileInputStream(imgFile);
        FileDescriptor fd = fi.getFD();
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;

        BitmapFactory.decodeFileDescriptor(fd, null, options);

        options.inSampleSize = computeSampleSize(options, minSideLength, maxNumOfPixels);
        try {
            // 这里一定要将其设置回false,因为之前我们将其设置成了true
            // 设置inJustDecodeBounds为true后,decodeFile并不分配空间,即,BitmapFactory解码出来的Bitmap为Null,但可计算出原始图片的长度和宽度
            options.inJustDecodeBounds = false;

            Bitmap bmp = BitmapFactory.decodeFile(imgFile, options);
            fi.close();
            return bmp == null ? null : bmp;
        } catch (OutOfMemoryError err) {
            System.gc();
            System.runFinalization();
            return null;
        }
    } catch (Exception e) {
        return null;
    }
}
 
Example 18
Source Project: tilt-game-android   File: MusicFactory.java    License: MIT License 5 votes vote down vote up
public static Music createMusicFromFile(final MusicManager pMusicManager, final File pFile) throws IOException {
	final MediaPlayer mediaPlayer = new MediaPlayer();

	final FileInputStream fileInputStream = new FileInputStream(pFile);
	final FileDescriptor fileDescriptor = fileInputStream.getFD();
	StreamUtils.close(fileInputStream);
	mediaPlayer.setDataSource(fileDescriptor);
	mediaPlayer.prepare();

	final Music music = new Music(pMusicManager, mediaPlayer);
	pMusicManager.add(music);

	return music;
}
 
Example 19
public void process() {
    try {
        FileDescriptor[] pipes = Os.pipe();
        mInterruptFd = pipes[0];
        mBlockFd = pipes[1];
        FileInputStream inputStream = new FileInputStream(descriptor.getFileDescriptor());
        FileOutputStream outputStream = new FileOutputStream(descriptor.getFileDescriptor());

        byte[] packet = new byte[32767];
        while (running) {
            StructPollfd deviceFd = new StructPollfd();
            deviceFd.fd = inputStream.getFD();
            deviceFd.events = (short) OsConstants.POLLIN;
            StructPollfd blockFd = new StructPollfd();
            blockFd.fd = mBlockFd;
            blockFd.events = (short) (OsConstants.POLLHUP | OsConstants.POLLERR);

            if (!deviceWrites.isEmpty())
                deviceFd.events |= (short) OsConstants.POLLOUT;

            StructPollfd[] polls = new StructPollfd[2];
            polls[0] = deviceFd;
            polls[1] = blockFd;
            Os.poll(polls, 100);
            if (blockFd.revents != 0) {
                Log.i(TAG, "Told to stop VPN");
                running = false;
                return;
            }

            Iterator<WaitingHttpsRequest> iterator = whqList.iterator();
            while (iterator.hasNext()) {
                WaitingHttpsRequest request = iterator.next();
                if (request.completed) {
                    handleDnsResponse(request.packet, request.result);
                    iterator.remove();
                }
            }

            if ((deviceFd.revents & OsConstants.POLLOUT) != 0) {
                Log.d(TAG, "Write to device");
                writeToDevice(outputStream);
            }
            if ((deviceFd.revents & OsConstants.POLLIN) != 0) {
                Log.d(TAG, "Read from device");
                readPacketFromDevice(inputStream, packet);
            }
            service.providerLoopCallback();
        }
    } catch (Exception e) {
        Logger.logException(e);
    }
}