use of java.nio.MappedByteBuffer in project ceylon-compiler by ceylon.
the class NewProjectToolTests method assertMatchInFile.
private void assertMatchInFile(File file, Pattern pattern) throws Exception {
Charset charset = Charset.forName("UTF-8");
FileInputStream stream = new FileInputStream(file);
try {
FileChannel channel = stream.getChannel();
try {
MappedByteBuffer map = channel.map(MapMode.READ_ONLY, 0, channel.size());
CharBuffer chars = charset.decode(map);
Matcher matcher = pattern.matcher(chars);
Assert.assertTrue(pattern + " not found in " + file, matcher.find());
} finally {
channel.close();
}
} finally {
stream.close();
}
}
use of java.nio.MappedByteBuffer in project FastDev4Android by jiangqqlmj.
the class StrUtils method calcMd5.
/**
* @param file
* 文件
* @return 文件转换MD5
*/
public static String calcMd5(File file) {
FileInputStream in = null;
try {
MessageDigest algorithm = MessageDigest.getInstance("MD5");
algorithm.reset();
in = new FileInputStream(file);
FileChannel ch = in.getChannel();
MappedByteBuffer byteBuffer;
byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
algorithm.update(byteBuffer);
return toHexString(algorithm.digest(), "");
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException(e);
} catch (FileNotFoundException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
use of java.nio.MappedByteBuffer in project jvm-tools by aragozin.
the class HprofLongMappedByteBuffer method get.
synchronized void get(long position, byte[] chars) {
MappedByteBuffer buffer = dumpBuffer[getBufferIndex(position)];
buffer.position(getBufferOffset(position));
buffer.get(chars);
}
use of java.nio.MappedByteBuffer in project Japid by branaway.
the class CompilerTests method readFile.
private static String readFile(String path) throws IOException {
FileInputStream stream = new FileInputStream(new File(path));
try {
FileChannel fc = stream.getChannel();
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
/* Instead of using default, pass in a decoder. */
return Charset.forName("UTF-8").decode(bb).toString();
} finally {
stream.close();
}
}
use of java.nio.MappedByteBuffer in project druid by druid-io.
the class StringDimensionMergerV9 method writeIndexes.
@Override
public void writeIndexes(List<IntBuffer> segmentRowNumConversions, Closer closer) throws IOException {
long dimStartTime = System.currentTimeMillis();
final BitmapSerdeFactory bitmapSerdeFactory = indexSpec.getBitmapSerdeFactory();
String bmpFilename = String.format("%s.inverted", dimensionName);
bitmapWriter = new GenericIndexedWriter<>(ioPeon, bmpFilename, bitmapSerdeFactory.getObjectStrategy());
bitmapWriter.open();
// write dim values to one single file because we need to read it
File dimValueFile = IndexIO.makeDimFile(outDir, dimensionName);
try (FileOutputStream fos = new FileOutputStream(dimValueFile)) {
ByteStreams.copy(dictionaryWriter.combineStreams(), fos);
}
final MappedByteBuffer dimValsMapped = Files.map(dimValueFile);
try (Closeable toCloseEncodedValueWriter = encodedValueWriter;
Closeable toCloseBitmapWriter = bitmapWriter;
Closeable dimValsMappedUnmapper = new Closeable() {
@Override
public void close() {
ByteBufferUtils.unmap(dimValsMapped);
}
}) {
Indexed<String> dimVals = GenericIndexed.read(dimValsMapped, GenericIndexed.STRING_STRATEGY);
BitmapFactory bmpFactory = bitmapSerdeFactory.getBitmapFactory();
RTree tree = null;
boolean hasSpatial = capabilities.hasSpatialIndexes();
if (hasSpatial) {
spatialWriter = new ByteBufferWriter<>(ioPeon, String.format("%s.spatial", dimensionName), new IndexedRTree.ImmutableRTreeObjectStrategy(bmpFactory));
spatialWriter.open();
tree = new RTree(2, new LinearGutmanSplitStrategy(0, 50, bmpFactory), bmpFactory);
}
IndexSeeker[] dictIdSeeker = toIndexSeekers(adapters, dimConversions, dimensionName);
//Iterate all dim values's dictionary id in ascending order which in line with dim values's compare result.
for (int dictId = 0; dictId < dimVals.size(); dictId++) {
progress.progress();
mergeBitmaps(segmentRowNumConversions, dimVals, bmpFactory, tree, hasSpatial, dictIdSeeker, dictId, adapters, dimensionName, nullRowsBitmap, bitmapWriter);
}
if (hasSpatial) {
spatialWriter.write(ImmutableRTree.newImmutableFromMutable(tree));
spatialWriter.close();
}
log.info("Completed dim[%s] inverted with cardinality[%,d] in %,d millis.", dimensionName, dimVals.size(), System.currentTimeMillis() - dimStartTime);
}
}
Aggregations