List of usage examples for java.nio.channels FileChannel read
public final long read(ByteBuffer[] dsts) throws IOException
From source file:org.alfresco.repo.search.impl.lucene.index.IndexInfo.java
private boolean checkVersion(FileChannel channel) throws IOException { if (channel.size() > 0) { channel.position(0);/*from ww w .ja v a2 s .co m*/ ByteBuffer buffer; if (useNIOMemoryMapping) { MappedByteBuffer mbb = channel.map(MapMode.READ_ONLY, 0, 8); mbb.load(); buffer = mbb; } else { buffer = ByteBuffer.wrap(new byte[8]); channel.read(buffer); buffer.position(0); } buffer.position(0); long onDiskVersion = buffer.getLong(); return (version == onDiskVersion); } return (version == 0); }
From source file:org.alfresco.repo.search.impl.lucene.index.IndexInfo.java
private void setStatusFromFile(FileChannel channel) throws IOException { if (channel.size() > 0) { channel.position(0);/*from w w w . j av a2 s .c o m*/ ByteBuffer buffer; if (useNIOMemoryMapping) { MappedByteBuffer mbb = channel.map(MapMode.READ_ONLY, 0, channel.size()); mbb.load(); buffer = mbb; } else { buffer = ByteBuffer.wrap(new byte[(int) channel.size()]); channel.read(buffer); buffer.position(0); } buffer.position(0); long onDiskVersion = buffer.getLong(); if (version != onDiskVersion) { CRC32 crc32 = new CRC32(); crc32.update((int) (onDiskVersion >>> 32) & 0xFFFFFFFF); crc32.update((int) (onDiskVersion >>> 0) & 0xFFFFFFFF); int size = buffer.getInt(); crc32.update(size); LinkedHashMap<String, IndexEntry> newIndexEntries = new LinkedHashMap<String, IndexEntry>(); // Not all state is saved some is specific to this index so we // need to add the transient stuff. // Until things are committed they are not shared unless it is // prepared for (int i = 0; i < size; i++) { String indexTypeString = readString(buffer, crc32); IndexType indexType; try { indexType = IndexType.valueOf(indexTypeString); } catch (IllegalArgumentException e) { throw new IOException("Invalid type " + indexTypeString); } String name = readString(buffer, crc32); String parentName = readString(buffer, crc32); String txStatus = readString(buffer, crc32); TransactionStatus status; try { status = TransactionStatus.valueOf(txStatus); } catch (IllegalArgumentException e) { throw new IOException("Invalid status " + txStatus); } String mergeId = readString(buffer, crc32); long documentCount = buffer.getLong(); crc32.update((int) (documentCount >>> 32) & 0xFFFFFFFF); crc32.update((int) (documentCount >>> 0) & 0xFFFFFFFF); long deletions = buffer.getLong(); crc32.update((int) (deletions >>> 32) & 0xFFFFFFFF); crc32.update((int) (deletions >>> 0) & 0xFFFFFFFF); byte deleteOnlyNodesFlag = buffer.get(); crc32.update(deleteOnlyNodesFlag); boolean isDeletOnlyNodes = deleteOnlyNodesFlag == 1; if (!status.isTransient()) { newIndexEntries.put(name, new IndexEntry(indexType, name, parentName, status, mergeId, documentCount, deletions, isDeletOnlyNodes)); } } long onDiskCRC32 = buffer.getLong(); if (crc32.getValue() == onDiskCRC32) { for (IndexEntry entry : indexEntries.values()) { if (entry.getStatus().isTransient()) { newIndexEntries.put(entry.getName(), entry); } } version = onDiskVersion; indexEntries = newIndexEntries; } else { throw new IOException("Invalid file check sum"); } } } }
From source file:org.sakaiproject.assignment.tool.AssignmentAction.java
private byte[] readIntoBytes(InputStream zin, String fName, long length) throws IOException { byte[] buffer = new byte[4096]; File f = File.createTempFile("asgnup", "tmp"); FileOutputStream fout = new FileOutputStream(f); try {/* w w w .j a v a 2 s . co m*/ int len; while ((len = zin.read(buffer)) > 0) { fout.write(buffer, 0, len); } zin.close(); } finally { try { fout.close(); // The file channel needs to be closed before the deletion. } catch (IOException ioException) { M_log.warn(this + "readIntoBytes: problem closing FileOutputStream " + ioException.getMessage()); } } FileInputStream fis = new FileInputStream(f); FileChannel fc = fis.getChannel(); byte[] data = null; try { data = new byte[(int) (fc.size())]; // fc.size returns the size of the file which backs the channel ByteBuffer bb = ByteBuffer.wrap(data); fc.read(bb); } finally { try { fc.close(); // The file channel needs to be closed before the deletion. } catch (IOException ioException) { M_log.warn(this + "readIntoBytes: problem closing FileChannel " + ioException.getMessage()); } try { fis.close(); // The file inputstream needs to be closed before the deletion. } catch (IOException ioException) { M_log.warn(this + "readIntoBytes: problem closing FileInputStream " + ioException.getMessage()); } } //remove the file f.delete(); return data; }