use of java.nio.MappedByteBuffer in project android_frameworks_base by ResurrectionRemix.
the class Hyphenator method loadHyphenator.
private static Hyphenator loadHyphenator(String languageTag) {
String patternFilename = "hyph-" + languageTag.toLowerCase(Locale.US) + ".hyb";
File patternFile = new File(getSystemHyphenatorLocation(), patternFilename);
try {
RandomAccessFile f = new RandomAccessFile(patternFile, "r");
try {
FileChannel fc = f.getChannel();
MappedByteBuffer buf = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
long nativePtr = StaticLayout.nLoadHyphenator(buf, 0);
return new Hyphenator(nativePtr, buf);
} finally {
f.close();
}
} catch (IOException e) {
Log.e(TAG, "error loading hyphenation " + patternFile, e);
return null;
}
}
use of java.nio.MappedByteBuffer in project opennms by OpenNMS.
the class AbstractSystemReportPlugin method slurp.
protected String slurp(final File lsb) {
if (lsb != null && lsb.exists()) {
FileInputStream stream = null;
try {
stream = new FileInputStream(lsb);
FileChannel fc = stream.getChannel();
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
return Charset.defaultCharset().decode(bb).toString().replace("[\\r\\n]*$", "");
} catch (final Exception e) {
LOG.debug("Unable to read from file '{}'", lsb.getPath(), e);
} finally {
IOUtils.closeQuietly(stream);
}
}
return null;
}
use of java.nio.MappedByteBuffer in project tdi-studio-se by Talend.
the class FileCopy method copyFileL.
private static void copyFileL(String srcFileName, FileInputStream srcInputStream, String desFileName, boolean delSrc) throws Exception {
File source = new File(srcFileName);
File dest = new File(desFileName);
FileChannel in = null, out = null;
try {
in = srcInputStream.getChannel();
out = new FileOutputStream(dest).getChannel();
long size = in.size();
long position = 0;
final long MAP_SIZE = 33525760;
MappedByteBuffer buf = null;
while (true) {
if (position + MAP_SIZE >= size) {
buf = in.map(FileChannel.MapMode.READ_ONLY, position, size - position);
out.write(buf);
//For But TDI-26493, here must clean first, or it can't delete
clean(buf);
break;
} else {
buf = in.map(FileChannel.MapMode.READ_ONLY, position, MAP_SIZE);
out.write(buf);
// here must clean first, or it can't delete
clean(buf);
position += MAP_SIZE;
}
}
in.close();
out.close();
if (delSrc) {
source.delete();
}
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
use of java.nio.MappedByteBuffer in project EventHub by Codecademy.
the class UserEventIndexModule method getBlockFactory.
@Provides
public UserEventIndex.Block.Factory getBlockFactory(@Named("eventhub.usereventindex.directory") final String directory, @Named("eventhub.usereventindex.blockCacheSize") int blockCacheSize, @Named("eventhub.usereventindex.numRecordsPerBlock") int numRecordsPerBlock, @Named("eventhub.usereventindex.numBlocksPerFile") int numBlocksPerFile) {
final int fileSize = numBlocksPerFile * (numRecordsPerBlock * UserEventIndex.ID_SIZE + UserEventIndex.Block.MetaData.SIZE);
LoadingCache<Integer, MappedByteBuffer> buffers = CacheBuilder.newBuilder().maximumSize(blockCacheSize).recordStats().removalListener(new RemovalListener<Integer, MappedByteBuffer>() {
@Override
public void onRemoval(RemovalNotification<Integer, MappedByteBuffer> notification) {
MappedByteBuffer value = notification.getValue();
if (value != null) {
value.force();
}
}
}).build(new CacheLoader<Integer, MappedByteBuffer>() {
@Override
public MappedByteBuffer load(Integer key) throws Exception {
return ByteBufferUtil.createNewBuffer(String.format("%s/block_%d.mem", directory, key), fileSize);
}
});
String filename = directory + "block_factory.ser";
File file = new File(filename);
if (file.exists()) {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file))) {
long currentPointer = ois.readLong();
return new UserEventIndex.Block.Factory(filename, buffers, numRecordsPerBlock, numBlocksPerFile, currentPointer);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return new UserEventIndex.Block.Factory(filename, buffers, numRecordsPerBlock, numBlocksPerFile, 0);
}
use of java.nio.MappedByteBuffer in project EventHub by Codecademy.
the class DmaList method build.
public static <T> DmaList<T> build(final Schema<T> schema, final String directory, final int numRecordsPerFile, int cacheSize) {
//noinspection ResultOfMethodCallIgnored
new File(directory).mkdirs();
try (RandomAccessFile raf = new RandomAccessFile(new File(String.format("%s/meta_data.mem", directory)), "rw")) {
MappedByteBuffer metaDataBuffer = raf.getChannel().map(FileChannel.MapMode.READ_WRITE, 0, 8);
long numRecords = metaDataBuffer.getLong();
final int fileSize = numRecordsPerFile * schema.getObjectSize();
LoadingCache<Integer, MappedByteBuffer> buffers = CacheBuilder.newBuilder().maximumSize(cacheSize).recordStats().removalListener(new RemovalListener<Integer, MappedByteBuffer>() {
@Override
public void onRemoval(RemovalNotification<Integer, MappedByteBuffer> notification) {
MappedByteBuffer value = notification.getValue();
if (value != null) {
value.force();
}
}
}).build(new CacheLoader<Integer, MappedByteBuffer>() {
@Override
public MappedByteBuffer load(Integer key) throws Exception {
return ByteBufferUtil.createNewBuffer(String.format("%s/dma_list_%d.mem", directory, key), fileSize);
}
});
return new DmaList<>(directory, schema, metaDataBuffer, buffers, numRecords, numRecordsPerFile);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
Aggregations