Example usage for java.io FileInputStream getFD

List of usage examples for java.io FileInputStream getFD

Introduction

In this page you can find the example usage for java.io FileInputStream getFD.

Prototype

public final FileDescriptor getFD() throws IOException 

Source Link

Document

Returns the FileDescriptor object that represents the connection to the actual file in the file system being used by this FileInputStream.

Usage

From source file:Main.java

public static void main(String[] args) throws IOException {
    FileInputStream fis = new FileInputStream("C://test.txt");

    // get file descriptor
    FileDescriptor fd = fis.getFD();

    // tests if the file is valid
    boolean bool = fd.valid();

    System.out.println("Valid file: " + bool);

}

From source file:MainClass.java

public static void main(String[] a) {
    File aFile = new File("C:/myFile.text");
    FileInputStream inputFile1 = null;
    FileDescriptor fd = null;//from  ww  w.  java2  s. c  om
    try {
        inputFile1 = new FileInputStream(aFile);
        fd = inputFile1.getFD();
    } catch (IOException e) {
        e.printStackTrace(System.err);
        System.exit(1);
    }
    FileInputStream inputFile2 = new FileInputStream(fd);
}

From source file:Main.java

public static void playRecordAction(File audiofile) throws Exception {
    MediaPlayer mp = new MediaPlayer();
    FileInputStream fis = new FileInputStream(audiofile.getAbsolutePath());
    mp.setDataSource(fis.getFD());
    mp.prepare();/*from  w w w.  ja va  2  s . c o m*/
    fis.close();
    mp.start();
}

From source file:Main.java

public static void setImageViewWidthBitmap(String imgPath, ImageView imageView) throws IOException {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*from  w ww .  j  av  a 2s. com*/
    BitmapFactory.decodeFile(imgPath, options);
    options.inSampleSize = Math.min(options.outWidth / imageView.getWidth(),
            options.outHeight / imageView.getWidth());
    options.inJustDecodeBounds = false;
    options.inPurgeable = true;
    options.inInputShareable = true;

    FileInputStream fis = new FileInputStream(imgPath);
    imageView.setImageBitmap(BitmapFactory.decodeFileDescriptor(fis.getFD(), null, options));
}

From source file:Main.java

public static boolean is3gpFileAudio(String url) {
    int height = 0;
    File mediaFile = new File(url);
    try {// w ww.  j  a  va 2s . c  o m
        MediaPlayer mp = new MediaPlayer();
        FileInputStream fs;
        FileDescriptor fd;
        fs = new FileInputStream(mediaFile);
        fd = fs.getFD();
        mp.setDataSource(fd);
        mp.prepare();
        height = mp.getVideoHeight();
        mp.release();
    } catch (Exception e) {
        Log.e("KKIM", "Exception trying to determine if 3gp file is video.", e);
    }
    Log.i("KKIM", "The height of the file is " + height);
    return height == 0;
}

From source file:Main.java

public static BitmapDrawable getBitmapDrawableFromUrl(Resources res, URL trueUrl,
        BitmapFactory.Options mOptions) throws Exception {
    Bitmap bitmap = null;/*  w  ww  .  ja  v  a2  s .  c o m*/
    FileInputStream mFS = null;
    try {
        mFS = new FileInputStream(trueUrl.getPath());
        bitmap = BitmapFactory.decodeFileDescriptor(mFS.getFD(), null, mOptions);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (mFS != null) {
            mFS.close();
        }
    }
    return new BitmapDrawable(res, bitmap);
}

From source file:Main.java

