本文整理汇总了Java中java.io.FileInputStream.getChannel方法的典型用法代码示例。如果您正苦于以下问题:Java FileInputStream.getChannel方法的具体用法?Java FileInputStream.getChannel怎么用?Java FileInputStream.getChannel使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.io.FileInputStream
的用法示例。
在下文中一共展示了FileInputStream.getChannel方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: load
import java.io.FileInputStream; //导入方法依赖的package包/类
/**
* Load the block.
*
* mmap and mlock the block, and then verify its checksum.
*
* @param length The current length of the block.
* @param blockIn The block input stream. Should be positioned at the
* start. The caller must close this.
* @param metaIn The meta file input stream. Should be positioned at
* the start. The caller must close this.
* @param blockFileName The block file name, for logging purposes.
*
* @return The Mappable block.
*/
public static MappableBlock load(long length,
FileInputStream blockIn, FileInputStream metaIn,
String blockFileName) throws IOException {
MappableBlock mappableBlock = null;
MappedByteBuffer mmap = null;
FileChannel blockChannel = null;
try {
blockChannel = blockIn.getChannel();
if (blockChannel == null) {
throw new IOException("Block InputStream has no FileChannel.");
}
mmap = blockChannel.map(MapMode.READ_ONLY, 0, length);
NativeIO.POSIX.getCacheManipulator().mlock(blockFileName, mmap, length);
verifyChecksum(length, metaIn, blockChannel, blockFileName);
mappableBlock = new MappableBlock(mmap, length);
} finally {
IOUtils.closeQuietly(blockChannel);
if (mappableBlock == null) {
if (mmap != null) {
NativeIO.POSIX.munmap(mmap); // unmapping also unlocks
}
}
}
return mappableBlock;
}
示例2: copyFile
import java.io.FileInputStream; //导入方法依赖的package包/类
/**
* Creates the specified <code>toFile</code> as a byte for byte copy of the
* <code>fromFile</code>. If <code>toFile</code> already exists, then it
* will be replaced with a copy of <code>fromFile</code>. The name and path
* of <code>toFile</code> will be that of <code>toFile</code>.<br/>
* <br/>
* <i> Note: <code>fromFile</code> and <code>toFile</code> will be closed by
* this function.</i>
*
* @param fromFile
* - FileInputStream for the file to copy from.
* @param toFile
* - FileInputStream for the file to copy to.
*/
public static void copyFile(FileInputStream fromFile, FileOutputStream toFile) throws IOException {
FileChannel fromChannel = null;
FileChannel toChannel = null;
try {
fromChannel = fromFile.getChannel();
toChannel = toFile.getChannel();
fromChannel.transferTo(0, fromChannel.size(), toChannel);
} finally {
try {
if (fromChannel != null) {
fromChannel.close();
}
} finally {
if (toChannel != null) {
toChannel.close();
}
}
}
}
示例3: copyFile
import java.io.FileInputStream; //导入方法依赖的package包/类
/**
* Creates the specified <code>toFile</code> as a byte for byte copy of the
* <code>fromFile</code>. If <code>toFile</code> already exists, then it
* will be replaced with a copy of <code>fromFile</code>. The name and path
* of <code>toFile</code> will be that of <code>toFile</code>.<br/>
* <br/>
* <i> Note: <code>fromFile</code> and <code>toFile</code> will be closed by
* this function.</i>
*
* @param fromFile - FileInputStream for the file to copy from.
* @param toFile - FileOutpubStream for the file to copy to.
*/
public static void copyFile(FileInputStream fromFile, FileOutputStream toFile) throws IOException {
FileChannel fromChannel = null;
FileChannel toChannel = null;
try {
fromChannel = fromFile.getChannel();
toChannel = toFile.getChannel();
fromChannel.transferTo(0, fromChannel.size(), toChannel);
} finally {
try {
if (fromChannel != null) {
fromChannel.close();
}
} finally {
if (toChannel != null) {
toChannel.close();
}
}
}
}
示例4: init
import java.io.FileInputStream; //导入方法依赖的package包/类
private void init(FileInputStream is) {
fileInputStream=is;
fileChannel=is.getChannel();
try{
fileSize=fileChannel.size();
}catch(IOException e){
e.printStackTrace();
fileSize=0;
}
boolean openok=false;
// reader=new InputStreamReader(fileInputStream);
//
// try{
// readHeader();
// }catch(IOException e){
// log.warning("couldn't read header");
// }
size=fileSize/motionData.getLoggedObjectSize();
dataInputStream=new DataInputStream(Channels.newInputStream(fileChannel));
getSupport().firePropertyChange("position",0,position());
position(1);
}
示例5: transfer
import java.io.FileInputStream; //导入方法依赖的package包/类
static public long transfer(long position, long count, FileInputStream src, FileOutputStream dst) throws IOException {
FileChannel srcChannel = src.getChannel();
FileChannel dstChannel = dst.getChannel();
if (!srcChannel.isOpen()) {
throw new ClosedChannelException();
}
if (!dstChannel.isOpen()) {
throw new ClosedChannelException();
}
if (position < 0 || count < 0) {
throw new IllegalArgumentException("position=" + position + " count=" + count);
}
if (count == 0 || position >= srcChannel.size()) {
return 0;
}
count = Math.min(count, srcChannel.size() - position);
FileDescriptor inFd = src.getFD();
FileDescriptor outFd = dst.getFD();
long rc = 0;
rc = native_sendfile_64(outFd, inFd, position, count);
return rc;
}
示例6: getFileMD5String
import java.io.FileInputStream; //导入方法依赖的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: index
import java.io.FileInputStream; //导入方法依赖的package包/类
public void index(Map<String, Object> context) throws Exception {
long size;
String content;
String modified;
File file = LoggerFactory.getFile();
if (file != null && file.exists()) {
FileInputStream fis = new FileInputStream(file);
FileChannel channel = fis.getChannel();
size = channel.size();
ByteBuffer bb;
if (size <= SHOW_LOG_LENGTH) {
bb = ByteBuffer.allocate((int) size);
channel.read(bb, 0);
} else {
int pos = (int) (size - SHOW_LOG_LENGTH);
bb = ByteBuffer.allocate(SHOW_LOG_LENGTH);
channel.read(bb, pos);
}
bb.flip();
content = new String(bb.array()).replace("<", "<").replace(">", ">");
modified = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date(file.lastModified()));
} else {
size = 0;
content = "";
modified = "Not exist";
}
Level level = LoggerFactory.getLevel();
context.put("name", file == null ? "" : file.getAbsoluteFile());
context.put("size", String.valueOf(size));
context.put("level", level == null ? "" : level);
context.put("modified", modified);
context.put("content", content);
}
示例8: ResettableInputStream
import java.io.FileInputStream; //导入方法依赖的package包/类
/**
* @param file
* can be null if not known
*/
private ResettableInputStream(FileInputStream fis, File file) throws IOException {
super(fis);
this.file = file;
this.fis = fis;
this.fileChannel = fis.getChannel();
this.markPos = fileChannel.position();
}
示例9: copyFile
import java.io.FileInputStream; //导入方法依赖的package包/类
/**
* copy a file
*
* @param source source file
* @param dest destination file
* @return null on success; error message on failure
*/
private static String copyFile(Context ctx, File source, File dest) {
// if (ContextCompat.checkSelfPermission(ctx, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
if (!FileUtils.isFileSystemAccessGranted(ctx)) {
return mResources.getString(R.string.error_102);
}
FileChannel in;
FileChannel out;
try {
FileInputStream fileInputStream = new FileInputStream(source);
in = fileInputStream.getChannel();
FileOutputStream fileOutputStream = new FileOutputStream(dest);
out = fileOutputStream.getChannel();
long size = in.size();
MappedByteBuffer buf = in.map(FileChannel.MapMode.READ_ONLY, 0, size);
out.write(buf);
fileInputStream.close();
fileOutputStream.close();
in.close();
out.close();
}
catch (IOException e) {
// Toast toast = Toast.makeText(ctx, "Error: " + e.getMessage(), Toast.LENGTH_LONG);
// toast.show();
mLastException = e;
mLastErrorMessage = e.getMessage();
return e.getMessage();
}
return null;
}
示例10: getDataFileChannel
import java.io.FileInputStream; //导入方法依赖的package包/类
/**
* Returns {@code FileChennel} for the specified data file located in the
* {@code org.netbeans.modules.search.data} package.
* @param fileName - name of the data file.
* @return {@code FileChennel} of the data file.
*/
public FileChannel getDataFileChannel(String fileName) {
File file = getFile(fileName);
FileInputStream fis = getFileInputStream(file);
return fis.getChannel();
}
示例11: RgxReader
import java.io.FileInputStream; //导入方法依赖的package包/类
public RgxReader(String fileName, Charset charset, int _limit) throws FileNotFoundException {
this(CharBuffer.allocate(1024));
fs = new FileInputStream(fileName);
ReadableByteChannel channel = fs.getChannel();
CharsetDecoder decoder = charset.newDecoder();
reader = Channels.newReader(channel, decoder, -1);
buf.limit(0);
limit = _limit;
}
示例12: exportFileByNIO
import java.io.FileInputStream; //导入方法依赖的package包/类
/**
* @description 文件下载 nio 大缓存
* @param response
* @param file
* @throws IOException
*/
public static void exportFileByNIO(HttpServletResponse response, File file) throws IOException {
String filename = URLEncoder.encode(file.getName(), CharEncoding.UTF_8);
response.setContentType(HttpUtil.CONTENT_TYPE_APPLICATION_OCTET_STREAM);
response.setContentLength((int) file.length());
response.setHeader(HttpUtil.CONTENT_DISPOSITION, "attachment;filename=" + filename);
response.setHeader(HttpUtil.LOCATION, filename);
ServletOutputStream op = response.getOutputStream();
int bufferSize = 1024 * 128;
FileInputStream fileInputStream = new FileInputStream(file.getPath());
FileChannel fileChannel = fileInputStream.getChannel();
ByteBuffer bb = ByteBuffer.allocateDirect(1024 * 1024 * 128);
byte[] buffer = new byte[bufferSize];
int nRead, nGet;
while ((nRead = fileChannel.read(bb)) != -1) {
if (nRead == 0) {
continue;
}
bb.position(0);
bb.limit(nRead);
while (bb.hasRemaining()) {
nGet = Math.min(bb.remaining(), bufferSize);
bb.get(buffer, 0, nGet);
op.write(buffer);
}
bb.clear();
}
fileChannel.close();
fileInputStream.close();
}
示例13: getBufferFromUri
import java.io.FileInputStream; //导入方法依赖的package包/类
@SuppressWarnings("resource")
public ByteBuffer getBufferFromUri(String uri, Path containingFolder) throws IOException {
// 2.0以后只在外部存储bin文件
Path binPath = containingFolder.resolve(uri);
FileInputStream fis = new FileInputStream(binPath.toFile());
FileChannel channel = fis.getChannel();
ByteBuffer byteBuffer = ByteBuffer.allocate((int) channel.size());
channel.read(byteBuffer);
return byteBuffer;
}
示例14: openSampleData
import java.io.FileInputStream; //导入方法依赖的package包/类
static ObjectStream<POSSample> openSampleData(String sampleDataName, File sampleDataFile, Charset encoding) {
CmdLineUtil.checkInputFile(sampleDataName + " Data", sampleDataFile);
FileInputStream sampleDataIn = CmdLineUtil.openInFile(sampleDataFile);
ObjectStream<String> lineStream = new PlainTextByLineStream(sampleDataIn.getChannel(), encoding);
return new WordTagSampleStream(lineStream);
}
示例15: onCreate
import java.io.FileInputStream; //导入方法依赖的package包/类
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> arData = new ArrayList<String>();
mAdapter = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, arData);
ListView list = (ListView)findViewById(R.id.logList);
mAdapter.add("ssss");
list.setAdapter(mAdapter);
try {
FileInputStream fis = openFileInput("MousePos.txt");
FileChannel inChannel = fis.getChannel();
final int s = fis.available();
ByteBuffer buff = ByteBuffer.allocate((int)fis.available());
buff.clear();
inChannel.read(buff);
buff.rewind();
final int posSize = s/4;
float[] pos = new float[posSize];
buff.asFloatBuffer().get(pos);
inChannel.close();
for (int i=0; i < posSize; i+=2)
{
mAdapter.add("" + pos[i] + ", " + pos[i+1]);
}
fis.close();
} catch (Exception e) {
System.err.println(e.getMessage());
}
list.setOnTouchListener(this);
}