本文整理汇总了Java中java.nio.channels.FileChannel.map方法的典型用法代码示例。如果您正苦于以下问题:Java FileChannel.map方法的具体用法?Java FileChannel.map怎么用?Java FileChannel.map使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.nio.channels.FileChannel
的用法示例。
在下文中一共展示了FileChannel.map方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: copyFile
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
/** Copia un fichero.
* @param source Fichero origen con el contenido que queremos copiar.
* @param dest Fichero destino de los datos.
* @throws IOException SI ocurre algun problema durante la copia */
public static void copyFile(final File source, final File dest) throws IOException {
if (source == null || dest == null) {
throw new IllegalArgumentException("Ni origen ni destino de la copia pueden ser nulos"); //$NON-NLS-1$
}
final FileInputStream is = new FileInputStream(source);
final FileOutputStream os = new FileOutputStream(dest);
final FileChannel in = is.getChannel();
final FileChannel out = os.getChannel();
final MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, in.size());
out.write(buf);
in.close();
out.close();
is.close();
os.close();
}
示例2: call
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
public ByteBuffer call() throws Exception {
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
buff = ByteBuffer.allocate(bufferSize);
serverSocketChannel.socket().bind(new InetSocketAddress(port));
RandomAccessFile temp = new RandomAccessFile(tempName, "rw");
MappedByteBuffer b;
while (!stop.isLocked()) {
sync=0;
SocketChannel socketChannel = serverSocketChannel.accept();
socketChannel.read(buff);
FileChannel channel = temp.getChannel();
channel.write(buff);
if (!pause.isLocked()) {
b = channel.map(MapMode.READ_WRITE, 0, (long) bufferSize);
sync = 1;
if(sync==2){
b.clear();
}
}
buff.clear();
}
temp.close();
return null;
}
示例3: insertTagAndShift
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
/**
* Insert new metadata into file by using memory mapped file, and if fails write in chunks
* <p>
* But this is problematic on 32bit systems for large flac files may not be able to map a contiguous address space large enough
* for a large audio size , so no longer used since better to go straight to using chunks
*
* @param tag
* @param fc
* @param blockInfo
* @param flacStream
* @param neededRoom
* @param availableRoom
* @throws IOException
* @throws UnsupportedEncodingException
*/
private void insertTagAndShift(File file, Tag tag, FileChannel fc, MetadataBlockInfo blockInfo, FlacStreamReader flacStream, int neededRoom, int availableRoom) throws IOException, UnsupportedEncodingException {
int headerLength = flacStream.getStartOfFlacInFile() + FlacStreamReader.FLAC_STREAM_IDENTIFIER_LENGTH + MetadataBlockHeader.HEADER_LENGTH // this should be the length of the block header for the stream info
+ MetadataBlockDataStreamInfo.STREAM_INFO_DATA_LENGTH;
long targetSizeBeforeAudioData = headerLength + neededRoom + FlacTagCreator.DEFAULT_PADDING;
long remainderTargetSize = fc.size() - (headerLength + availableRoom);
long totalTargetSize = targetSizeBeforeAudioData + remainderTargetSize;
MappedByteBuffer mappedFile = null;
try {
//Use ByteBuffer
mappedFile = fc.map(MapMode.READ_WRITE, 0, totalTargetSize);
insertTagAndShiftViaMappedByteBuffer(tag, mappedFile, fc, targetSizeBeforeAudioData, totalTargetSize, blockInfo, flacStream, neededRoom, availableRoom);
} catch (IOException ioe) {
//#175: Flac Map error on write
if (mappedFile == null) {
insertUsingChunks(file, tag, fc, blockInfo, flacStream, neededRoom + FlacTagCreator.DEFAULT_PADDING, availableRoom);
} else {
logger.log(Level.SEVERE, ioe.getMessage(), ioe);
throw ioe;
}
}
}
示例4: CommandLogReader
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
public CommandLogReader(String path) {
FileChannel roChannel = null;
ByteBuffer readonlybuffer = null;
File f = new File(path);
try {
roChannel = new RandomAccessFile(f, "r").getChannel();
LOG.trace("File Size :"+roChannel.size());
readonlybuffer = roChannel.map(FileChannel.MapMode.READ_ONLY, 0, (int)roChannel.size());
LOG.trace("Opened file :"+f.getAbsolutePath());
LOG.trace("Size :"+readonlybuffer.remaining());
} catch (IOException ex) {
LOG.trace("Failed to open file :"+f.getAbsolutePath());
throw new RuntimeException(ex);
}
assert(readonlybuffer != null);
this.fd = new FastDeserializer(readonlybuffer);
this.procedures = this.readHeader();
}
示例5: writeFileFromBytesByMap
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
/**
* 将字节数组写入文件
*
* @param file 文件
* @param bytes 字节数组
* @param append 是否追加在文件末
* @param isForce 是否写入文件
* @return {@code true}: 写入成功<br>{@code false}: 写入失败
*/
public static boolean writeFileFromBytesByMap(final File file, final byte[] bytes, final boolean append, final boolean isForce) {
if (bytes == null || !createOrExistsFile(file)) return false;
FileChannel fc = null;
try {
fc = new FileOutputStream(file, append).getChannel();
MappedByteBuffer mbb = fc.map(FileChannel.MapMode.READ_WRITE, fc.size(), bytes.length);
mbb.put(bytes);
if (isForce) mbb.force();
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
CloseUtils.closeIO(fc);
}
}
示例6: getFileMD5String
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
public static String getFileMD5String(String path) {
try {
File file = new File(path);
FileInputStream in = new FileInputStream(file);
FileChannel ch = in.getChannel();
MappedByteBuffer byteBuffer = ch.map(FileChannel.MapMode.READ_ONLY, 0, file.length());
MessageDigest messageDigest = MessageDigest.getInstance("MD5");
messageDigest.update(byteBuffer);
return bufferToHex(messageDigest.digest());
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
示例7: readFile
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
/**
* Read a file to string
*
* @author erickson
* @link http://stackoverflow.com/a/326440
*
* @param file
* @return
* @throws IOException
*/
public static String readFile(final File file) throws IOException {
final FileInputStream stream = new FileInputStream(file);
try {
final FileChannel fc = stream.getChannel();
final ByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
return Charset.forName(ENCODING).decode(bb).toString();
} finally {
stream.close();
}
}
示例8: mapFileIn
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
/**
* Converts the file into a byte[].
* @param infile The File you want to specify
* @return a byte array
* @throws IOException if something goes wrong reading the file.
*/
private byte[] mapFileIn(File infile) throws IOException{
FileInputStream fis = new FileInputStream(infile);
try{
FileChannel fc = fis.getChannel(); // Get the file's size and then map it into memory
int sz = (int)fc.size();
MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, sz);
byte[] data2 = new byte[bb.remaining()];
bb.get(data2);
return data2;
}
finally{//Ensures resources are closed regardless of whether the action suceeded
fis.close();
}
}
示例9: CreateMappedBytes
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
private void CreateMappedBytes(FileChannel InChannel) throws IOException {
try {
if (_IPv4Buffer == null) {
final long _IPv4Bytes = (long)_IPv4ColumnSize * (long)_DBCount;
_IPv4Offset = _BaseAddr - 1;
_IPv4Buffer = InChannel.map(FileChannel.MapMode.READ_ONLY, _IPv4Offset, _IPv4Bytes);
_IPv4Buffer.order(ByteOrder.LITTLE_ENDIAN);
_MapDataOffset = _IPv4Offset + _IPv4Bytes;
}
if (_DBCountIPv6 > 0 && _IPv6Buffer == null) {
final long _IPv6Bytes = (long)_IPv6ColumnSize * (long)_DBCountIPv6;
_IPv6Offset = _BaseAddrIPv6 - 1;
_IPv6Buffer = InChannel.map(FileChannel.MapMode.READ_ONLY, _IPv6Offset, _IPv6Bytes);
_IPv6Buffer.order(ByteOrder.LITTLE_ENDIAN);
_MapDataOffset = _IPv6Offset + _IPv6Bytes;
}
if (_MapDataBuffer == null) {
_MapDataBuffer = InChannel.map(FileChannel.MapMode.READ_ONLY, _MapDataOffset, InChannel.size() - _MapDataOffset);
_MapDataBuffer.order(ByteOrder.LITTLE_ENDIAN);
}
}
catch (IOException Ex) {
throw Ex;
}
}
示例10: nioByteBuffer
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
@Override
public ByteBuffer nioByteBuffer() throws IOException {
FileChannel channel = null;
try {
channel = new RandomAccessFile(file, "r").getChannel();
// Just copy the buffer if it's sufficiently small, as memory mapping has a high overhead.
if (length < conf.memoryMapBytes()) {
ByteBuffer buf = ByteBuffer.allocate((int) length);
channel.position(offset);
while (buf.remaining() != 0) {
if (channel.read(buf) == -1) {
throw new IOException(String.format("Reached EOF before filling buffer\n" +
"offset=%s\nfile=%s\nbuf.remaining=%s",
offset, file.getAbsoluteFile(), buf.remaining()));
}
}
buf.flip();
return buf;
} else {
return channel.map(FileChannel.MapMode.READ_ONLY, offset, length);
}
} catch (IOException e) {
try {
if (channel != null) {
long size = channel.size();
throw new IOException("Error in reading " + this + " (actual file length " + size + ")",
e);
}
} catch (IOException ignored) {
// ignore
}
throw new IOException("Error in opening " + this, e);
} finally {
JavaUtils.closeQuietly(channel);
}
}
示例11: map
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
private static MappedByteBuffer map(RandomAccessFile raf, MapMode mode, long size)
throws IOException {
Closer closer = Closer.create();
try {
FileChannel channel = closer.register(raf.getChannel());
return channel.map(mode, 0, size);
} catch (Throwable e) {
throw closer.rethrow(e);
} finally {
closer.close();
}
}
示例12: updateFunctionCode
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
private FunctionEntry updateFunctionCode(final String functionName, final FileChannel fileChannel) throws IOException {
final MappedByteBuffer buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileChannel.size());
buffer.load();
final UpdateFunctionCodeRequest request = new UpdateFunctionCodeRequest()
.withFunctionName(functionName)
.withZipFile(buffer);
final UpdateFunctionCodeResult result = awsLambdaClient.updateFunctionCode(request);
return new FunctionEntry(result);
}
示例13: TrieDictionary
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
private TrieDictionary(File data) throws IOException {
this.array = null;
FileInputStream ins = new FileInputStream(data);
FileChannel channel = ins.getChannel();
try {
this.buffer = channel.map(MapMode.READ_ONLY, 0, channel.size());
} finally {
channel.close();
ins.close();
}
}
示例14: HprofMappedByteBuffer
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
HprofMappedByteBuffer(File dumpFile) throws IOException {
FileInputStream fis = new FileInputStream(dumpFile);
FileChannel channel = fis.getChannel();
length = channel.size();
dumpBuffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, length);
channel.close();
readHeader();
}
示例15: copy
import java.nio.channels.FileChannel; //导入方法依赖的package包/类
public static void copy(File srcPath, File dstPath) throws IOException {
if (srcPath.isDirectory()) {
if (!dstPath.exists()) {
boolean result = dstPath.mkdir();
if (!result) {
throw new IOException("Unable to create directoy: " + dstPath);
}
}
String[] files = srcPath.list();
for (String file : files) {
copy(new File(srcPath, file), new File(dstPath, file));
}
} else {
if (srcPath.exists()) {
FileChannel in = null;
FileChannel out = null;
try (FileInputStream fis = new FileInputStream(srcPath);
FileOutputStream fos = new FileOutputStream(dstPath)) {
in = fis.getChannel();
out = fos.getChannel();
long size = in.size();
MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size);
out.write(buf);
} finally {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
}
}
}
}