Search in sources :

Example 31 with FileChannel

use of java.nio.channels.FileChannel in project platform_frameworks_base by android.

the class FontFamily method addFont.

public boolean addFont(String path, int ttcIndex) {
    try (FileInputStream file = new FileInputStream(path)) {
        FileChannel fileChannel = file.getChannel();
        long fontSize = fileChannel.size();
        ByteBuffer fontBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fontSize);
        return nAddFont(mNativePtr, fontBuffer, ttcIndex);
    } catch (IOException e) {
        Log.e(TAG, "Error mapping font file " + path);
        return false;
    }
}
Also used : FileChannel(java.nio.channels.FileChannel) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) FileInputStream(java.io.FileInputStream)

Example 32 with FileChannel

use of java.nio.channels.FileChannel in project platform_frameworks_base by android.

the class Typeface method makeFamilyFromParsed.

private static FontFamily makeFamilyFromParsed(FontListParser.Family family, Map<String, ByteBuffer> bufferForPath) {
    FontFamily fontFamily = new FontFamily(family.lang, family.variant);
    for (FontListParser.Font font : family.fonts) {
        ByteBuffer fontBuffer = bufferForPath.get(font.fontName);
        if (fontBuffer == null) {
            try (FileInputStream file = new FileInputStream(font.fontName)) {
                FileChannel fileChannel = file.getChannel();
                long fontSize = fileChannel.size();
                fontBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fontSize);
                bufferForPath.put(font.fontName, fontBuffer);
            } catch (IOException e) {
                Log.e(TAG, "Error mapping font file " + font.fontName);
                continue;
            }
        }
        if (!fontFamily.addFontWeightStyle(fontBuffer, font.ttcIndex, font.axes, font.weight, font.isItalic)) {
            Log.e(TAG, "Error creating font " + font.fontName + "#" + font.ttcIndex);
        }
    }
    return fontFamily;
}
Also used : FileChannel(java.nio.channels.FileChannel) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) FileInputStream(java.io.FileInputStream)

Example 33 with FileChannel

use of java.nio.channels.FileChannel in project androidquery by androidquery.

the class AbstractAQuery method makeSharedFile.

/**
	 * Create a temporary file on EXTERNAL storage (sdcard) that holds the cached content of the url.
	 * Returns null if url is not cached, or the system cannot create such file (sdcard is absent, such as in emulator).
	 * 
	 * The returned file is accessable to all apps, therefore it is ideal for sharing content (such as photo) via the intent mechanism.
	 * 
	 * <br>
	 * <br>
	 * Example Usage:
	 * 
	 * <pre>
	 *	Intent intent = new Intent(Intent.ACTION_SEND);
	 *	intent.setType("image/jpeg");
	 *	intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
	 *	startActivityForResult(Intent.createChooser(intent, "Share via:"), 0);
	 * </pre>
	 * 
	 * <br>
	 * The temp file will be deleted when AQUtility.cleanCacheAsync is invoked, or the file can be explicitly deleted after use.
	 * 
	 * @param url The url of the desired cached content.
	 * @param filename The desired file name, which might be used by other apps to describe the content, such as an email attachment.
	 * @return temp file
	 * 
	 */
public File makeSharedFile(String url, String filename) {
    File file = null;
    try {
        File cached = getCachedFile(url);
        if (cached != null) {
            File temp = AQUtility.getTempDir();
            if (temp != null) {
                file = new File(temp, filename);
                file.createNewFile();
                FileInputStream fis = new FileInputStream(cached);
                FileOutputStream fos = new FileOutputStream(file);
                FileChannel ic = fis.getChannel();
                FileChannel oc = fos.getChannel();
                try {
                    ic.transferTo(0, ic.size(), oc);
                } finally {
                    AQUtility.close(fis);
                    AQUtility.close(fos);
                    AQUtility.close(ic);
                    AQUtility.close(oc);
                }
            }
        }
    } catch (Exception e) {
        AQUtility.debug(e);
    }
    return file;
}
Also used : FileChannel(java.nio.channels.FileChannel) FileOutputStream(java.io.FileOutputStream) File(java.io.File) FileInputStream(java.io.FileInputStream) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 34 with FileChannel

