List of usage examples for java.nio.channels FileChannel read
public final long read(ByteBuffer[] dsts) throws IOException
From source file:com.l2jfree.loginserver.L2LoginIdentifier.java
private synchronized void load() { if (isLoaded()) return;//from w w w . j av a 2 s .com File f = new File(System.getProperty("user.home", null), FILENAME); ByteBuffer bb = ByteBuffer.allocateDirect(8); if (!f.exists() || f.length() != 8) { _uid = getRandomUID(); _loaded = true; _log.info("A new UID has been generated for this login server."); FileOutputStream fos = null; try { f.createNewFile(); fos = new FileOutputStream(f); FileChannel fc = fos.getChannel(); bb.putLong(getUID()); bb.flip(); fc.write(bb); fos.flush(); } catch (IOException e) { _log.warn("Could not store login server's UID!", e); } finally { IOUtils.closeQuietly(fos); f.setReadOnly(); } } else { FileInputStream fis = null; try { fis = new FileInputStream(f); FileChannel fc = fis.getChannel(); fc.read(bb); } catch (IOException e) { _log.warn("Could not read stored login server's UID!", e); } finally { IOUtils.closeQuietly(fis); } if (bb.position() > 0) { bb.flip(); _uid = bb.getLong(); } else _uid = getRandomUID(); _loaded = true; } }
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./*from ww w .j ava2 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: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; }// www . ja v a 2 s. c o 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:tachyon.command.TFsShell.java
private int copyPath(File src, TachyonFS tachyonClient, String dstPath) throws IOException { if (!src.isDirectory()) { int fileId = tachyonClient.createFile(dstPath); if (fileId == -1) { return -1; }//from w ww .j a v a2 s . c om TachyonFile tFile = tachyonClient.getFile(fileId); OutStream os = tFile.getOutStream(WriteType.CACHE_THROUGH); FileInputStream in = new FileInputStream(src); FileChannel channel = in.getChannel(); ByteBuffer buf = ByteBuffer.allocate(Constants.KB); while (channel.read(buf) != -1) { buf.flip(); os.write(buf.array(), 0, buf.limit()); } os.close(); channel.close(); in.close(); return 0; } else { tachyonClient.mkdir(dstPath); for (String file : src.list()) { String newPath = FilenameUtils.concat(dstPath, file); File srcFile = new File(src, file); if (copyPath(srcFile, tachyonClient, newPath) == -1) { return -1; } } } return 0; }
From source file:voldemort.store.cachestore.impl.ChannelStore.java
private boolean checkSignature(FileChannel channel) throws IOException { ByteBuffer intBytes = ByteBuffer.allocate(OFFSET); if (channel.size() == 0) { intBytes.putInt(MAGIC);/* w w w . j a va 2 s . co m*/ intBytes.flip(); channel.write(intBytes); } else { channel.read(intBytes); intBytes.rewind(); int s = intBytes.getInt(); if (s != MAGIC) throw new StoreException( "Header mismatch expect " + Integer.toHexString(MAGIC) + " read " + Integer.toHexString(s)); } return true; }
From source file:org.jtransforms.fft.DoubleFFT_1DTest.java
/** * Read the binary reference data files generated with FFTW. The structure * of these files is very simple: double values are written linearly (little * endian)./*from w ww . jav a 2 s . c om*/ * * @param name * the file name * @param data * the array to be updated with the data read (the size of this * array gives the number of <code>double</code> to be retrieved */ public void readData(final String name, final double[] data) { try { final File f = new File(getClass().getClassLoader().getResource(name).getFile()); final FileInputStream fin = new FileInputStream(f); final FileChannel fc = fin.getChannel(); final ByteBuffer buffer = ByteBuffer.allocate(8 * data.length); buffer.order(ByteOrder.LITTLE_ENDIAN); fc.read(buffer); for (int i = 0; i < data.length; i++) { data[i] = buffer.getDouble(8 * i); } } catch (IOException e) { Assert.fail(e.getMessage()); } }
From source file:org.jtransforms.fft.DoubleFFT_1DTest.java
/** * Read the binary reference data files generated with FFTW. The structure * of these files is very simple: double values are written linearly (little * endian).// ww w. j ava2s. c o m * * @param name * the file name * @param data * the array to be updated with the data read (the size of this * array gives the number of <code>double</code> to be retrieved */ public void readData(final String name, final DoubleLargeArray data) { try { final File f = new File(getClass().getClassLoader().getResource(name).getFile()); final FileInputStream fin = new FileInputStream(f); final FileChannel fc = fin.getChannel(); final ByteBuffer buffer = ByteBuffer.allocate(8 * (int) data.length()); buffer.order(ByteOrder.LITTLE_ENDIAN); fc.read(buffer); for (int i = 0; i < data.length(); i++) { data.setDouble(i, buffer.getDouble(8 * i)); } } catch (IOException e) { Assert.fail(e.getMessage()); } }
From source file:alluxio.shell.command.CpCommand.java
/** * Copies a file or directory specified by srcPath from the local filesystem to dstPath in the * Alluxio filesystem space./*from ww w. j ava 2 s .co m*/ * * @param srcPath the {@link AlluxioURI} of the source file in the local filesystem * @param dstPath the {@link AlluxioURI} of the destination * @throws AlluxioException when Alluxio exception occurs * @throws IOException when non-Alluxio exception occurs */ private void copyPath(AlluxioURI srcPath, AlluxioURI dstPath) throws AlluxioException, IOException { File src = new File(srcPath.getPath()); if (!src.isDirectory()) { // If the dstPath is a directory, then it should be updated to be the path of the file where // src will be copied to. if (mFileSystem.exists(dstPath) && mFileSystem.getStatus(dstPath).isFolder()) { dstPath = dstPath.join(src.getName()); } FileOutStream os = null; try (Closer closer = Closer.create()) { os = closer.register(mFileSystem.createFile(dstPath)); FileInputStream in = closer.register(new FileInputStream(src)); FileChannel channel = closer.register(in.getChannel()); ByteBuffer buf = ByteBuffer.allocate(8 * Constants.MB); while (channel.read(buf) != -1) { buf.flip(); os.write(buf.array(), 0, buf.limit()); } } catch (Exception e) { // Close the out stream and delete the file, so we don't have an incomplete file lying // around. if (os != null) { os.cancel(); if (mFileSystem.exists(dstPath)) { mFileSystem.delete(dstPath); } } throw e; } } else { mFileSystem.createDirectory(dstPath); List<String> errorMessages = new ArrayList<>(); File[] fileList = src.listFiles(); if (fileList == null) { String errMsg = String.format("Failed to list files for directory %s", src); errorMessages.add(errMsg); fileList = new File[0]; } int misFiles = 0; for (File srcFile : fileList) { AlluxioURI newURI = new AlluxioURI(dstPath, new AlluxioURI(srcFile.getName())); try { copyPath(new AlluxioURI(srcPath.getScheme(), srcPath.getAuthority(), srcFile.getPath()), newURI); } catch (IOException e) { errorMessages.add(e.getMessage()); if (!mFileSystem.exists(newURI)) { misFiles++; } } } if (errorMessages.size() != 0) { if (misFiles == fileList.length) { // If the directory doesn't exist and no files were created, then delete the directory if (mFileSystem.exists(dstPath)) { mFileSystem.delete(dstPath); } } throw new IOException(Joiner.on('\n').join(errorMessages)); } } }
From source file:alluxio.cli.fs.command.CpCommand.java
/** * Copies a file or directory specified by srcPath from the local filesystem to dstPath in the * Alluxio filesystem space.//from w w w . j a v a 2 s .co m * * @param srcPath the {@link AlluxioURI} of the source file in the local filesystem * @param dstPath the {@link AlluxioURI} of the destination */ private void copyPath(AlluxioURI srcPath, AlluxioURI dstPath) throws AlluxioException, IOException { File src = new File(srcPath.getPath()); if (!src.isDirectory()) { // If the dstPath is a directory, then it should be updated to be the path of the file where // src will be copied to. if (mFileSystem.exists(dstPath) && mFileSystem.getStatus(dstPath).isFolder()) { dstPath = dstPath.join(src.getName()); } FileOutStream os = null; try (Closer closer = Closer.create()) { FileWriteLocationPolicy locationPolicy; locationPolicy = CommonUtils.createNewClassInstance( Configuration.<FileWriteLocationPolicy>getClass( PropertyKey.USER_FILE_COPY_FROM_LOCAL_WRITE_LOCATION_POLICY), new Class[] {}, new Object[] {}); os = closer.register(mFileSystem.createFile(dstPath, CreateFileOptions.defaults().setLocationPolicy(locationPolicy))); FileInputStream in = closer.register(new FileInputStream(src)); FileChannel channel = closer.register(in.getChannel()); ByteBuffer buf = ByteBuffer.allocate(8 * Constants.MB); while (channel.read(buf) != -1) { buf.flip(); os.write(buf.array(), 0, buf.limit()); } } catch (Exception e) { // Close the out stream and delete the file, so we don't have an incomplete file lying // around. if (os != null) { os.cancel(); if (mFileSystem.exists(dstPath)) { mFileSystem.delete(dstPath); } } throw e; } } else { mFileSystem.createDirectory(dstPath); List<String> errorMessages = new ArrayList<>(); File[] fileList = src.listFiles(); if (fileList == null) { String errMsg = String.format("Failed to list files for directory %s", src); errorMessages.add(errMsg); fileList = new File[0]; } int misFiles = 0; for (File srcFile : fileList) { AlluxioURI newURI = new AlluxioURI(dstPath, new AlluxioURI(srcFile.getName())); try { copyPath(new AlluxioURI(srcPath.getScheme(), srcPath.getAuthority(), srcFile.getPath()), newURI); } catch (IOException e) { errorMessages.add(e.getMessage()); if (!mFileSystem.exists(newURI)) { misFiles++; } } } if (errorMessages.size() != 0) { if (misFiles == fileList.length) { // If the directory doesn't exist and no files were created, then delete the directory if (mFileSystem.exists(dstPath)) { mFileSystem.delete(dstPath); } } throw new IOException(Joiner.on('\n').join(errorMessages)); } } }
From source file:com.android.mms.transaction.RetrieveTransaction.java
public int checkPduResult() { if (!mPduFile.exists()) { Log.e(MmsApp.TXN_TAG, "checkPduResult MMS Fail, no pduFile = " + mPduFile); return SmsManager.MMS_ERROR_UNSPECIFIED; }//w ww . j av a 2 s . com 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; byte[] messageId = retrieveConf.getMessageId(); MmsLog.d(MmsApp.TXN_TAG, "checkPduResult retrieveConf.messageId = " + new String(messageId)); // 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(); } } }