List of usage examples for java.io FileInputStream getChannel
public FileChannel getChannel()
From source file:org.gdms.driver.dbf.DBFDriver.java
@Override public void open() throws DriverException { LOG.trace("Opening file"); try {//from w ww .j a v a 2 s .c o m FileInputStream fis = new FileInputStream(file); dbaseReader = new DbaseFileReader(fis.getChannel()); loadInternalMetadata(); } catch (IOException e) { throw new DriverException(e); } }
From source file:com.frostwire.util.MP4Muxer.java
public void mux(String video, String audio, String output, final MP4Metadata mt) throws IOException { FileInputStream videoIn = new FileInputStream(video); FileInputStream audioIn = new FileInputStream(audio); try {/* w w w.jav a 2 s . c o m*/ FileChannel videoChannel = videoIn.getChannel(); Movie videoMovie = buildMovie(videoChannel); FileChannel audioChannel = audioIn.getChannel(); Movie audioMovie = buildMovie(audioChannel); Movie outMovie = new Movie(); for (Track trk : videoMovie.getTracks()) { outMovie.addTrack(trk); } for (Track trk : audioMovie.getTracks()) { outMovie.addTrack(trk); } Container out = new DefaultMp4Builder() { @Override protected FileTypeBox createFileTypeBox(Movie movie) { List<String> minorBrands = new LinkedList<String>(); minorBrands.add("iso6"); minorBrands.add("avc1"); minorBrands.add("mp41"); minorBrands.add("\0\0\0\0"); return new FileTypeBox("MP4 ", 0, minorBrands); } @Override protected MovieBox createMovieBox(Movie movie, Map<Track, int[]> chunks) { MovieBox moov = super.createMovieBox(movie, chunks); moov.getMovieHeaderBox().setVersion(0); return moov; } @Override protected TrackBox createTrackBox(Track track, Movie movie, Map<Track, int[]> chunks) { TrackBox trak = super.createTrackBox(track, movie, chunks); trak.getTrackHeaderBox().setVersion(0); trak.getTrackHeaderBox().setVolume(1.0f); return trak; } @Override protected Box createUdta(Movie movie) { return mt != null ? addUserDataBox(mt) : null; } }.build(outMovie); FileOutputStream fos = new FileOutputStream(output); try { out.writeContainer(fos.getChannel()); } finally { IOUtils.closeQuietly(fos); } } finally { IOUtils.closeQuietly(videoIn); IOUtils.closeQuietly(audioIn); } }
From source file:com.yahoo.glimmer.indexing.preprocessor.ResourceRecordWriterTest.java
@Test public void writeSubjectAndObjectTest() throws IOException, InterruptedException, ClassNotFoundException { ByteArrayOutputStream bySubjectBos = new ByteArrayOutputStream(1024); FSDataOutputStream bySubjectOs = new FSDataOutputStream(bySubjectBos, null); ByteArrayOutputStream bySubjectOffsetsBos = new ByteArrayOutputStream(1024); FSDataOutputStream bySubjectOffsetsOs = new FSDataOutputStream(bySubjectOffsetsBos, null); e.one(fs).create(e.with(new Path(tempDirPath, "bySubject.bz2")), e.with(false)); e.will(Expectations.returnValue(bySubjectOs)); e.one(fs).create(e.with(new Path(tempDirPath, "bySubject.blockOffsets")), e.with(false)); e.will(Expectations.returnValue(bySubjectOffsetsOs)); e.one(allOs).write(e.with(new ByteMatcher("http://a/key1\nhttp://a/key2\nhttp://a/key3\n", true)), e.with(0), e.with(42));//w w w .j a va 2 s . c o m e.one(contextOs).write(e.with(new ByteMatcher("http://a/key\n", true)), e.with(0), e.with(13)); e.one(objectOs).write(e.with(new ByteMatcher("http://a/key\nbNode123\n", true)), e.with(0), e.with(22)); e.one(predicateOs).write(e.with(new ByteMatcher("3\thttp://a/key\n", true)), e.with(0), e.with(15)); e.one(subjectOs).write(e.with(new ByteMatcher("http://a/key\n", true)), e.with(0), e.with(13)); context.checking(e); ResourceRecordWriter writer = new ResourceRecordWriter(fs, tempDirPath, null); OutputCount outputCount = new OutputCount(); outputCount.output = OUTPUT.PREDICATE; outputCount.count = 3; writer.write(new Text("http://a/key"), outputCount); outputCount.output = OUTPUT.OBJECT; outputCount.count = 0; writer.write(new Text("http://a/key"), outputCount); outputCount.output = OUTPUT.CONTEXT; outputCount.count = 0; writer.write(new Text("http://a/key"), outputCount); outputCount.output = OUTPUT.ALL; outputCount.count = 0; writer.write(new Text("http://a/key1"), outputCount); writer.write(new Text("http://a/key2"), outputCount); writer.write(new Text("http://a/key3"), outputCount); BySubjectRecord record = new BySubjectRecord(); record.setId(66); record.setPreviousId(55); record.setSubject("http://a/key"); record.addRelation("<http://predicate/> <http://Object> ."); writer.write(new Text("http://a/key"), record); outputCount.output = OUTPUT.OBJECT; outputCount.count = 0; writer.write(new Text("bNode123"), outputCount); writer.close(null); context.assertIsSatisfied(); BlockCompressedDocumentCollection collection = new BlockCompressedDocumentCollection("foo", null, 10); InputStream blockOffsetsInputStream = new ByteArrayInputStream(bySubjectOffsetsBos.toByteArray()); File bySubjectTempFile = File.createTempFile(ResourceRecordWriterTest.class.getSimpleName(), "tmp"); FileOutputStream tempFileOutputStream = new FileOutputStream(bySubjectTempFile); bySubjectBos.writeTo(tempFileOutputStream); tempFileOutputStream.flush(); tempFileOutputStream.close(); FileInputStream bySubjectFileInputStream = new FileInputStream(bySubjectTempFile); collection.init(bySubjectFileInputStream.getChannel(), blockOffsetsInputStream, 100000); blockOffsetsInputStream.close(); // Size of collection. This is the same as the number of lines written to ALL. assertEquals(3l, collection.size()); InputStream documentInputStream = collection.stream(65l); assertEquals(-1, documentInputStream.read()); documentInputStream = collection.stream(67l); assertEquals(-1, documentInputStream.read()); documentInputStream = collection.stream(66l); assertNotNull(documentInputStream); collection.close(); bySubjectFileInputStream.close(); }
From source file:pyromaniac.IO.MMFastqImporter.java
/** * Helper function for init(). Scans this.fastq file for sequence starts and records their position. * Multiple MappedByteBuffers are used to handle large files. * @throws Exceptions relating to file reading and decoding. *///w w w .j a v a2 s. c o m private void _initFile() throws Exception { FileInputStream tempStream = new FileInputStream(new File(this.fastqFile)); FileChannel fcSeq = tempStream.getChannel(); this.seqSizeLong = fcSeq.size(); this.recordStarts = new ArrayList<Pair<Integer, Long>>(); int state = -1; for (long startPosition = 0L; startPosition < this.seqSizeLong; startPosition += HALF_GIGA) { MappedByteBuffer recordBuffer = fcSeq.map(FileChannel.MapMode.READ_ONLY, startPosition, Math.min(this.seqSizeLong - startPosition, HALF_GIGA)); this.recordBuffers.add(recordBuffer); int sbf_pos = this.recordBuffers.size() - 1; int maxBuffer = 2048; int bufferSize = (recordBuffer.capacity() > maxBuffer) ? maxBuffer : recordBuffer.capacity(); recordBuffer.limit(bufferSize); recordBuffer.position(0); while (recordBuffer.position() != recordBuffer.capacity()) { int prevPos = recordBuffer.position(); CharBuffer result = decoder.decode(recordBuffer); recordBuffer.position(prevPos); for (int i = 0; i < result.capacity(); i++) { char curr = result.charAt(i); int posInFile = prevPos + i; //I see a fastq header, I am either at beginning of file, or last saw the quality line... if (curr == BEGINNING_FASTQ_SEQ && (state == -1 || state == 4)) { this.recordStarts.add(new Pair<Integer, Long>(sbf_pos, new Long(posInFile))); state = 1; } else if (curr == BEGINNING_FASTQ_QUAL && (state == 1)) { state = 2; } else if ((curr == '\n' || curr == '\r') & state == 2) { state = 3; } else if ((curr == '\n' || curr == '\r') & state == 3) { state = 4; } } int newPos = recordBuffer.limit(); if (recordBuffer.limit() + bufferSize > recordBuffer.capacity()) recordBuffer.limit(recordBuffer.capacity()); else recordBuffer.limit(recordBuffer.limit() + bufferSize); recordBuffer.position(newPos); } recordBuffer.rewind(); } }
From source file:org.meerkat.util.FileUtil.java
/** * readFileContents//from w w w. j ava 2 s . com * * @param filename * @return file contents */ public final String readFileContents(String filePath) { FileInputStream stream = null; String contents = ""; try { stream = new FileInputStream(new File(filePath)); } catch (Exception e) { log.error("File not found: " + filePath, e); } try { FileChannel fc = stream.getChannel(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); /* Instead of using default, pass in a decoder. */ contents = Charset.defaultCharset().decode(bb).toString(); } catch (IOException e) { log.error("Error streaming file contents: " + filePath, e); } finally { try { stream.close(); } catch (IOException e) { log.error("Error closing file stream: " + filePath, e); } } return contents; }
From source file:org.h819.commons.file.MyPDFUtilss.java
/** * PdfReader ? pdf pdf ???/*w ww . j av a2s . c o m*/ * * @param pdfFile ? pdf * @return PdfReader * @throws IOException */ public static PdfReader getPdfReader(File pdfFile) throws IOException { Document.plainRandomAccess = true; FileInputStream fileStream = new FileInputStream(pdfFile); return new PdfReader( new RandomAccessFileOrArray(new FileChannelRandomAccessSource(fileStream.getChannel())), null); }
From source file:org.opennms.systemreport.AbstractSystemReportPlugin.java
protected String slurp(final File lsb) { if (lsb != null && lsb.exists()) { FileInputStream stream = null; try {// ww w. java 2s. c o m stream = new FileInputStream(lsb); FileChannel fc = stream.getChannel(); MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size()); return Charset.defaultCharset().decode(bb).toString().replace("[\\r\\n]*$", ""); } catch (final Exception e) { LOG.debug("Unable to read from file '{}'", lsb.getPath(), e); } finally { IOUtils.closeQuietly(stream); } } return null; }
From source file:edu.rit.flick.genetics.FastFileDeflator.java
protected void createOutputFiles(final File fastFile, final String tempOutputDirectory) throws IOException { datahcf = ByteBufferOutputStream.map(new File(tempOutputDirectory + SEQUENCE_DATA_FILE), MapMode.READ_WRITE, (long) (fastFile.length() * EXPECTED_COMPRESSION_RATIO)); nfile = ByteBufferOutputStream.map(new File(tempOutputDirectory + N_FILE), MapMode.READ_WRITE, (long) (fastFile.length() * EXPECTED_COMPRESSION_RATIO * 2)); headerfile = new BufferedOutputStream(new FileOutputStream(tempOutputDirectory + SEQUENCE_ID_FILE), DEFAULT_BUFFER);/* w w w . j a v a2 s . c o m*/ iupacfile = new BufferedOutputStream(new FileOutputStream(tempOutputDirectory + IUPAC_CODE_FILE), DEFAULT_BUFFER); tailfile = new BufferedOutputStream(new FileOutputStream(tempOutputDirectory + SEQUENCE_TAIL_FILE), DEFAULT_BUFFER); metafile = new FileWriter(new File(tempOutputDirectory + META_FILE)); metafile.write(format(META_FILE_SIZE_FORMAT, fastFile.length())); final FileInputStream fastFis = new FileInputStream(fastFile); fastIn = ByteBufferInputStream.map(fastFis.getChannel()); fastFis.close(); }
From source file:org.apache.pig.data.SchemaTupleBackend.java
private void copyAllFromDistributedCache() throws IOException { String toDeserialize = jConf.get(PigConstants.GENERATED_CLASSES_KEY); if (toDeserialize == null) { LOG.info("No classes in in key [" + PigConstants.GENERATED_CLASSES_KEY + "] to copy from distributed cache."); return;/*from w w w . java 2s . com*/ } LOG.info("Copying files in key [" + PigConstants.GENERATED_CLASSES_KEY + "] from distributed cache: " + toDeserialize); for (String s : toDeserialize.split(",")) { LOG.info("Attempting to read file: " + s); // The string is the symlink into the distributed cache File src = new File(s); FileInputStream fin = null; FileOutputStream fos = null; try { fin = new FileInputStream(src); fos = new FileOutputStream(new File(codeDir, s)); fin.getChannel().transferTo(0, src.length(), fos.getChannel()); LOG.info("Successfully copied file to local directory."); } finally { if (fin != null) { fin.close(); } if (fos != null) { fos.close(); } } } }