Java Code Examples for java.nio.channels.FileChannel#read()
The following examples show how to use
java.nio.channels.FileChannel#read() .
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: PEResourceDirectory.java From davmail with GNU General Public License v2.0 | 6 votes |
public DataEntry(FileChannel chan, long offset) throws IOException { long orgpos = chan.position(); chan.position(PEResourceDirectory.this.m_offsetBase + offset); ByteBuffer buf = ByteBuffer.allocate(16); buf.order(ByteOrder.LITTLE_ENDIAN); chan.read(buf); buf.position(0); OffsetToData = buf.getInt(); Size = buf.getInt(); CodePage = buf.getInt(); Reserved = buf.getInt(); long datapos = PEResourceDirectory.this.m_master.PointerToRawData + (OffsetToData - PEResourceDirectory.this.m_master.VirtualAddress); Data = ByteBuffer.allocate((int)Size); Data.order(ByteOrder.LITTLE_ENDIAN); chan.position(datapos); chan.read(Data); Data.position(0); chan.position(orgpos); }
Example 2
Source File: BlockMetadataHeader.java From hadoop with Apache License 2.0 | 6 votes |
/** * Read the header without changing the position of the FileChannel. * * @param fc The FileChannel to read. * @return the Metadata Header. * @throws IOException on error. */ public static BlockMetadataHeader preadHeader(FileChannel fc) throws IOException { final byte arr[] = new byte[getHeaderSize()]; ByteBuffer buf = ByteBuffer.wrap(arr); while (buf.hasRemaining()) { if (fc.read(buf, 0) <= 0) { throw new EOFException("unexpected EOF while reading " + "metadata file header"); } } short version = (short)((arr[0] << 8) | (arr[1] & 0xff)); DataChecksum dataChecksum = DataChecksum.newDataChecksum(arr, 2); return new BlockMetadataHeader(version, dataChecksum); }
Example 3
Source File: IndexSnapshotFileHeader.java From light-task-scheduler with Apache License 2.0 | 6 votes |
@Override public void read(FileChannel fileChannel) throws IOException { if (fileChannel.size() == 0) { return; } fileChannel.position(0); fileChannel.read(byteBuffer()); byteBuffer().position(0); short readMagic = byteBuffer().getShort(); if (readMagic != magic) { throw new IOException("Invalid file type magic number 0x" + Integer.toHexString(readMagic & 0xFFFF)); } this.storeTxLogRecordId = byteBuffer().getLong(); }
Example 4
Source File: AbstractDiskHttpData.java From netty4.0.27Learn with Apache License 2.0 | 6 votes |
/** * Utility function * @return the array of bytes */ private static byte[] readFrom(File src) throws IOException { long srcsize = src.length(); if (srcsize > Integer.MAX_VALUE) { throw new IllegalArgumentException( "File too big to be loaded in memory"); } FileInputStream inputStream = new FileInputStream(src); byte[] array = new byte[(int) srcsize]; try { FileChannel fileChannel = inputStream.getChannel(); ByteBuffer byteBuffer = ByteBuffer.wrap(array); int read = 0; while (read < srcsize) { read += fileChannel.read(byteBuffer); } } finally { inputStream.close(); } return array; }
Example 5
Source File: TestBlockingNIO.java From code with Apache License 2.0 | 6 votes |
@Test public void client() throws IOException{ //1. 获取通道 SocketChannel sChannel = SocketChannel.open(new InetSocketAddress("127.0.0.1", 9898)); FileChannel inChannel = FileChannel.open(Paths.get("1.jpg"), StandardOpenOption.READ); //2. 分配指定大小的缓冲区 ByteBuffer buf = ByteBuffer.allocate(1024); //3. 读取本地文件,并发送到服务端 while(inChannel.read(buf) != -1){ buf.flip(); sChannel.write(buf); buf.clear(); } //4. 关闭通道 inChannel.close(); sChannel.close(); }
Example 6
Source File: ByteUtil.java From proxyee-down with Apache License 2.0 | 6 votes |
public static boolean matchToken(FileChannel fileChannel, long start, long position, byte[] bts) throws IOException { boolean ret; ByteBuffer buffer = ByteBuffer.allocate(bts.length); long rawPosition; if (start >= 0) { rawPosition = start; } else { rawPosition = fileChannel.position(); } if (position >= 0) { fileChannel.position(position); } fileChannel.read(buffer); buffer.flip(); ret = findBytes(buffer, bts) == 0; fileChannel.position(rawPosition); return ret; }
Example 7
Source File: FileSerializationBenchmark.java From imhotep with Apache License 2.0 | 6 votes |
@Override public int[] deserialize(File file) throws IOException { FileChannel ch = new RandomAccessFile(file, "r").getChannel(); int[] ret = new int[(int)(file.length() / 4)]; ByteBuffer buffer = ByteBuffer.allocateDirect(8192); buffer.order(ByteOrder.LITTLE_ENDIAN); IntBuffer intBuffer = buffer.asIntBuffer(); for (int i = 0; i < ret.length; i += 2048) { buffer.clear(); int lim = ch.read(buffer) / 4; intBuffer.clear(); intBuffer.get(ret, i, lim); } ch.close(); return ret; }
Example 8
Source File: FileIOUtils.java From DevUtils with Apache License 2.0 | 6 votes |
/** * 通过 FileChannel, 读取文件内容, 返回 byte[] * @param file 文件 * @return 文件内容 byte[] */ public static byte[] readFileToBytesByChannel(final File file) { if (!FileUtils.isFileExists(file)) return null; FileChannel fc = null; try { fc = new RandomAccessFile(file, "r").getChannel(); ByteBuffer byteBuffer = ByteBuffer.allocate((int) fc.size()); while (true) { if (!((fc.read(byteBuffer)) > 0)) break; } return byteBuffer.array(); } catch (IOException e) { JCLogUtils.eTag(TAG, e, "readFileToBytesByChannel"); return null; } finally { CloseUtils.closeIOQuietly(fc); } }
Example 9
Source File: ApkUtil.java From walle with Apache License 2.0 | 6 votes |
public static long findCentralDirStartOffset(final FileChannel fileChannel, final long commentLength) throws IOException { // End of central directory record (EOCD) // Offset Bytes Description[23] // 0 4 End of central directory signature = 0x06054b50 // 4 2 Number of this disk // 6 2 Disk where central directory starts // 8 2 Number of central directory records on this disk // 10 2 Total number of central directory records // 12 4 Size of central directory (bytes) // 16 4 Offset of start of central directory, relative to start of archive // 20 2 Comment length (n) // 22 n Comment // For a zip with no archive comment, the // end-of-central-directory record will be 22 bytes long, so // we expect to find the EOCD marker 22 bytes from the end. final ByteBuffer zipCentralDirectoryStart = ByteBuffer.allocate(4); zipCentralDirectoryStart.order(ByteOrder.LITTLE_ENDIAN); fileChannel.position(fileChannel.size() - commentLength - 6); // 6 = 2 (Comment length) + 4 (Offset of start of central directory, relative to start of archive) fileChannel.read(zipCentralDirectoryStart); final long centralDirStartOffset = zipCentralDirectoryStart.getInt(0); return centralDirStartOffset; }
Example 10
Source File: NIODemo4.java From cs-summary-reflection with Apache License 2.0 | 5 votes |
/** * 分散(Scatter)与聚集(Gather) 分散读取(Scattering Reads):将通道中的数据分散到多个缓冲区中 聚集写入(Gathering * Writes):将多个缓冲区中的数据聚集到通道中 * * @throws IOException */ @Test public void test4() throws IOException { RandomAccessFile raf1 = new RandomAccessFile("1.txt", "rw"); // 1. 获取通道 FileChannel channel1 = raf1.getChannel(); // 2. 分配指定大小的缓冲区 ByteBuffer buf1 = ByteBuffer.allocate(100); ByteBuffer buf2 = ByteBuffer.allocate(1024); // 3. 分散读取 ByteBuffer[] bufs = {buf1, buf2}; channel1.read(bufs); for (ByteBuffer byteBuffer : bufs) { byteBuffer.flip(); } System.out.println(new String(bufs[0].array(), 0, bufs[0].limit())); System.out.println("-----------------"); System.out.println(new String(bufs[1].array(), 0, bufs[1].limit())); raf1.close(); // 4. 聚集写入 RandomAccessFile raf2 = new RandomAccessFile("2.txt", "rw"); FileChannel channel2 = raf2.getChannel(); channel2.write(bufs); raf2.close(); }
Example 11
Source File: ByteBufUtils.java From Distributed-KV with Apache License 2.0 | 5 votes |
/** * 读取文件数据到ByteBuf中 * @param channel * @param buffer * @throws IOException */ public static void read(FileChannel channel, ByteBuf buffer) throws IOException { // 建立bytebuffer以便写入文件 ByteBuffer tmpBuffer = ByteBuffer.allocate(buffer.capacity()); // 文件数据写入buffer channel.read(tmpBuffer); tmpBuffer.flip(); // bytebuffer写入byteBuf buffer.writeBytes(tmpBuffer); }
Example 12
Source File: SamplingProfiler.java From apm-agent-java with Apache License 2.0 | 5 votes |
private void readActivationEventsToBuffer(FileChannel activationEventsFileChannel, long eof, ByteBuffer byteBuffer) throws IOException { Buffer buf = byteBuffer; buf.clear(); long remaining = eof - activationEventsFileChannel.position(); activationEventsFileChannel.read(byteBuffer); buf.flip(); if (remaining < buf.capacity()) { buf.limit((int) remaining); } }
Example 13
Source File: PooledHeapByteBuf.java From netty-4.1.22 with Apache License 2.0 | 5 votes |
@Override public final int setBytes(int index, FileChannel in, long position, int length) throws IOException { checkIndex(index, length); index = idx(index); try { return in.read((ByteBuffer) internalNioBuffer().clear().position(index).limit(index + length), position); } catch (ClosedChannelException ignored) { return -1; } }
Example 14
Source File: MemFileChannelTest.java From jackcess with Apache License 2.0 | 5 votes |
private static void copy(FileChannel src, FileChannel dst, ByteBuffer bb) throws IOException { src.position(0L); while(true) { bb.clear(); if(src.read(bb) < 0) { break; } bb.flip(); dst.write(bb); } }
Example 15
Source File: GetChannel.java From java-core-learning-example with Apache License 2.0 | 5 votes |
@SuppressWarnings("resource") public static void main(String[] args) throws IOException { ////// // 写入文件 ////// // 从文件输出流获取FileChannel FileChannel fc = new FileOutputStream("data.txt").getChannel(); // 将含有字节序列的缓冲区写入文件通道 fc.write(ByteBuffer.wrap("Some Text".getBytes()));// 将已存在的字节数组包装到ByteBuffer // 关闭通道 fc.close(); ////// // 从文件尾部写入 ////// // 从RandomAccessFile获取FileChannel文件 fc = new RandomAccessFile("data.txt", "rw").getChannel(); // 文件指针指向文件尾部 fc.position(fc.size()); // 将含有字节序列的缓冲区写入文件通道 fc.write(ByteBuffer.wrap(" Some more".getBytes())); // 关闭通道 fc.close(); ////// // 读取文件 ////// // 从文件输出流获取FileChannel文件 fc = new FileInputStream("data.txt").getChannel(); // 分配ByteBuffer的大小 1K ByteBuffer buffer = ByteBuffer.allocate(BSIZE); // 将字节序列从此通道读入给定的缓冲区。 fc.read(buffer); // 反转缓冲区,为写入或读取做好准备 buffer.flip(); // 打印 while (buffer.hasRemaining()) { System.out.print((char)buffer.get()); } }
Example 16
Source File: Read.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
public static void main(String[] args) throws Exception { StringBuffer sb = new StringBuffer(); sb.setLength(4); blah = File.createTempFile("blah", null); blah.deleteOnExit(); initTestFile(blah); FileInputStream fis = new FileInputStream(blah); FileChannel c = fis.getChannel(); for (int x=0; x<1000; x++) { long offset = x * CHARS_PER_LINE; long expectedResult = offset / CHARS_PER_LINE; offset = expectedResult * CHARS_PER_LINE; ByteBuffer bleck = ByteBuffer.allocateDirect(CHARS_PER_LINE); c.read(bleck); for (int i=0; i<4; i++) { byte aByte = bleck.get(i); sb.setCharAt(i, (char)aByte); } int result = Integer.parseInt(sb.toString()); if (result != expectedResult) { err.println("I expected "+expectedResult); err.println("I got "+ result); throw new Exception("Read test failed"); } } c.close(); fis.close(); blah.delete(); }
Example 17
Source File: NIOUtils.java From incubator-hivemall with Apache License 2.0 | 5 votes |
public static void readFully(final FileChannel src, final ByteBuffer dst, final long position) throws IOException { while (dst.remaining() > 0) { if (-1 == src.read(dst, position + dst.position())) { throw new EOFException(); } } }
Example 18
Source File: ZipCompletionScanner.java From wildfly-core with GNU Lesser General Public License v2.1 | 4 votes |
private static void read(ByteBuffer bb, FileChannel ch) throws IOException { bb.clear(); ch.read(bb); bb.flip(); }
Example 19
Source File: PEHeader.java From davmail with GNU General Public License v2.0 | 4 votes |
public void read() throws IOException { FileChannel ch = m_pe.getChannel(); ByteBuffer head = ByteBuffer.allocate(350); head.order(ByteOrder.LITTLE_ENDIAN); ch.position(m_baseoffset); ch.read(head); head.position(0); PeMagic = head.getInt(); // System.out.println("MAGIC::: " + pemagic); Machine = head.getShort(); // 4 NumberOfSections = head.getShort(); // 6 TimeDateStamp = head.getInt(); // 8 PointerToSymbolTable = head.getInt(); // C NumberOfSymbols = head.getInt(); // 10 SizeOfOptionalHeader = head.getShort(); // 14 Characteristics = head.getShort(); // 16 // Optional Header Magic = head.getShort(); // 18 MajorLinkerVersion = head.get(); // 1a MinorLinkerVersion = head.get(); // 1b SizeOfCode = head.getInt(); // 1c SizeOfInitializedData = head.getInt(); // 20 SizeOfUninitializedData = head.getInt(); // 24 AddressOfEntryPoint = head.getInt(); // 28 BaseOfCode = head.getInt(); // 2c if (isPe32Plus()) { ImageBase = head.getLong(); // 34 } else { BaseOfData = head.getInt(); // // NT additional fields. // 30 ImageBase = head.getInt(); // 34 } SectionAlignment = head.getInt(); // 38 FileAlignment = head.getInt(); // 3c MajorOperatingSystemVersion = head.getShort(); // 40 MinorOperatingSystemVersion = head.getShort(); // 42 MajorImageVersion = head.getShort(); // 44 MinorImageVersion = head.getShort(); // 46 MajorSubsystemVersion = head.getShort(); // 48 MinorSubsystemVersion = head.getShort(); // 4a Reserved1 = head.getInt(); // 4c SizeOfImage = head.getInt(); // 50 SizeOfHeaders = head.getInt(); // 54 CheckSum = head.getInt(); // 58 Subsystem = head.getShort(); // 5c DllCharacteristics = head.getShort(); // 5e if (isPe32Plus()) { SizeOfStackReserve = head.getLong(); // 60 SizeOfStackCommit = head.getLong(); // 64 SizeOfHeapReserve = head.getLong(); // 68 SizeOfHeapCommit = head.getLong(); // 6c } else { SizeOfStackReserve = head.getInt(); // 60 SizeOfStackCommit = head.getInt(); // 64 SizeOfHeapReserve = head.getInt(); // 68 SizeOfHeapCommit = head.getInt(); // 6c } LoaderFlags = head.getInt(); // 70 NumberOfRvaAndSizes = head.getInt(); // 74 ExportDirectory_VA = head.getInt(); // 78 ExportDirectory_Size = head.getInt(); // 7c ImportDirectory_VA = head.getInt(); // 80 ImportDirectory_Size = head.getInt(); // 84 ResourceDirectory_VA = head.getInt(); // 88 ResourceDirectory_Size = head.getInt(); // 8c ExceptionDirectory_VA = head.getInt(); // 90 ExceptionDirectory_Size = head.getInt(); // 94 SecurityDirectory_VA = head.getInt(); // 98 SecurityDirectory_Size = head.getInt(); // 9c BaseRelocationTable_VA = head.getInt(); // a0 BaseRelocationTable_Size = head.getInt(); // a4 DebugDirectory_VA = head.getInt(); // a8 DebugDirectory_Size = head.getInt(); // ac ArchitectureSpecificData_VA = head.getInt(); // b0 ArchitectureSpecificData_Size = head.getInt(); // b4 RVAofGP_VA = head.getInt(); // b8 RVAofGP_Size = head.getInt(); // bc TLSDirectory_VA = head.getInt(); // c0 TLSDirectory_Size = head.getInt(); // c4 LoadConfigurationDirectory_VA = head.getInt(); // c8 LoadConfigurationDirectory_Size = head.getInt(); // cc BoundImportDirectoryinheaders_VA = head.getInt(); // d0 BoundImportDirectoryinheaders_Size = head.getInt(); // d4 ImportAddressTable_VA = head.getInt(); // d8 ImportAddressTable_Size = head.getInt(); // dc DelayLoadImportDescriptors_VA = head.getInt(); // e0 DelayLoadImportDescriptors_Size = head.getInt(); // e4 COMRuntimedescriptor_VA = head.getInt(); // e8 COMRuntimedescriptor_Size = head.getInt(); // ec }
Example 20
Source File: SNPLoader.java From systemsgenetics with GNU General Public License v3.0 | 4 votes |
public void loadGenotypes(SNP snp) throws IOException { byte[] allele1 = new byte[m_numIndividuals]; byte[] allele2 = new byte[m_numIndividuals]; int nrBytesToRead = m_numIndividuals * 2; long seekLoc = (long) snp.getId() * (long) nrBytesToRead; // byte[] alleles = new byte[nrBytesToRead]; // initiate buffer if it doesn't exist, or if location we're looking for is beyond the current map long seekEnd = seekLoc + (m_numIndividuals * 2); // System.out.println("Seekloc: " + seekLoc); // System.out.println("SeekEnd: " + seekEnd); if (mappedGenotypeHandle == null || seekLoc < currentGtMapStart || seekLoc > currentGtMapEnd || seekEnd > currentGtMapEnd) { // 32 megabytes worth of variants; (32*1048576)/(m_numIndividuals * 2) bytes if (mappedGenotypeHandle == null) { int bytesPerVariant = (m_numIndividuals * 2); int nrBytesPerBuffer = bytesPerVariant * numberOfVariantsInMemoryMap; //(32 * 1048576); while (nrBytesPerBuffer < 0) { numberOfVariantsInMemoryMap /= 2; nrBytesPerBuffer = bytesPerVariant * numberOfVariantsInMemoryMap; //(32 * 1048576); // System.out.println("WARNING: BUFFER OVERFLOW! Setting max number of variants in memory to " + numberOfVariantsInMemoryMap); // System.out.println("Buffer will be " + Gpio.humanizeFileSize(nrBytesPerBuffer) + " (" + nrBytesPerBuffer + "b)"); } // int remainder = nrBytesPerBuffer % bytesPerVariant; // nrBytesPerBuffer += remainder; gtmaplen = nrBytesPerBuffer; dsmaplen = nrBytesPerBuffer / 2; // System.out.println("bytes in buffer1: " + gtmaplen); // System.out.println("bytes in buffer2: " + dsmaplen); } // prevent overflow int maplentouse = gtmaplen; if (seekLoc + maplentouse > m_genotypehandle.length()) { maplentouse = (int) (m_genotypehandle.length() - seekLoc); } FileChannel gtChannel = m_genotypehandle.getChannel(); gtChannel.position(seekLoc); if (mappedGenotypeHandle == null || mappedGenotypeHandle.capacity() != maplentouse) { mappedGenotypeHandle = ByteBuffer.allocateDirect(maplentouse); } else { ((Buffer) mappedGenotypeHandle).clear(); } gtChannel.read(mappedGenotypeHandle); // mappedGenotypeHandle = m_genotypehandle.getChannel().map(FileChannel.MapMode.READ_ONLY, seekLoc, maplentouse); // mappedGenotypeHandle.load(); // bGt = new byte[(int) maplentouse]; // mappedGenotypeHandle.get(bGt); // mappedGenotypeHandle.g ((Buffer) mappedGenotypeHandle).flip(); currentGtMapStart = seekLoc; currentGtMapEnd = currentGtMapStart + maplentouse; // System.out.println("Reload buffer:\t" + seekLoc + "\tstart\t" + currentGtMapStart + "\tstop\t" + currentGtMapEnd + "\tlen\t" + maplentouse); } // if (m_genotypehandle.getFilePointer() != seekLoc) { // m_genotypehandle.seek(seekLoc); // } // m_genotypehandle.read(alleles, 0, bytesize); // mappedGenotypeHandle(alleles, 0, bytesize); // recalculate where we should be looking for this particular snp // mappedGenotypeHandle.get(alleles, offset, nrBytesToRead); // mappedGenotypeHandle.slice(); int offset = (int) (seekLoc - currentGtMapStart); // System.out.println("Seekloc: " + seekLoc); // System.out.println("offset: " + offset); // System.out.println("btr: " + nrBytesToRead); // System.out.println("capacity: " + mappedGenotypeHandle.capacity()); // System.out.println("remaining: " + mappedGenotypeHandle.remaining()); // System.out.println("limit:" + mappedGenotypeHandle.limit()); // System.out.println("position:" + mappedGenotypeHandle.position()); // System.out.println("req: " + m_numIndividuals); ((Buffer) mappedGenotypeHandle).position(offset); mappedGenotypeHandle.get(allele1, 0, m_numIndividuals); mappedGenotypeHandle.get(allele2, 0, m_numIndividuals); // System.arraycopy(bGt, offset, allele1, 0, m_numIndividuals); // System.arraycopy(bGt, offset + m_numIndividuals, allele2, 0, m_numIndividuals); // System.arraycopy(alleles, 0, allele1, 0, m_numIndividuals); // System.arraycopy(alleles, m_numIndividuals, allele2, 0, m_numIndividuals); // alleles = null; snp.setAlleles(allele1, allele2, m_isIncluded, m_isFemale); }