use of java.nio.channels.FileChannel in project otter by alibaba.

the class NioUtils method copy.

/**
     * 基于流的数据copy
     */
public static long copy(InputStream input, OutputStream output) throws IOException {
    long count = 0;
    long n = 0;
    if (input instanceof FileInputStream) {
        FileChannel inChannel = ((FileInputStream) input).getChannel();
        WritableByteChannel outChannel = Channels.newChannel(output);
        count = inChannel.transferTo(0, inChannel.size(), outChannel);
    } else if (output instanceof FileOutputStream) {
        FileChannel outChannel = ((FileOutputStream) output).getChannel();
        ReadableByteChannel inChannel = Channels.newChannel(input);
        do {
            n = outChannel.transferFrom(inChannel, count, DEFAULT_BUFFER_SIZE);
            count += n;
        } while (n > 0);
    } else {
        byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
        while (-1 != (n = input.read(buffer))) {
            output.write(buffer, 0, (int) n);
            count += n;
        }
    // ReadableByteChannel inChannel = Channels.newChannel(input);
    // WritableByteChannel outChannel = Channels.newChannel(output);
    //            
    // //ByteBuffer buffer = new ByteBuffer(DEFAULT_BUFFER_SIZE);
    // ByteBuffer buffer = ByteBuffer.allocateDirect(DEFAULT_BUFFER_SIZE);
    // while (-1 != (n = inChannel.read(buffer))) {
    // outChannel.write(buffer);
    // count += n;
    // }
    }
    return count;
}
Also used : ReadableByteChannel(java.nio.channels.ReadableByteChannel) FileChannel(java.nio.channels.FileChannel) FileOutputStream(java.io.FileOutputStream) WritableByteChannel(java.nio.channels.WritableByteChannel) FileInputStream(java.io.FileInputStream)

Example 35 with FileChannel

use of java.nio.channels.FileChannel in project graphhopper by graphhopper.

the class NativeFSLockFactory method main.

public static void main(String[] args) throws IOException {
    // trying FileLock mechanics in different processes
    File file = new File("tmp.lock");
    file.createNewFile();
    FileChannel channel = new RandomAccessFile(file, "r").getChannel();
    boolean shared = true;
    FileLock lock1 = channel.tryLock(0, Long.MAX_VALUE, shared);
    System.out.println("locked " + lock1);
    System.in.read();
    System.out.println("release " + lock1);
    lock1.release();
}
Also used : RandomAccessFile(java.io.RandomAccessFile) FileChannel(java.nio.channels.FileChannel) FileLock(java.nio.channels.FileLock) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File)

Aggregations

FileChannel (java.nio.channels.FileChannel)676 IOException (java.io.IOException)247 ByteBuffer (java.nio.ByteBuffer)215 File (java.io.File)199 FileInputStream (java.io.FileInputStream)177 FileOutputStream (java.io.FileOutputStream)167 RandomAccessFile (java.io.RandomAccessFile)151 Test (org.junit.Test)95 MappedByteBuffer (java.nio.MappedByteBuffer)82 Path (java.nio.file.Path)42 FileLock (java.nio.channels.FileLock)34 FileNotFoundException (java.io.FileNotFoundException)32 ArrayList (java.util.ArrayList)14 Random (java.util.Random)13 OutputStream (java.io.OutputStream)12 ReadableByteChannel (java.nio.channels.ReadableByteChannel)12 OverlappingFileLockException (java.nio.channels.OverlappingFileLockException)11 AsynchronousFileChannel (java.nio.channels.AsynchronousFileChannel)10 LinkedList (java.util.LinkedList)10 ProjectWorkspace (com.facebook.buck.testutil.integration.ProjectWorkspace)9