Java Code Examples for java.io.FileInputStream#getChannel()

The following examples show how to use java.io.FileInputStream#getChannel() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: DatabaseUtil.java    From xDrip with GNU General Public License v3.0 6 votes vote down vote up
public static void loadSql(Context context, Uri uri) {
    try {
        final String databaseName = new Configuration.Builder(context).create().getDatabaseName();

        final File currentDB = context.getDatabasePath(databaseName);
        final File replacement = new File(uri.getPath());
        if (currentDB.canWrite()) {
            final FileInputStream srcStream = new FileInputStream(replacement);
            final FileChannel src = srcStream.getChannel();
            final FileOutputStream destStream = new FileOutputStream(currentDB);
            final FileChannel dst = destStream.getChannel();
            dst.transferFrom(src, 0, src.size());
            src.close();
            srcStream.close();
            dst.close();
            destStream.close();
        } else {
            throw new RuntimeException("Couldn't write to " + currentDB);
        }
    } catch (final Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 2
Source File: LocalFilesystem.java    From reader with MIT License 6 votes vote down vote up
/**
 * Moved this code into it's own method so moveTo could use it when the move is across file systems
 */
private void copyAction(File srcFile, File destFile)
        throws FileNotFoundException, IOException {
    FileInputStream istream = new FileInputStream(srcFile);
    FileOutputStream ostream = new FileOutputStream(destFile);
    FileChannel input = istream.getChannel();
    FileChannel output = ostream.getChannel();

    try {
        input.transferTo(0, input.size(), output);
    } finally {
        istream.close();
        ostream.close();
        input.close();
        output.close();
    }
}
 
Example 3
Source File: StorageUtils.java    From Camdroid with Apache License 2.0 6 votes vote down vote up
private static void copyAction(File srcFile, File destFile)
        throws FileNotFoundException, IOException {
    FileInputStream istream = new FileInputStream(srcFile);
    FileOutputStream ostream = new FileOutputStream(destFile);
    FileChannel input = istream.getChannel();
    FileChannel output = ostream.getChannel();

    try {
        input.transferTo(0, input.size(), output);
    } finally {
        istream.close();
        ostream.close();
        input.close();
        output.close();
    }
}
 
Example 4
Source File: ArrowConverter.java    From DataVec with Apache License 2.0 6 votes vote down vote up
/**
 * Read a datavec schema and record set
 * from the given arrow file.
 * @param input the input to read
 * @return the associated datavec schema and record
 */
public static Pair<Schema,ArrowWritableRecordBatch> readFromFile(FileInputStream input) throws IOException {
    BufferAllocator allocator = new RootAllocator(Long.MAX_VALUE);
    Schema retSchema = null;
    ArrowWritableRecordBatch ret = null;
    SeekableReadChannel channel = new SeekableReadChannel(input.getChannel());
    ArrowFileReader reader = new ArrowFileReader(channel, allocator);
    reader.loadNextBatch();
    retSchema = toDatavecSchema(reader.getVectorSchemaRoot().getSchema());
    //load the batch
    VectorUnloader unloader = new VectorUnloader(reader.getVectorSchemaRoot());
    VectorLoader vectorLoader = new VectorLoader(reader.getVectorSchemaRoot());
    ArrowRecordBatch recordBatch = unloader.getRecordBatch();

    vectorLoader.load(recordBatch);
    ret = asDataVecBatch(recordBatch,retSchema,reader.getVectorSchemaRoot());
    ret.setUnloader(unloader);

    return Pair.of(retSchema,ret);

}
 
Example 5
Source File: FileUtil.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
/**
 * Copy a single file using NIO.
 * @throws IOException
 */
private static void nioCopy(FileOutputStream fos, FileInputStream fis)
    throws IOException {
  FileChannel outChannel = fos.getChannel();
  FileChannel inChannel = fis.getChannel();
  long length = inChannel.size();
  long offset = 0;
  while(true) {
    long remaining = length - offset;
    
    long toTransfer = remaining < MAX_TRANSFER_SIZE ? remaining : MAX_TRANSFER_SIZE;
    long transferredBytes = inChannel.transferTo(offset, toTransfer, outChannel);
    offset += transferredBytes;
    length = inChannel.size();
    if(offset >= length) {
      break;
    }
  }
}
 
Example 6
Source File: MappableBlock.java    From big-c with Apache License 2.0 6 votes vote down vote up
/**
 * Load the block.
 *
 * mmap and mlock the block, and then verify its checksum.
 *
 * @param length         The current length of the block.
 * @param blockIn        The block input stream.  Should be positioned at the
 *                       start.  The caller must close this.
 * @param metaIn         The meta file input stream.  Should be positioned at
 *                       the start.  The caller must close this.
 * @param blockFileName  The block file name, for logging purposes.
 *
 * @return               The Mappable block.
 */
public static MappableBlock load(long length,
    FileInputStream blockIn, FileInputStream metaIn,
    String blockFileName) throws IOException {
  MappableBlock mappableBlock = null;
  MappedByteBuffer mmap = null;
  FileChannel blockChannel = null;
  try {
    blockChannel = blockIn.getChannel();
    if (blockChannel == null) {
      throw new IOException("Block InputStream has no FileChannel.");
    }
    mmap = blockChannel.map(MapMode.READ_ONLY, 0, length);
    NativeIO.POSIX.getCacheManipulator().mlock(blockFileName, mmap, length);
    verifyChecksum(length, metaIn, blockChannel, blockFileName);
    mappableBlock = new MappableBlock(mmap, length);
  } finally {
    IOUtils.closeQuietly(blockChannel);
    if (mappableBlock == null) {
      if (mmap != null) {
        NativeIO.POSIX.munmap(mmap); // unmapping also unlocks
      }
    }
  }
  return mappableBlock;
}
 
Example 7
Source File: FileUtils.java    From cordova-android-chromeview with Apache License 2.0 6 votes vote down vote up
/**
 * Moved this code into it's own method so moveTo could use it when the move is across file systems
 */
private void copyAction(File srcFile, File destFile)
        throws FileNotFoundException, IOException {
    FileInputStream istream = new FileInputStream(srcFile);
    FileOutputStream ostream = new FileOutputStream(destFile);
    FileChannel input = istream.getChannel();
    FileChannel output = ostream.getChannel();

    try {
        input.transferTo(0, input.size(), output);
    } finally {
        istream.close();
        ostream.close();
        input.close();
        output.close();
    }
}
 
Example 8
Source File: LocalFileSystem.java    From beam with Apache License 2.0 5 votes vote down vote up
@Override
protected ReadableByteChannel open(LocalResourceId resourceId) throws IOException {
  LOG.debug("opening file {}", resourceId);
  @SuppressWarnings("resource") // The caller is responsible for closing the channel.
  FileInputStream inputStream = new FileInputStream(resourceId.getPath().toFile());
  // Use this method for creating the channel (rather than new FileChannel) so that we get
  // regular FileNotFoundException. Closing the underyling channel will close the inputStream.
  return inputStream.getChannel();
}
 
Example 9
Source File: Files.java    From joyqueue with Apache License 2.0 5 votes vote down vote up
/**
 * 从文件流里面拷贝数据
 *
 * @param in     输入流
 * @param out    输出流
 * @param start  输入位置
 * @param length 长度
 * @throws IOException
 */
protected static void copy(final FileInputStream in, final FileOutputStream out, final long start,
        final long length) throws IOException {
    if (in == null || out == null) {
        return;
    }
    FileChannel fci = in.getChannel();
    FileChannel fco = out.getChannel();
    try {
        fci.transferTo(start, length, fco);
        fco.force(false);
    } finally {
        Close.close(fci).close(fco);
    }
}
 
Example 10
Source File: ResettableInputStream.java    From ibm-cos-sdk-java with Apache License 2.0 5 votes vote down vote up
/**
 * @param file
 *            can be null if not known
 */
private ResettableInputStream(FileInputStream fis, File file) throws IOException {
    super(fis);
    this.file = file;
    this.fis = fis;
    this.fileChannel = fis.getChannel();
    this.markPos = fileChannel.position();
}
 
Example 11
Source File: ResettableInputStream.java    From cos-java-sdk-v5 with MIT License 5 votes vote down vote up
/**
 * @param file can be null if not known
 */
private ResettableInputStream(FileInputStream fis, File file) throws IOException {
    super(fis);
    this.file = file;
    this.fis = fis;
    this.fileChannel = fis.getChannel();
    this.markPos = fileChannel.position();
}
 
Example 12
Source File: RapidoidConnection.java    From rapidoid with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized Channel write(File file) {
	try {
		FileInputStream stream = new FileInputStream(file);
		FileChannel fileChannel = stream.getChannel();
		output.append(fileChannel);
		stream.close();
	} catch (IOException e) {
		throw U.rte(e);
	}

	return this;
}
 
Example 13
Source File: LogPageHandler.java    From dubbox with Apache License 2.0 5 votes vote down vote up
public Page handle(URL url) {
      long size = 0;
String content = "";
String modified = "Not exist";
if (file != null && file.exists()) {
	try {
		FileInputStream fis = new FileInputStream(file);
		FileChannel channel = fis.getChannel();
		size = channel.size();
		ByteBuffer bb;
		if (size <= SHOW_LOG_LENGTH) {
			bb = ByteBuffer.allocate((int) size);
			channel.read(bb, 0);
		} else {
			int pos = (int) (size - SHOW_LOG_LENGTH);
			bb = ByteBuffer.allocate(SHOW_LOG_LENGTH);
			channel.read(bb, pos);
		}
		bb.flip();
		content = new String(bb.array()).replace("<", "&lt;")
				.replace(">", "&gt;").replace("\n", "<br/><br/>");
		modified = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(new Date(file.lastModified()));
	} catch (IOException e) {
	}
}
Level level = LogManager.getRootLogger().getLevel();
      List<List<String>> rows = new ArrayList<List<String>>();
      List<String> row = new ArrayList<String>();
      row.add(content);
      rows.add(row);
      return new Page("Log", "Log",  new String[] {(file == null ? "" : file.getName()) + ", " + size + " bytes, " + modified + ", " + level}, rows);
  }
 
Example 14
Source File: FileUtils.java    From MyTv with Apache License 2.0 5 votes vote down vote up
/**
 * 以指定编码读入本地文件,以指定编码方式输出,NIO
 * 
 * @param filePath
 *            文件路径
 * @param inCharsetName
 *            编码名称,读入
 * @param outCharsetName
 *            编码名称,输出
 * @throws FileNotFoundException
 * @throws IOException
 * @return
 */
public static byte[] readWithNIO(String filePath, String inCharsetName,
		String outCharsetName) throws FileNotFoundException, IOException {
	if (filePath == null) {
		return null;
	}
	FileInputStream fis = new FileInputStream(new File(filePath));
	FileChannel fc = fis.getChannel();
	ByteBuffer byteBuffer = ByteBuffer.allocate(fis.available());
	ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);

	while (true) {
		int count = fc.read(buffer);
		if (count == -1) {
			break;
		}
		buffer.flip();
		byteBuffer.put(buffer);
		buffer.clear();
	}
	fc.close();
	fis.close();
	byteBuffer.flip();
	Charset inCharset = Charset.forName(inCharsetName);
	Charset outCharset = Charset.forName(outCharsetName);
	CharsetDecoder decoder = inCharset.newDecoder();
	CharsetEncoder encoder = outCharset.newEncoder();
	CharBuffer charBuffer = decoder.decode(byteBuffer);
	ByteBuffer resultBuffer = encoder.encode(charBuffer);
	int size = resultBuffer.limit();
	byte[] data = new byte[size];
	resultBuffer.get(data, 0, size);
	return data;
}
 
Example 15
Source File: Classifier.java    From Machine-Learning-Projects-for-Mobile-Applications with MIT License 5 votes vote down vote up
/** Memory-map the model file in Assets. */
private MappedByteBuffer loadModelFile(Activity activity) throws IOException {
    AssetFileDescriptor fileDescriptor = activity.getAssets().openFd(getModelPath());
    FileInputStream inputStream = new FileInputStream(fileDescriptor.getFileDescriptor());
    FileChannel fileChannel = inputStream.getChannel();
    long startOffset = fileDescriptor.getStartOffset();
    long declaredLength = fileDescriptor.getDeclaredLength();
    return fileChannel.map(FileChannel.MapMode.READ_ONLY, startOffset, declaredLength);
}
 
Example 16
Source File: ByteBufferInputStreamTest.java    From database with GNU General Public License v2.0 4 votes vote down vote up
public void testStream() throws FileNotFoundException, IOException {
	File f = File.createTempFile( ByteBufferInputStreamTest.class.getName(), "tmp" );
	f.deleteOnExit();
	final int l = 100000;
	long seed = System.currentTimeMillis();
	if ( DEBUG ) System.err.println( "Seed: " + seed );
	Random random = new Random( seed );

	for( int n = 1; n < 4; n++ ) {
		final FileOutputStream fos = new FileOutputStream( f );
		for( int i = 0; i < l * n; i++ ) fos.write( random.nextInt() & 0xFF );
		fos.close();

		ByteBufferInputStream bis = ByteBufferInputStream.map( new FileInputStream( f ).getChannel(), MapMode.READ_ONLY );

		FileInputStream test = new FileInputStream( f );
		FileChannel fc = test.getChannel();
		int a1, a2, off, len, pos;
		byte b1[] = new byte[ 32768 ];
		byte b2[] = new byte[ 32768 ];

		for( int k = 0; k < l / 10; k++ ) {

			switch ( random.nextInt( 6 ) ) {

			case 0:
				if ( DEBUG ) System.err.println( "read()" );
				a1 = bis.read();
				a2 = test.read();
				assertEquals( a2, a1 );
				break;

			case 1:
				off = random.nextInt( b1.length );
				len = random.nextInt( b1.length - off + 1 );
				a1 = bis.read( b1, off, len );
				a2 = test.read( b2, off, len );
				if ( DEBUG ) System.err.println( "read(b, " + off + ", " + len + ")" );

				assertEquals( a2, a1 );

				for ( int i = off; i < off + len; i++ )
					assertEquals( b2[ i ], b1[ i ] );
				break;

			case 2:
				if ( DEBUG ) System.err.println( "available()" );
				assertEquals( test.available(), bis.available() );
				break;

			case 3:
				pos = (int)bis.position();
				if ( DEBUG ) System.err.println( "position()=" + pos );
				assertEquals( (int)fc.position(), pos );
				break;

			case 4:
				pos = random.nextInt( l * n );
				bis.position( pos );
				if ( DEBUG ) System.err.println( "position(" + pos + ")" );
				( test = new FileInputStream( f ) ).skip( pos );
				fc = test.getChannel();
				break;

			case 5:
				pos = random.nextInt( (int)( l * n - bis.position() + 1 ) );
				if ( DEBUG ) System.err.println( "skip(" + pos + ")" );
				a1 = (int)bis.skip( pos );
				a2 = (int)test.skip( pos );
				assertEquals( a2, a1 );
				break;
			}
		}
		
		test.close();
	}
}
 
Example 17
Source File: GFSnapshot.java    From gemfirexd-oss with Apache License 2.0 4 votes vote down vote up
public GFSnapshotImporter(File in) throws IOException, ClassNotFoundException {
  pdx = new ExportedRegistry();

  // read header and pdx registry
  long entryPosition;
  
  FileInputStream fis = new FileInputStream(in);
  FileChannel fc = fis.getChannel();
  DataInputStream tmp = new DataInputStream(fis);
  try {
    // read the snapshot file header
    version = tmp.readByte();
    if (version == SNAP_VER_1) {
      throw new IOException(LocalizedStrings.Snapshot_UNSUPPORTED_SNAPSHOT_VERSION_0.toLocalizedString(SNAP_VER_1) + ": " + in);
      
    } else if (version == SNAP_VER_2) {
      // read format
      byte[] format = new byte[3];
      tmp.readFully(format);
      if (!Arrays.equals(format, SNAP_FMT)) {
        throw new IOException(LocalizedStrings.Snapshot_UNRECOGNIZED_FILE_TYPE_0.toLocalizedString(Arrays.toString(format)) + ": " + in);
      }
      
      // read pdx location
      long registryPosition = tmp.readLong();
      
      // read region
      region = tmp.readUTF();
      entryPosition = fc.position();
      
      // read pdx
      if (registryPosition != -1) {
        fc.position(registryPosition);
        pdx.fromData(tmp);
      }
    } else {
      throw new IOException(LocalizedStrings.Snapshot_UNRECOGNIZED_FILE_VERSION_0.toLocalizedString(version) + ": " + in);
    }
  } finally {
    tmp.close();
  }
  
  // check compatibility with the existing pdx types so we don't have to 
  // do any translation...preexisting types or concurrent put ops may cause
  // this check to fail
  checkPdxTypeCompatibility();
  checkPdxEnumCompatibility();
  
  // open new stream with buffering for reading entries
  dis = new DataInputStream(new BufferedInputStream(new FileInputStream(in)));
  dis.skip(entryPosition);
}
 
Example 18
Source File: TestConvertArrowFormatTool.java    From multiple-dimension-spread with Apache License 2.0 4 votes vote down vote up
@Test
public void T_convert_1() throws IOException{
  byte[] mdsFile = createTestData();
  InputStream in = new ByteArrayInputStream( mdsFile );
  MDSReader reader = new MDSReader();
  Configuration config = new Configuration();
  reader.setNewStream( in , mdsFile.length , config );
  MDSArrowReader arrowReader = new MDSArrowReader( reader , config );
  File testFile = new File( "target/TestConvertArrowFormatTool_T_convert_1.mds" );
  if( testFile.exists() ){
    testFile.delete();
  }
  FileOutputStream out = new FileOutputStream( testFile );
  ConvertArrowFormatTool.convert( arrowReader , out , config );

  FileInputStream arrowIn = new FileInputStream( testFile ); 
  ArrowFileReader ar = new ArrowFileReader( arrowIn.getChannel() , new RootAllocator( Integer.MAX_VALUE ) );
  VectorSchemaRoot root  = ar.getVectorSchemaRoot();
  ArrowBlock rbBlock = ar.getRecordBlocks().get(0);
  ar.loadRecordBatch(rbBlock);
  List<FieldVector> fieldVectorList = root.getFieldVectors();
  Map<String,FieldVector> vectorMap = new HashMap<String,FieldVector>();
  for( FieldVector v : fieldVectorList ){
    vectorMap.put( v.getField().getName() , v );
  }

  assertTrue( vectorMap.containsKey( "col1" ) );
  assertTrue( vectorMap.containsKey( "col2" ) );
  assertTrue( vectorMap.containsKey( "col3" ) );

  BigIntVector col1 = (BigIntVector)( vectorMap.get( "col1" ) );
  VarCharVector col2 = (VarCharVector)( vectorMap.get( "col2" ) );
  VarCharVector col3 = (VarCharVector)( vectorMap.get( "col3" ) );


  assertEquals( col1.get(0) , 100L );
  assertEquals( col1.get(1) , 200L );
  assertEquals( col1.get(2) , 300L );

  assertEquals( col2.getObject(0).toString() , "aaa" );
  assertTrue( col2.isNull(1) );
  assertTrue( col2.isNull(2) );

  assertTrue( col3.isNull(0) );
  assertEquals( col3.getObject(1).toString() , "BBB" );
  assertEquals( col3.getObject(2).toString() , "CCC" );

  testFile.delete();
  ar.close();
}
 
Example 19
Source File: Transfer.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
@Test
public void testFileChannel() throws Exception {
    File source = File.createTempFile("source", null);
    source.deleteOnExit();
    File sink = File.createTempFile("sink", null);
    sink.deleteOnExit();

    FileOutputStream fos = new FileOutputStream(source);
    FileChannel sourceChannel = fos.getChannel();
    sourceChannel.write(ByteBuffer.wrap(
        "Use the source, Luke!".getBytes()));
    sourceChannel.close();

    FileInputStream fis = new FileInputStream(source);
    sourceChannel = fis.getChannel();

    RandomAccessFile raf = new RandomAccessFile(sink, "rw");
    FileChannel sinkChannel = raf.getChannel();
    long oldSinkPosition = sinkChannel.position();
    long oldSourcePosition = sourceChannel.position();

    long bytesWritten = sinkChannel.transferFrom(sourceChannel, 0, 10);
    if (bytesWritten != 10)
        throw new RuntimeException("Transfer failed");

    if (sourceChannel.position() == oldSourcePosition)
        throw new RuntimeException("Source position didn't change");

    if (sinkChannel.position() != oldSinkPosition)
        throw new RuntimeException("Sink position changed");

    if (sinkChannel.size() != 10)
        throw new RuntimeException("Unexpected sink size");

    bytesWritten = sinkChannel.transferFrom(sourceChannel, 1000, 10);

    if (bytesWritten > 0)
        throw new RuntimeException("Wrote past file size");

    sourceChannel.close();
    sinkChannel.close();

    source.delete();
    sink.delete();
}
 
Example 20
Source File: FileUtils.java    From KJFrameForAndroid with Apache License 2.0 3 votes vote down vote up
/**
 * 快速复制文件(采用nio操作)
 * 
 * @param is
 *            数据来源
 * @param os
 *            数据目标
 * @throws IOException
 */
public static void copyFileFast(FileInputStream is, FileOutputStream os)
        throws IOException {
    FileChannel in = is.getChannel();
    FileChannel out = os.getChannel();
    in.transferTo(0, in.size(), out);
}