List of usage examples for java.io FileInputStream getChannel
public FileChannel getChannel()
From source file:it.readbeyond.minstrel.commander.Commander.java
private void copyFile(String sourcePath, String destinationPath, final CallbackContext callbackContext) { String source = this.normalizePath(sourcePath); String destination = this.normalizePath(destinationPath); try {/*from w w w . j a v a 2 s .com*/ File f = new File(source); if (f.exists()) { File d = new File(destination); // create parent directory, if not existing File destinationParent = d.getParentFile(); destinationParent.mkdirs(); // TODO check for write permission? // copy file FileInputStream inStream = new FileInputStream(f); FileOutputStream outStream = new FileOutputStream(d); FileChannel inChannel = inStream.getChannel(); FileChannel outChannel = outStream.getChannel(); inChannel.transferTo(0, inChannel.size(), outChannel); inStream.close(); outStream.close(); callbackContext.success(MESSAGE_FILE_COPIED); } else { callbackContext.success(MESSAGE_FILE_DOES_NOT_EXIST); } } catch (Exception e) { callbackContext.success(MESSAGE_ERROR_WHILE_COPYING); } }
From source file:thv.th.view.AnimationPanel.java
protected Vector<THAnimation> loadAnimations(int start) throws IOException { Vector<THAnimation> res = new Vector<THAnimation>(); int animCount = (int) (startFile.length() / 4); FileInputStream startStream = new FileInputStream(startFile); ABuffer tabStream = new FileBuffer(tabFile); FileInputStream chunkStream = new FileInputStream(chunksFile); FileInputStream frameStream = new FileInputStream(framesFile); FileInputStream listStream = new FileInputStream(listFile); FileInputStream elementStream = new FileInputStream(spriteElementFile); for (int i = 0; i < perPage; ++i) { if ((i + start) >= animCount) break; startStream.getChannel().position((i + start) * 4); int index = EndianUtils.readSwappedShort(startStream); int maxFrameWidth = 0; int maxFrameHeight = 0; int maxXOffset = 0; int minYOffset = 256; Vector<THFrame> frames = loadFrameChain(index, frameStream, listStream, elementStream); if (frames.size() == 0) continue; for (THFrame frame : frames) { if (frame.getWidth() > maxFrameWidth) { maxFrameWidth = frame.getWidth(); //maxXOffset = frame.getMaxXOffset(tabStream); }/* w ww . j a va2 s . co m*/ maxXOffset = Math.max(maxXOffset, frame.getMaxXOffset(tabStream)); minYOffset = Math.min(minYOffset, frame.getMinYOffset(tabStream)); } int xover = 0; int xunder = 0; int yover = 0; int yunder = 0; for (THFrame frame : frames) { xover = Math.max(xover, frame.getXOverrun(maxFrameWidth, maxXOffset, tabStream)); xunder = Math.max(xunder, frame.getXUnderrun(maxFrameWidth, maxXOffset, tabStream)); yover = Math.max(yover, frame.getYOverrun(0, minYOffset, tabStream)); yunder = Math.max(yunder, frame.getYUnderrun(0, minYOffset, tabStream)); } maxFrameWidth += xover + xunder; maxFrameHeight += yover + yunder; THAnimation an = new THAnimation(maxFrameWidth, maxFrameHeight, i + start); for (THFrame frame : frames) { an.addFrame(frame.renderInAnim(tabStream, chunkStream, palette, 4, maxFrameWidth, maxFrameHeight, maxXOffset, minYOffset, xunder, yunder), frame); } res.add(an); } //tabStream.close(); chunkStream.close(); frameStream.close(); listStream.close(); elementStream.close(); selectPanel.setSelectionCount((animCount / perPage) + 1); return res; }
From source file:automenta.knowtention.channel.LineFileChannel.java
@Override public void run() { FileInputStream fileInputStream = null; FileChannel channel = null;//from w w w . j av a 2s . c om ByteBuffer buffer = null; 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:kr.wdream.storyshop.AndroidUtilities.java
public static boolean copyFile(File sourceFile, File destFile) throws IOException { if (!destFile.exists()) { destFile.createNewFile();//from w w w.j ava 2 s . c om } FileInputStream source = null; FileOutputStream destination = null; try { source = new FileInputStream(sourceFile); destination = new FileOutputStream(destFile); destination.getChannel().transferFrom(source.getChannel(), 0, source.getChannel().size()); } catch (Exception e) { FileLog.e("tmessages", e); return false; } finally { if (source != null) { source.close(); } if (destination != null) { destination.close(); } } return true; }
From source file:org.cytobank.fcs_files.events.MemoryEvents.java
public MemoryEvents(File doubleFile) throws IOException, MemoryEventsException { this.doubleFile = doubleFile; numberOfEvents = (int) doubleFile.length() / BYTES_PER_DOUBLE; setupMemoryEventsArray(numberOfEvents); try {/*from ww w . j a v a 2 s . c o m*/ // Read in the doubles file FileInputStream fileInputStream = new FileInputStream(doubleFile); FileChannel fileChannel = fileInputStream.getChannel(); MappedByteBuffer mappedByteBuffer = fileChannel.map(MapMode.READ_ONLY, 0, doubleFile.length()); DoubleBuffer doubleBuffer = mappedByteBuffer.asDoubleBuffer(); doubleBuffer.get(events, 0, numberOfEvents); fileChannel.close(); fileInputStream.close(); } catch (IOException ioe) { destroy(); throw ioe; } openedAt = System.currentTimeMillis(); }
From source file:org.apache.hadoop.hdfs.tools.offlineImageViewer.PBImageTextWriter.java
private void output(Configuration conf, FileSummary summary, FileInputStream fin, ArrayList<FileSummary.Section> sections) throws IOException { InputStream is;//from w w w . ja va 2 s.c o m long startTime = Time.monotonicNow(); for (FileSummary.Section section : sections) { if (SectionName.fromString(section.getName()) == SectionName.INODE) { fin.getChannel().position(section.getOffset()); is = FSImageUtil.wrapInputStreamForCompression(conf, summary.getCodec(), new BufferedInputStream(new LimitInputStream(fin, section.getLength()))); outputINodes(is); } } long timeTaken = Time.monotonicNow() - startTime; LOG.debug("Time to output inodes: {}ms", timeTaken); }
From source file:com.quartz.AmazonQuartzJob.java
/** * Calculate content MD5 header values for feeds stored on disk. *//* ww w. ja va2 s . c o m*/ public String computeContentMD5HeaderValue(FileInputStream fis) throws IOException, NoSuchAlgorithmException { DigestInputStream dis = new DigestInputStream(fis, MessageDigest.getInstance("MD5")); byte[] buffer = new byte[8192]; while (dis.read(buffer) > 0) ; String md5Content = new String( org.apache.commons.codec.binary.Base64.encodeBase64(dis.getMessageDigest().digest())); // Effectively resets the stream to be beginning of the file // via a FileChannel. fis.getChannel().position(0); return md5Content; }
From source file:org.apache.hadoop.hdfs.BlockReaderLocalLegacy.java
/** * Reads bytes into a buffer until EOF or the buffer's limit is reached *//*from w w w . j a v a 2s . c o m*/ private int fillBuffer(FileInputStream stream, ByteBuffer buf) throws IOException { try (TraceScope ignored = tracer.newScope("BlockReaderLocalLegacy#fillBuffer(" + blockId + ")")) { int bytesRead = stream.getChannel().read(buf); if (bytesRead < 0) { //EOF return bytesRead; } while (buf.remaining() > 0) { int n = stream.getChannel().read(buf); if (n < 0) { //EOF return bytesRead; } bytesRead += n; } return bytesRead; } }
From source file:org.gdms.driver.shapefile.ShapefileDriver.java
@Override public void open() throws DriverException { LOG.trace("Opening"); try {//w w w. j a va 2s . com FileInputStream shpFis = new FileInputStream(file); reader = new ShapefileReader(shpFis.getChannel()); File shx = FileUtils.getFileWithExtension(file, "shx"); if (shx == null || !shx.exists()) { throw new DriverException("The file " + file.getAbsolutePath() + " has no corresponding .shx file"); } FileInputStream shxFis = new FileInputStream(shx); shxFile = new IndexFile(shxFis.getChannel()); ShapefileHeader header = reader.getHeader(); envelope = new Envelope(new Coordinate(header.minX(), header.minY()), new Coordinate(header.maxX(), header.maxY())); ShapeType type = header.getShapeType(); dbfDriver = new DBFDriver(); dbfDriver.setDataSourceFactory(dataSourceFactory); File dbf = FileUtils.getFileWithExtension(file, "dbf"); if (dbf == null || !dbf.exists()) { throw new DriverException("The file " + file.getAbsolutePath() + " has no corresponding .dbf file"); } dbfDriver.setFile(dbf); dbfDriver.open(); // registering DataSet dataSet = dbfDriver.getTable(DriverManager.DEFAULT_SINGLE_TABLE_NAME); Constraint dc; //We can force the type of the data in the GDMS table according to the type //given in the ShapeFile. This variable is here for this task. int gtype; // In case of a geometric type, the GeometryConstraint is mandatory if (type.id == ShapeType.POINT.id) { gtype = Type.POINT; dc = new Dimension3DConstraint(2); } else if (type.id == ShapeType.ARC.id) { gtype = Type.MULTILINESTRING; dc = new Dimension3DConstraint(2); } else if (type.id == ShapeType.POLYGON.id) { gtype = Type.MULTIPOLYGON; dc = new Dimension3DConstraint(2); } else if (type.id == ShapeType.MULTIPOINT.id) { gtype = Type.MULTIPOINT; dc = new Dimension3DConstraint(2); } else if (type.id == ShapeType.POINTZ.id) { gtype = Type.POINT; dc = new Dimension3DConstraint(3); } else if (type.id == ShapeType.ARCZ.id) { gtype = Type.MULTILINESTRING; dc = new Dimension3DConstraint(3); } else if (type.id == ShapeType.POLYGONZ.id) { gtype = Type.MULTIPOLYGON; dc = new Dimension3DConstraint(3); } else if (type.id == ShapeType.MULTIPOINTZ.id) { gtype = Type.MULTIPOINT; dc = new Dimension3DConstraint(3); } else { throw new DriverException("Unknown geometric type !"); } Constraint[] constraints; File prj = FileUtils.getFileWithExtension(file, "prj"); if (prj != null && prj.exists()) { crs = DataSourceFactory.getCRSFactory().createFromPrj(prj); if (crs != null) { CRSConstraint cc = new CRSConstraint(crs); constraints = new Constraint[] { dc, cc }; } else { constraints = new Constraint[] { dc }; } } else { constraints = new Constraint[] { dc }; } metadata.clear(); metadata.addField(0, "the_geom", gtype, constraints); metadata.addAll(dataSet.getMetadata()); } catch (IOException e) { throw new DriverException(e); } catch (ShapefileException e) { throw new DriverException(e); } catch (InvalidTypeException e) { throw new DriverException(e); } catch (CRSException e) { throw new DriverException(e); } }
From source file:com.liferay.portal.util.FileImpl.java
public boolean isSameContent(File file, byte[] bytes, int length) { FileChannel fileChannel = null; try {// www . j a v a 2s . c o m FileInputStream fileInputStream = new FileInputStream(file); fileChannel = fileInputStream.getChannel(); if (fileChannel.size() != length) { return false; } byte[] buffer = new byte[1024]; ByteBuffer byteBuffer = ByteBuffer.wrap(buffer); int bufferIndex = 0; int bufferLength = -1; while (((bufferLength = fileChannel.read(byteBuffer)) > 0) && (bufferIndex < length)) { for (int i = 0; i < bufferLength; i++) { if (buffer[i] != bytes[bufferIndex++]) { return false; } } byteBuffer.clear(); } if ((bufferIndex != length) || (bufferLength != -1)) { return false; } else { return true; } } catch (Exception e) { return false; } finally { if (fileChannel != null) { try { fileChannel.close(); } catch (IOException ioe) { } } } }