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