public static Bitmap getBitmap(String path, int requiredSize) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;/*from   w w w.  j  a  v a2  s.  c o  m*/
    BitmapFactory.decodeFile(path, options);
    int scale = 1;

    while (options.outWidth / scale > requiredSize && options.outHeight / scale > requiredSize)
        scale *= 2;

    options.inSampleSize = scale;
    options.inJustDecodeBounds = false;
    options.inDither = false;
    options.inPurgeable = true;
    options.inInputShareable = true;
    options.inTempStorage = new byte[32 * 1024];

    Bitmap result = null;
    File file = new File(path);
    FileInputStream fs = null;
    try {
        fs = new FileInputStream(file);

        try {
            result = BitmapFactory.decodeFileDescriptor(fs.getFD(), null, options);
        } catch (OutOfMemoryError oom) {
            oom.printStackTrace();

            try {
                options.inSampleSize *= 4;
                result = BitmapFactory.decodeFileDescriptor(fs.getFD(), null, options);
            } catch (OutOfMemoryError oom1) {
                oom.printStackTrace();
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (fs != null)
            try {
                fs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
    }

    return result;
}

From source file:org.apache.hadoop.hdfs.BlockReaderLocalBase.java

/**
 * The only way this object can be instantiated.
 *///from   w  w w . ja  va  2 s .c  om
public static BlockReaderLocalBase newBlockReader(Configuration conf, String file, int namespaceid, Block blk,
        DatanodeInfo node, long startOffset, long length, DFSClientMetrics metrics, boolean verifyChecksum,
        boolean clearOsBuffer, boolean positionalReadMode) throws IOException {

    LocalDatanodeInfo localDatanodeInfo = getLocalDatanodeInfo(node);

    BlockPathInfo pathinfo = localDatanodeInfo.getOrComputePathInfo(namespaceid, blk, node, conf);

    // Another alternative is for datanode to pass whether it is an inline checksum
    // file and checksum metadata through BlockPathInfo, which is a cleaner approach.
    // However, we need to worry more about protocol compatible issue. We avoid this
    // trouble for now. We can always change to the other approach later.
    //
    boolean isInlineChecksum = Block.isInlineChecksumBlockFilename(new Path(pathinfo.getBlockPath()).getName());

    // check to see if the file exists. It may so happen that the
    // HDFS file has been deleted and this block-lookup is occuring
    // on behalf of a new HDFS file. This time, the block file could
    // be residing in a different portion of the fs.data.dir directory.
    // In this case, we remove this entry from the cache. The next
    // call to this method will repopulate the cache.
    try {

        // get a local file system
        FileChannel dataFileChannel;
        FileDescriptor dataFileDescriptor;
        File blkfile = new File(pathinfo.getBlockPath());
        FileInputStream fis = new FileInputStream(blkfile);
        dataFileChannel = fis.getChannel();
        dataFileDescriptor = fis.getFD();

        if (LOG.isDebugEnabled()) {
            LOG.debug("New BlockReaderLocal for file " + pathinfo.getBlockPath() + " of size "
                    + blkfile.length() + " startOffset " + startOffset + " length " + length);
        }

        DataChecksum checksum = null;
        if (isInlineChecksum) {
            GenStampAndChecksum gac = BlockInlineChecksumReader
                    .getGenStampAndChecksumFromInlineChecksumFile(new Path(pathinfo.getBlockPath()).getName());
            checksum = DataChecksum.newDataChecksum(gac.getChecksumType(), gac.getBytesPerChecksum());

            if (verifyChecksum) {

                return new BlockReaderLocalInlineChecksum(conf, file, blk, startOffset, length, pathinfo,
                        metrics, checksum, verifyChecksum, dataFileChannel, dataFileDescriptor, clearOsBuffer,
                        positionalReadMode);
            } else {
                return new BlockReaderLocalInlineChecksum(conf, file, blk, startOffset, length, pathinfo,
                        metrics, checksum, dataFileChannel, dataFileDescriptor, clearOsBuffer,
                        positionalReadMode);
            }
        } else if (verifyChecksum) {
            FileChannel checksumInChannel = null;
            // get the metadata file
            File metafile = new File(pathinfo.getMetaPath());
            FileInputStream checksumIn = new FileInputStream(metafile);
            checksumInChannel = checksumIn.getChannel();
            // read and handle the common header here. For now just a version
            BlockMetadataHeader header = BlockMetadataHeader.readHeader(new DataInputStream(checksumIn),
                    new NativeCrc32());
            short version = header.getVersion();

            if (version != FSDataset.FORMAT_VERSION_NON_INLINECHECKSUM) {
                LOG.warn("Wrong version (" + version + ") for metadata file for " + blk + " ignoring ...");
            }
            checksum = header.getChecksum();

            return new BlockReaderLocalWithChecksum(conf, file, blk, startOffset, length, pathinfo, metrics,
                    checksum, verifyChecksum, dataFileChannel, dataFileDescriptor, checksumInChannel,
                    clearOsBuffer, positionalReadMode);
        } else {
            return new BlockReaderLocalWithChecksum(conf, file, blk, startOffset, length, pathinfo, metrics,
                    dataFileChannel, dataFileDescriptor, clearOsBuffer, positionalReadMode);
        }

    } catch (FileNotFoundException e) {
        localDatanodeInfo.removeBlockLocalPathInfo(namespaceid, blk);
        DFSClient.LOG.warn("BlockReaderLoca: Removing " + blk + " from cache because local file "
                + pathinfo.getBlockPath() + " could not be opened.");
        throw e;
    }
}

From source file:com.benefit.buy.library.http.query.callback.BitmapAjaxCallback.java

private static Bitmap decodeFile(String path, BitmapFactory.Options options, boolean rotate) {
    Bitmap result = null;/*from  w  ww  . java2s .  c  om*/
    if (options == null) {
        options = new Options();
    }
    options.inInputShareable = true;
    options.inPurgeable = true;
    FileInputStream fis = null;
    try {
        fis = new FileInputStream(path);
        FileDescriptor fd = fis.getFD();
        result = BitmapFactory.decodeFileDescriptor(fd, null, options);
        if ((result != null) && rotate) {
            result = rotate(path, result);
        }
    } catch (IOException e) {
        AQUtility.report(e);
    } finally {
        AQUtility.close(fis);
    }
    return result;
}

From source file:com.appbase.androidquery.callback.BitmapAjaxCallback.java

private static Bitmap decodeFile(String path, BitmapFactory.Options options, boolean rotate) {

    Bitmap result = null;//  w  ww .  ja v  a 2  s.c o  m

    if (options == null) {
        options = new Options();
    }

    options.inInputShareable = true;
    options.inPurgeable = true;

    FileInputStream fis = null;

    try {

        fis = new FileInputStream(path);
        FileDescriptor fd = fis.getFD();
        result = BitmapFactory.decodeFileDescriptor(fd, null, options);

        if (result != null && rotate) {
            result = rotate(path, result);
        }

    } catch (IOException e) {
        AQUtility.report(e);
    } finally {
        AQUtility.close(fis);
    }

    return result;

}