List of usage examples for java.io FileInputStream getChannel
public FileChannel getChannel()
From source file:pyromaniac.IO.MMFastaImporter.java
/** * _init seq./* w w w . ja v a 2s .co m*/ * * @throws Exception the exception */ private void _initSeq() throws Exception { FileInputStream tempStream = new FileInputStream(new File(this.seqFile)); FileChannel fcSeq = tempStream.getChannel(); this.seqSizeLong = fcSeq.size(); this.seqStartsLL = new ArrayList<Pair<Integer, Long>>(); for (long startPosition = 0L; startPosition < this.seqSizeLong; startPosition += HALF_GIGA) { MappedByteBuffer seqBuffer = fcSeq.map(FileChannel.MapMode.READ_ONLY, startPosition, Math.min(this.seqSizeLong - startPosition, HALF_GIGA)); this.seqBuffers.add(seqBuffer); int sbf_pos = seqBuffers.size() - 1; int maxBuffer = 2048; int bufferSize = (seqBuffer.capacity() > maxBuffer) ? maxBuffer : seqBuffer.capacity(); seqBuffer.limit(bufferSize); seqBuffer.position(0); while (seqBuffer.position() != seqBuffer.capacity()) { int prevPos = seqBuffer.position(); CharBuffer result = decoder.decode(seqBuffer); seqBuffer.position(prevPos); for (int i = 0; i < result.capacity(); i++) { char curr = result.charAt(i); int posInFile = prevPos + i; if (curr == BEGINNING_FASTA_HEADER) { seqStartsLL.add(new Pair<Integer, Long>(sbf_pos, new Long(posInFile))); } } int newPos = seqBuffer.limit(); if (seqBuffer.limit() + bufferSize > seqBuffer.capacity()) seqBuffer.limit(seqBuffer.capacity()); else seqBuffer.limit(seqBuffer.limit() + bufferSize); seqBuffer.position(newPos); } seqBuffer.rewind(); } }
From source file:pyromaniac.IO.MMFastaImporter.java
/** * _init qual.//w w w.j av a 2 s. c o m * * @throws Exception the exception */ private void _initQual() throws Exception { FileInputStream tempStream = new FileInputStream(new File(this.qualFile)); FileChannel fcQual = tempStream.getChannel(); this.qualSizeLong = fcQual.size(); //qual starts LL contains pairs, marking file #no (in qualBuffers) and position #no (in the buffer). this.qualStartsLL = new ArrayList<Pair<Integer, Long>>(); for (long startPosition = 0L; startPosition < this.qualSizeLong; startPosition += HALF_GIGA) { MappedByteBuffer qualBuffer = fcQual.map(FileChannel.MapMode.READ_ONLY, startPosition, Math.min(this.qualSizeLong - startPosition, HALF_GIGA)); //map half a gig to this channel. this.qualBuffers.add(qualBuffer); int qbf_pos = qualBuffers.size() - 1; int maxBuffer = 2048; int bufferSize = (qualBuffer.capacity() > maxBuffer) ? maxBuffer : qualBuffer.capacity(); qualBuffer.limit(bufferSize); qualBuffer.position(0); while (qualBuffer.position() != qualBuffer.capacity()) { int prevPos = qualBuffer.position(); CharBuffer result = decoder.decode(qualBuffer); qualBuffer.position(prevPos); for (int i = 0; i < result.capacity(); i++) { char curr = result.charAt(i); int posInFile = prevPos + i; if (curr == BEGINNING_FASTA_HEADER) { qualStartsLL.add(new Pair<Integer, Long>(qbf_pos, new Long(posInFile))); } } int newPos = qualBuffer.limit(); if (qualBuffer.limit() + bufferSize > qualBuffer.capacity()) qualBuffer.limit(qualBuffer.capacity()); else qualBuffer.limit(qualBuffer.limit() + bufferSize); qualBuffer.position(newPos); } qualBuffer.rewind(); } }
From source file:filesrv.Data.java
public void writeData(ClientHandler handler) throws Exception { if (wrapedByteBuffer != null && wrapedByteBuffer.remaining() != 0) { logger.finest(nonBlockingWriteDesc); int written = handler.getSocketChannel().write(wrapedByteBuffer); logger.finest("Written " + written + " Bytes"); if (wrapedByteBuffer.remaining() != 0) { handler.registerForWrite();//from ww w . j a va2 s. c o m return; } wroteFileHttpHeader = true; if (sendFile == false) { wrapedByteBuffer = null; if (closeConWhenDone) { handler.closeConnection(); } return; } } if (sendFile == true) { if (fileChannel == null) { File file = getFile(); logger.finest("Sending file data: " + file); FileInputStream fin = new FileInputStream(file); fileChannel = fin.getChannel(); fileChannel.position(startRange); } if (pooledByteBuffer == null) { pooledByteBuffer = (ByteBuffer) handler.getServer().getByteBufferPool().borrowObject(); } if (pooledByteBuffer.hasRemaining()) { int ret = -1; long remain = fileLength - startRange; logger.finest("Remain: " + remain); if (pooledByteBuffer.remaining() > remain) { pooledByteBuffer.limit((int) (pooledByteBuffer.position() + remain)); } ret = fileChannel.read(pooledByteBuffer); logger.finest("Read " + ret + " Bytes from file"); if (ret < 0 || remain == 0) {//EOF fileChannel.close(); //fileChannel = null; } else { startRange += ret; } } pooledByteBuffer.flip(); if (pooledByteBuffer.hasRemaining()) { int written = handler.getSocketChannel().write(pooledByteBuffer); logger.finest("Written " + written + " Bytes to socket"); } long remain = fileLength - startRange; if (remain == 0 && pooledByteBuffer.hasRemaining() == false) { fileChannel.close(); } if (pooledByteBuffer.hasRemaining() == false && fileChannel.isOpen() == false) { sendFile = false; logger.finest("Sent the file!"); if (false) { handler.closeConnection(); } else { //so that we can take more req. clean(); } return; //work done } pooledByteBuffer.compact(); //In case of partial write handler.registerForWrite(); } //end of sendFile }
From source file:net.librec.data.convertor.TextDataConvertor.java
/** * Read data from the data file. Note that we didn't take care of the * duplicated lines./*w w w. ja v a2 s .co m*/ * * @param dataColumnFormat * the format of input data file * @param inputDataPath * the path of input data file * @param binThold * the threshold to binarize a rating. If a rating is greater * than the threshold, the value will be 1; otherwise 0. To * disable this appender, i.e., keep the original rating value, * set the threshold a negative value * @throws IOException * if the <code>inputDataPath</code> is not valid. */ private void readData(String dataColumnFormat, String inputDataPath, double binThold) throws IOException { LOG.info(String.format("Dataset: %s", StringUtil.last(inputDataPath, 38))); // Table {row-id, col-id, rate} Table<Integer, Integer, Double> dataTable = HashBasedTable.create(); // Table {row-id, col-id, timestamp} Table<Integer, Integer, Long> timeTable = null; // Map {col-id, multiple row-id}: used to fast build a rating matrix Multimap<Integer, Integer> colMap = HashMultimap.create(); // BiMap {raw id, inner id} userIds, itemIds if (this.userIds == null) { this.userIds = HashBiMap.create(); } if (this.itemIds == null) { this.itemIds = HashBiMap.create(); } final List<File> files = new ArrayList<File>(); final ArrayList<Long> fileSizeList = new ArrayList<Long>(); SimpleFileVisitor<Path> finder = new SimpleFileVisitor<Path>() { @Override public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException { fileSizeList.add(file.toFile().length()); files.add(file.toFile()); return super.visitFile(file, attrs); } }; Files.walkFileTree(Paths.get(inputDataPath), finder); LOG.info("All dataset files " + files.toString()); long allFileSize = 0; for (Long everyFileSize : fileSizeList) { allFileSize = allFileSize + everyFileSize.longValue(); } LOG.info("All dataset files size " + Long.toString(allFileSize)); int readingFileCount = 0; long loadAllFileByte = 0; // loop every dataFile collecting from walkFileTree for (File dataFile : files) { LOG.info("Now loading dataset file " + dataFile.toString().substring( dataFile.toString().lastIndexOf(File.separator) + 1, dataFile.toString().lastIndexOf("."))); readingFileCount += 1; loadFilePathRate = readingFileCount / (float) files.size(); long readingOneFileByte = 0; FileInputStream fis = new FileInputStream(dataFile); FileChannel fileRead = fis.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(BSIZE); int len; String bufferLine = new String(); byte[] bytes = new byte[BSIZE]; while ((len = fileRead.read(buffer)) != -1) { readingOneFileByte += len; loadDataFileRate = readingOneFileByte / (float) fileRead.size(); loadAllFileByte += len; loadAllFileRate = loadAllFileByte / (float) allFileSize; buffer.flip(); buffer.get(bytes, 0, len); bufferLine = bufferLine.concat(new String(bytes, 0, len)); bufferLine = bufferLine.replaceAll("\r", "\n"); String[] bufferData = bufferLine.split("(\n)+"); boolean isComplete = bufferLine.endsWith("\n"); int loopLength = isComplete ? bufferData.length : bufferData.length - 1; for (int i = 0; i < loopLength; i++) { String line = new String(bufferData[i]); String[] data = line.trim().split("[ \t,]+"); String user = data[0]; String item = data[1]; Double rate = ((dataColumnFormat.equals("UIR") || dataColumnFormat.equals("UIRT")) && data.length >= 3) ? Double.valueOf(data[2]) : 1.0; // binarize the rating for item recommendation task if (binThold >= 0) { rate = rate > binThold ? 1.0 : 0.0; } // inner id starting from 0 int row = userIds.containsKey(user) ? userIds.get(user) : userIds.size(); userIds.put(user, row); int col = itemIds.containsKey(item) ? itemIds.get(item) : itemIds.size(); itemIds.put(item, col); dataTable.put(row, col, rate); colMap.put(col, row); // record rating's issuing time if (StringUtils.equals(dataColumnFormat, "UIRT") && data.length >= 4) { if (timeTable == null) { timeTable = HashBasedTable.create(); } // convert to million-seconds long mms = 0L; try { mms = Long.parseLong(data[3]); // cannot format // 9.7323480e+008 } catch (NumberFormatException e) { mms = (long) Double.parseDouble(data[3]); } long timestamp = timeUnit.toMillis(mms); timeTable.put(row, col, timestamp); } } if (!isComplete) { bufferLine = bufferData[bufferData.length - 1]; } buffer.clear(); } fileRead.close(); fis.close(); } int numRows = numUsers(), numCols = numItems(); // build rating matrix preferenceMatrix = new SparseMatrix(numRows, numCols, dataTable, colMap); if (timeTable != null) datetimeMatrix = new SparseMatrix(numRows, numCols, timeTable, colMap); // release memory of data table dataTable = null; timeTable = null; }
From source file:esg.node.components.monitoring.MonitorDAO.java
private void loadDiskInfoResource() { disk_info_dsroot_pat = Pattern.compile("thredds_dataset_roots\\s*=\\s*(.*?)\\s*\\w+\\s*?="); disk_info_dsroot_keyval_pat = Pattern.compile("\\s*(\\w+)\\s*\\|\\s*(\\S+)\\s*"); String filename = props.getProperty("monitor.esg.ini", System.getenv().get("ESG_USER_HOME") + "/.esgcet/esg.ini"); File iniFile = new File(filename); if (!iniFile.exists()) { log.warn("ESG publisher config file [" + filename + "] not found! Cannot provide disk info!"); return;/* w w w. j av a2 s . c o m*/ } log.debug("Scanning for drives specified in: " + filename); try { FileInputStream fis = new FileInputStream(iniFile); FileChannel fc = fis.getChannel(); disk_info_byte_buffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, (int) fc.size()); } catch (FileNotFoundException e) { log.error(e); } catch (IOException e) { log.error(e); } }
From source file:com.wwpass.connection.WWPassConnection.java
private static PKCS8EncodedKeySpec readKeyFile(String path) throws IOException { FileInputStream stream = new FileInputStream(new File(path)); try {//from www.j a v a 2s. c o m FileChannel fc = stream.getChannel(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); String pem = Charset.defaultCharset().decode(bb).toString(); pem = pem.replaceFirst("-----BEGIN (RSA )?PRIVATE KEY-----\r?\n?", "") .replace("-----END (RSA )?PRIVATE KEY-----", ""); Base64 dec1 = new Base64(); byte[] encoded = dec1.decode(pem); return new PKCS8EncodedKeySpec(encoded); } finally { stream.close(); } }
From source file:edu.harvard.iq.dvn.core.analysis.NetworkDataServiceBean.java
private void copyFile(StudyFileEditBean editBean) throws IOException { File tempFile = new File(editBean.getTempSystemFileLocation()); dbgLog.fine("begin copyFile()"); // create a sub-directory "ingested" File newDir = new File(tempFile.getParentFile(), "ingested"); if (!newDir.exists()) { newDir.mkdirs();/* w w w. jav a2 s . c o m*/ } dbgLog.fine("newDir: abs path:\n" + newDir.getAbsolutePath()); File newFile = new File(newDir, tempFile.getName()); FileInputStream fis = new FileInputStream(tempFile); FileOutputStream fos = new FileOutputStream(newFile); FileChannel fcin = fis.getChannel(); FileChannel fcout = fos.getChannel(); fcin.transferTo(0, fcin.size(), fcout); fcin.close(); fcout.close(); fis.close(); fos.close(); dbgLog.fine("newFile: abs path:\n" + newFile.getAbsolutePath()); // store the tab-file location editBean.setIngestedSystemFileLocation(newFile.getAbsolutePath()); }
From source file:com.android.mms.transaction.NotificationTransaction.java
public int checkPduResult() { if (!mPduFile.exists()) { Log.e(MmsApp.TXN_TAG, "checkPduResult MMS Fail, no pduFile = " + mPduFile); return SmsManager.MMS_ERROR_UNSPECIFIED; }// w w w . ja v a 2s. co m FileChannel channel = null; FileInputStream fs = null; RetrieveConf retrieveConf; try { fs = new FileInputStream(mPduFile); channel = fs.getChannel(); ByteBuffer byteBuffer = ByteBuffer.allocate((int) channel.size()); while ((channel.read(byteBuffer)) > 0) { // do nothing // System.out.println("reading"); } final GenericPdu pdu = (new PduParser(byteBuffer.array(), PduParserUtil.shouldParseContentDisposition(mSubId))).parse(); if (pdu == null || !(pdu instanceof RetrieveConf)) { Log.e(MmsApp.TXN_TAG, "checkPduResult: invalid parsed PDU"); return SmsManager.MMS_ERROR_UNSPECIFIED; } retrieveConf = (RetrieveConf) pdu; // Store the downloaded message PduPersister persister = PduPersister.getPduPersister(mContext); Uri messageUri = persister.persist(pdu, Telephony.Mms.Inbox.CONTENT_URI, true/*createThreadId*/, true/*groupMmsEnabled*/, null/*preOpenedFiles*/); if (messageUri == null) { Log.e(MmsApp.TXN_TAG, "checkPduResult: can not persist message"); return SmsManager.MMS_ERROR_UNSPECIFIED; } mMessageUri = messageUri.toString(); // Update some of the properties of the message final ContentValues values = new ContentValues(); values.put(Telephony.Mms.DATE, System.currentTimeMillis() / 1000L); values.put(Telephony.Mms.READ, 0); values.put(Telephony.Mms.SEEN, 0); String creator = ActivityThread.currentPackageName(); if (!TextUtils.isEmpty(creator)) { values.put(Telephony.Mms.CREATOR, creator); } values.put(Telephony.Mms.SUBSCRIPTION_ID, mSubId); if (SqliteWrapper.update(mContext, mContext.getContentResolver(), messageUri, values, null/*where*/, null/*selectionArg*/) != 1) { Log.e(MmsApp.TXN_TAG, "persistIfRequired: can not update message"); } // Delete the corresponding NotificationInd SqliteWrapper.delete(mContext, mContext.getContentResolver(), Telephony.Mms.CONTENT_URI, LOCATION_SELECTION, new String[] { Integer.toString(PduHeaders.MESSAGE_TYPE_NOTIFICATION_IND), mContentLocation }); return Activity.RESULT_OK; } catch (IOException e) { e.printStackTrace(); return SmsManager.MMS_ERROR_UNSPECIFIED; } catch (MmsException e) { e.printStackTrace(); return SmsManager.MMS_ERROR_UNSPECIFIED; } catch (SQLiteException e) { e.printStackTrace(); return SmsManager.MMS_ERROR_UNSPECIFIED; } catch (RuntimeException e) { e.printStackTrace(); return SmsManager.MMS_ERROR_UNSPECIFIED; } finally { if (mPduFile != null) { mPduFile.delete(); } try { if (channel != null) { channel.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (fs != null) { fs.close(); } } catch (IOException e) { e.printStackTrace(); } } }
From source file:dk.netarkivet.common.utils.FileUtils.java
/** * Copy file from one location to another. Will silently overwrite an * already existing file.//from w w w . ja va2 s. co m * * @param from * original to copy * @param to * destination of copy * @throws IOFailure if an io error occurs while copying file, * or the original file does not exist. */ public static void copyFile(File from, File to) { ArgumentNotValid.checkNotNull(from, "File from"); ArgumentNotValid.checkNotNull(to, "File to"); if (!from.exists()) { String errMsg = "Original file '" + from.getAbsolutePath() + "' does not exist"; log.warn(errMsg); throw new IOFailure(errMsg); } try { FileInputStream inStream = null; FileOutputStream outStream = null; FileChannel in = null; FileChannel out = null; try { inStream = new FileInputStream(from); outStream = new FileOutputStream(to); in = inStream.getChannel(); out = outStream.getChannel(); long bytesTransferred = 0; do { //Note: in.size() is called every loop, because if it should //change size, we might end up in an infinite loop trying to //copy more bytes than are actually available. bytesTransferred += in.transferTo(bytesTransferred, Math.min(Constants.IO_CHUNK_SIZE, in.size() - bytesTransferred), out); } while (bytesTransferred < in.size()); } finally { if (inStream != null) { inStream.close(); } if (outStream != null) { outStream.close(); } if (in != null) { in.close(); } if (out != null) { out.close(); } } } catch (IOException e) { final String errMsg = "Error copying file '" + from.getAbsolutePath() + "' to '" + to.getAbsolutePath() + "'"; log.warn(errMsg, e); throw new IOFailure(errMsg, e); } }
From source file:org.apache.hadoop.hdfs.client.ShortCircuitReplica.java
public ShortCircuitReplica(ExtendedBlockId key, FileInputStream dataStream, FileInputStream metaStream, ShortCircuitCache cache, long creationTimeMs, Slot slot) throws IOException { this.key = key; this.dataStream = dataStream; this.metaStream = metaStream; this.metaHeader = BlockMetadataHeader.preadHeader(metaStream.getChannel()); if (metaHeader.getVersion() != 1) { throw new IOException( "invalid metadata header version " + metaHeader.getVersion() + ". Can only handle version 1."); }/* w w w . j a v a 2s . c o m*/ this.cache = cache; this.creationTimeMs = creationTimeMs; this.slot = slot; }