List of usage examples for java.nio.channels FileChannel size
public abstract long size() throws IOException;
From source file:org.carbondata.query.aggregator.impl.CustomAggregatorHelper.java
/** * Below method will be used to read the level files * * @param memberFile/*from w ww. ja v a2s .c o m*/ * @param fileName * @throws IOException */ private void readLevelFileAndUpdateCache(File memberFile, String fileName) throws IOException { FileInputStream fos = null; FileChannel fileChannel = null; try { // create an object of FileOutputStream fos = new FileInputStream(memberFile); fileChannel = fos.getChannel(); Map<Integer, String> memberMap = surrogateKeyMap.get(fileName); if (null == memberMap) { memberMap = new HashMap<Integer, String>(CarbonCommonConstants.DEFAULT_COLLECTION_SIZE); surrogateKeyMap.put(fileName, memberMap); } long size = fileChannel.size(); int maxKey = 0; ByteBuffer rowlengthToRead = null; int len = 0; ByteBuffer row = null; int toread = 0; byte[] bb = null; String value = null; int surrogateValue = 0; boolean enableEncoding = Boolean.valueOf( CarbonProperties.getInstance().getProperty(CarbonCommonConstants.ENABLE_BASE64_ENCODING, CarbonCommonConstants.ENABLE_BASE64_ENCODING_DEFAULT)); while (fileChannel.position() < size) { rowlengthToRead = ByteBuffer.allocate(4); fileChannel.read(rowlengthToRead); rowlengthToRead.rewind(); len = rowlengthToRead.getInt(); if (len == 0) { continue; } row = ByteBuffer.allocate(len); fileChannel.read(row); row.rewind(); toread = row.getInt(); bb = new byte[toread]; row.get(bb); if (enableEncoding) { value = new String(Base64.decodeBase64(bb), Charset.defaultCharset()); } else { value = new String(bb, Charset.defaultCharset()); } surrogateValue = row.getInt(); memberMap.put(surrogateValue, value); // check if max key is less than Surrogate key then update the max key if (maxKey < surrogateValue) { maxKey = surrogateValue; } } } finally { CarbonUtil.closeStreams(fileChannel, fos); } }
From source file:com.clustercontrol.agent.log.LogfileMonitor.java
/** * ?/*from w ww. j a va2 s. c o m*/ */ private boolean openFile() { m_log.info("openFile : filename=" + status.rsFilePath.getName()); closeFile(); FileChannel fc = null; // try { if (checkPrefix()) status.rotate(); fc = FileChannel.open(Paths.get(getFilePath()), StandardOpenOption.READ); long filesize = fc.size(); if (filesize > LogfileMonitorConfig.fileMaxSize) { // ???????? // message.log.agent.1={0}? // message.log.agent.3=?????? // message.log.agent.5={0} byte? String[] args1 = { getFilePath() }; String[] args2 = { String.valueOf(filesize) }; sendMessage(PriorityConstant.TYPE_INFO, MessageConstant.AGENT.getMessage(), MessageConstant.MESSAGE_LOG_FILE_SIZE_EXCEEDED_UPPER_BOUND.getMessage(), MessageConstant.MESSAGE_LOG_FILE.getMessage(args1) + ", " + MessageConstant.MESSAGE_LOG_FILE_SIZE_BYTE.getMessage(args2)); } // ?? // ?open?init=true?? // ??open?init=false??? fc.position(status.position); fileChannel = fc; return true; } catch (FileNotFoundException e) { m_log.info("openFile : " + e.getMessage()); if (m_initFlag) { // ???????? // message.log.agent.1={0}? // message.log.agent.2=??????? String[] args = { getFilePath() }; sendMessage(PriorityConstant.TYPE_INFO, MessageConstant.AGENT.getMessage(), MessageConstant.MESSAGE_LOG_FILE_NOT_FOUND.getMessage(), MessageConstant.MESSAGE_LOG_FILE.getMessage(args)); } return false; } catch (SecurityException e) { m_log.info("openFile : " + e.getMessage()); if (m_initFlag) { // ?? // message.log.agent.1={0}? // message.log.agent.4=???????? String[] args = { getFilePath() }; sendMessage(PriorityConstant.TYPE_WARNING, MessageConstant.AGENT.getMessage(), MessageConstant.MESSAGE_LOG_FAILED_TO_READ_FILE.getMessage(), MessageConstant.MESSAGE_LOG_FILE.getMessage(args) + "\n" + e.getMessage()); } return false; } catch (IOException e) { m_log.info("openFile : " + e.getMessage()); if (m_initFlag) { // ?? // message.log.agent.1={0}? // message.log.agent.4=???????? String[] args = { getFilePath() }; sendMessage(PriorityConstant.TYPE_INFO, MessageConstant.AGENT.getMessage(), MessageConstant.MESSAGE_LOG_FAILED_TO_READ_FILE.getMessage(), MessageConstant.MESSAGE_LOG_FILE.getMessage(args)); } return false; } finally { // ?????????????? if (fc != null && fileChannel == null) { try { fc.close(); } catch (IOException e) { m_log.warn(e.getMessage(), e); } } m_initFlag = false; } }
From source file:com.twinsoft.convertigo.beans.steps.WriteXMLStep.java
protected void writeFile(String filePath, NodeList nodeList) throws EngineException { if (nodeList == null) { throw new EngineException("Unable to write to xml file: element is Null"); }// w w w .jav a 2 s. c o m String fullPathName = getAbsoluteFilePath(filePath); synchronized (Engine.theApp.filePropertyManager.getMutex(fullPathName)) { try { String encoding = getEncoding(); encoding = encoding.length() > 0 && Charset.isSupported(encoding) ? encoding : "UTF-8"; if (!isReallyAppend(fullPathName)) { String tTag = defaultRootTagname.length() > 0 ? StringUtils.normalize(defaultRootTagname) : "document"; FileUtils.write(new File(fullPathName), "<?xml version=\"1.0\" encoding=\"" + encoding + "\"?>\n<" + tTag + "/>", encoding); } StringBuffer content = new StringBuffer(); /* do the content, only append child element */ for (int i = 0; i < nodeList.getLength(); i++) { if (nodeList.item(i).getNodeType() == Node.ELEMENT_NODE) { content.append(XMLUtils.prettyPrintElement((Element) nodeList.item(i), true, true)); } } /* detect current xml encoding */ RandomAccessFile randomAccessFile = null; try { randomAccessFile = new RandomAccessFile(fullPathName, "rw"); FileChannel fc = randomAccessFile.getChannel(); ByteBuffer buf = ByteBuffer.allocate(60); int nb = fc.read(buf); String sbuf = new String(buf.array(), 0, nb, "ASCII"); String enc = sbuf.replaceFirst("^.*encoding=\"", "").replaceFirst("\"[\\d\\D]*$", ""); if (!Charset.isSupported(enc)) { enc = encoding; } buf.clear(); /* retrieve last header tag*/ long pos = fc.size() - buf.capacity(); if (pos < 0) { pos = 0; } nb = fc.read(buf, pos); boolean isUTF8 = Charset.forName(enc) == Charset.forName("UTF-8"); if (isUTF8) { for (int i = 0; i < buf.capacity(); i++) { sbuf = new String(buf.array(), i, nb - i, enc); if (!sbuf.startsWith("")) { pos += i; break; } } } else { sbuf = new String(buf.array(), 0, nb, enc); } int lastTagIndex = sbuf.lastIndexOf("</"); if (lastTagIndex == -1) { int iend = sbuf.lastIndexOf("/>"); if (iend != -1) { lastTagIndex = sbuf.lastIndexOf("<", iend); String tagname = sbuf.substring(lastTagIndex + 1, iend); content = new StringBuffer( "<" + tagname + ">\n" + content.toString() + "</" + tagname + ">"); } else { throw new EngineException("Malformed XML file"); } } else { content.append(sbuf.substring(lastTagIndex)); if (isUTF8) { String before = sbuf.substring(0, lastTagIndex); lastTagIndex = before.getBytes(enc).length; } } fc.write(ByteBuffer.wrap(content.toString().getBytes(enc)), pos + lastTagIndex); } finally { if (randomAccessFile != null) { randomAccessFile.close(); } } } catch (IOException e) { throw new EngineException("Unable to write to xml file", e); } finally { Engine.theApp.filePropertyManager.releaseMutex(fullPathName); } } }
From source file:org.alfresco.repo.content.AbstractContentWriter.java
/** * {@inheritDoc}// ww w. ja v a 2 s . c om */ public FileChannel getFileChannel(boolean truncate) throws ContentIOException { /* * By calling this method, clients indicate that they wish to make random * changes to the file. It is possible that the client might only want * to update a tiny proportion of the file (truncate == false) or * start afresh (truncate == true). * * Where the underlying support is not present for this method, a temporary * file will be used as a substitute. When the write is complete, the * results are copied directly to the underlying channel. */ // get the underlying implementation's best writable channel channel = getWritableChannel(); // now use this channel if it can provide the random access, otherwise spoof it FileChannel clientFileChannel = null; if (channel instanceof FileChannel) { // all the support is provided by the underlying implementation clientFileChannel = (FileChannel) channel; // copy over the existing content, if required if (!truncate && existingContentReader != null) { ReadableByteChannel existingContentChannel = existingContentReader.getReadableChannel(); long existingContentLength = existingContentReader.getSize(); // copy the existing content try { clientFileChannel.transferFrom(existingContentChannel, 0, existingContentLength); // copy complete if (logger.isDebugEnabled()) { logger.debug("Copied content for random access: \n" + " writer: " + this + "\n" + " existing: " + existingContentReader); } } catch (IOException e) { throw new ContentIOException("Failed to copy from existing content to enable random access: \n" + " writer: " + this + "\n" + " existing: " + existingContentReader, e); } finally { try { existingContentChannel.close(); } catch (IOException e) { } } } // debug if (logger.isDebugEnabled()) { logger.debug("Content writer provided direct support for FileChannel: \n" + " writer: " + this); } } else { // No random access support is provided by the implementation. // Spoof it by providing a 2-stage write via a temp file File tempFile = TempFileProvider.createTempFile("random_write_spoof_", ".bin"); final FileContentWriter spoofWriter = new FileContentWriter(tempFile, // the file to write to getExistingContentReader()); // this ensures that the existing content is pulled in // Attach a listener // - to ensure that the content gets loaded from the temp file once writing has finished // - to ensure that the close call gets passed on to the underlying channel ContentStreamListener spoofListener = new ContentStreamListener() { public void contentStreamClosed() throws ContentIOException { // the spoofed temp channel has been closed, so get a new reader for it ContentReader spoofReader = spoofWriter.getReader(); FileChannel spoofChannel = spoofReader.getFileChannel(); // upload all the temp content to the real underlying channel try { long spoofFileSize = spoofChannel.size(); spoofChannel.transferTo(0, spoofFileSize, channel); } catch (IOException e) { throw new ContentIOException( "Failed to copy from spoofed temporary channel to permanent channel: \n" + " writer: " + this + "\n" + " temp: " + spoofReader, e); } finally { try { spoofChannel.close(); } catch (Throwable e) { } try { channel.close(); } catch (IOException e) { throw new ContentIOException("Failed to close underlying channel", e); } } } }; spoofWriter.addListener(spoofListener); // we now have the spoofed up channel that the client can work with clientFileChannel = spoofWriter.getFileChannel(truncate); // debug if (logger.isDebugEnabled()) { logger.debug("Content writer provided indirect support for FileChannel: \n" + " writer: " + this + "\n" + " temp writer: " + spoofWriter); } } // the file is now available for random access return clientFileChannel; }
From source file:org.wso2.appserver.integration.tests.config.EnvironmentVariableReadTestCase.java
public void applyConfiguration(File sourceFile, File targetFile) throws IOException { FileChannel source = null; FileChannel destination = null; if (!targetFile.exists() && !targetFile.createNewFile()) { throw new IOException("File " + targetFile + "creation fails"); }// ww w . j a v a 2 s . c om source = (new FileInputStream(sourceFile)).getChannel(); destination = (new FileOutputStream(targetFile)).getChannel(); destination.transferFrom(source, 0L, source.size()); if (source != null) { source.close(); } if (destination != null) { destination.close(); } }
From source file:de.suse.swamp.core.container.WorkflowManager.java
/** * Copy all files from "fromDir" to "toDir". * Will create the destination location if needed. * @throws Exception if source not existant or error occurs. *///from w w w . j a v a 2 s .c o m private void copyDirContent(String fromDir, String toDir) throws Exception { String fs = System.getProperty("file.separator"); File[] files = new File(fromDir).listFiles(); if (files == null) { throw new FileNotFoundException("Sourcepath: " + fromDir + " not found!"); } for (int i = 0; i < files.length; i++) { File dir = new File(toDir); dir.mkdirs(); if (files[i].isFile()) { try { // Create channel on the source FileChannel srcChannel = new FileInputStream(files[i]).getChannel(); // Create channel on the destination FileChannel dstChannel = new FileOutputStream(toDir + fs + files[i].getName()).getChannel(); dstChannel.transferFrom(srcChannel, 0, srcChannel.size()); srcChannel.close(); dstChannel.close(); } catch (Exception e) { Logger.ERROR("Error during file copy: " + e.getMessage()); throw e; } } } }
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./*ww w. ja v a 2 s.c o 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:automenta.knowtention.channel.LineFileChannel.java
@Override public void run() { FileInputStream fileInputStream = null; FileChannel channel = null; ByteBuffer buffer = null;//from ww w. j a va 2 s. c o m LinkedList<String> lines = new LinkedList(); StringBuilder builder = new StringBuilder(); long lastSize = -1, lastLastModified = -1; while (running) { try { Thread.sleep(delayPeriodMS); } catch (InterruptedException ex) { } lines.clear(); try { fileInputStream = new FileInputStream(file); channel = fileInputStream.getChannel(); long lastModified = file.lastModified(); long csize = channel.size(); if ((lastModified == lastLastModified) && (csize == lastSize)) { //also check file update time? fileInputStream.close(); continue; } int currentPos = (int) csize; buffer = channel.map(FileChannel.MapMode.READ_ONLY, 0, csize); buffer.position(currentPos); lastSize = csize; lastLastModified = lastModified; int count = 0; for (long i = csize - 1; i >= 0; i--) { char c = (char) buffer.get((int) i); if (c == '\n') { count++; builder.reverse(); lines.addFirst(builder.toString()); if (count == numLines) { break; } builder.setLength(0); } else builder.append(c); } update(lines); lines.clear(); buffer.clear(); channel.close(); fileInputStream.close(); fileInputStream = null; } catch (Exception ex) { Logger.getLogger(LineFileChannel.class.getName()).log(Level.SEVERE, null, ex); } } try { channel.close(); } catch (IOException ex) { Logger.getLogger(LineFileChannel.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:Xtext2SonarM2T.launcher.popupMenus.AcceleoGenerateGenerateSonarQubePluginAction.java
@SuppressWarnings("resource") private void copyFile(File source, File target, String name) throws IOException { if (!target.exists()) { target.createNewFile();// ww w .j a va2 s.c o m } //updateNaming(source, name); FileChannel sourceChannel; FileChannel targetChannel; sourceChannel = new FileInputStream(source).getChannel(); targetChannel = new FileOutputStream(target).getChannel(); sourceChannel.transferTo(0, sourceChannel.size(), targetChannel); if (sourceChannel != null) { sourceChannel.close(); } if (targetChannel != null) { targetChannel.close(); } }
From source file:com.aimluck.eip.services.storage.impl.ALDefaultStorageHanlder.java
@Override public boolean copyFile(String srcRootPath, String srcDir, String srcFileName, String destRootPath, String destDir, String destFileName) { File srcPath = new File(srcRootPath + separator() + Database.getDomainName() + separator() + srcDir); if (!srcPath.exists()) { try {/*ww w. ja va2s.co m*/ srcPath.mkdirs(); } catch (Exception e) { logger.error("Can't create directory...:" + srcPath); return false; } } File destPath = new File(destRootPath + separator() + Database.getDomainName() + separator() + destDir); if (!destPath.exists()) { try { destPath.mkdirs(); } catch (Exception e) { logger.error("Can't create directory...:" + destPath); return false; } } File from = new File(srcPath + separator() + srcFileName); File to = new File(destPath + separator() + destFileName); boolean res = true; FileChannel srcChannel = null; FileChannel destChannel = null; try { srcChannel = new FileInputStream(from).getChannel(); destChannel = new FileOutputStream(to).getChannel(); destChannel.transferFrom(srcChannel, 0, srcChannel.size()); } catch (Exception ex) { logger.error("ALDefaultStorageHanlder.copyFile", ex); res = false; } finally { if (destChannel != null) { try { destChannel.close(); } catch (IOException ex) { logger.error("ALDefaultStorageHanlder.copyFile", ex); res = false; } } if (srcChannel != null) { try { srcChannel.close(); } catch (IOException ex) { logger.error("ALDefaultStorageHanlder.copyFile", ex); res = false; } } } return res; }