public void write(Tag tag, RandomAccessFile raf, RandomAccessFile tempRaf) throws CannotWriteException, IOException { FileChannel fc = raf.getChannel(); int oldTagSize = 0; if (tagExists(fc)) { // read the length if (!canOverwrite(raf)) throw new CannotWriteException("Overwritting of this kind of ID3v2 tag not supported yet"); fc.position(6); ByteBuffer buf = ByteBuffer.allocate(4); fc.read(buf); oldTagSize = (buf.get(0) & 0xFF) << 21; oldTagSize += (buf.get(1) & 0xFF) << 14; oldTagSize += (buf.get(2) & 0xFF) << 7; oldTagSize += buf.get(3) & 0xFF; oldTagSize += 10; // System.err.println("Old tag size: "+oldTagSize); int newTagSize = tc.getTagLength(tag); if (oldTagSize >= newTagSize) { // replace // System.err.println("Old ID32v Tag found, replacing the old // tag"); fc.position(0); fc.write(tc.convert(tag, oldTagSize - newTagSize)); // ID3v2 Tag Written return; } } // create new tag with padding // System.err.println("Creating a new ID3v2 Tag"); fc.position(oldTagSize); if (fc.size() > 15 * 1024 * 1024) { FileChannel tempFC = tempRaf.getChannel(); tempFC.position(0); tempFC.write(tc.convert(tag, Id3v2TagCreator.DEFAULT_PADDING)); tempFC.transferFrom(fc, tempFC.position(), fc.size() - oldTagSize); fc.close(); } else { ByteBuffer[] content = new ByteBuffer[2]; content[1] = ByteBuffer.allocate((int) fc.size()); fc.read(content[1]); content[1].rewind(); content[0] = tc.convert(tag, Id3v2TagCreator.DEFAULT_PADDING); fc.position(0); fc.write(content); } }
private void insertRecord(int recordPosition, long value) throws IOException { try { FileChannel channel = getFileChannel(); long previousPosition = channel.position(); channel.position(RECORD_SIZE * recordPosition); int trail = (int) (channel.size() - channel.position()); ByteBuffer trailBuffer = null; if (trail > 0) { trailBuffer = ByteBuffer.allocate(trail); channel.read(trailBuffer); trailBuffer.flip(); } ByteBuffer buffer = ByteBuffer.allocate(RECORD_SIZE); buffer.put(Record.IN_USE.byteValue()); buffer.putLong(value); buffer.flip(); channel.position(RECORD_SIZE * recordPosition); channel.write(buffer); if (trail > 0) { channel.write(trailBuffer); } channel.position(previousPosition); } catch (IOException e) { throw new RuntimeException(e); } }
@Test public void testAppend() throws IOException { RegularFile file = regularFile(0); FileChannel channel = channel(file, WRITE, APPEND); assertEquals(0, channel.position()); ByteBuffer buf = buffer("1234567890"); ByteBuffer buf2 = buffer("1234567890"); assertEquals(10, channel.write(buf)); assertEquals(10, channel.position()); buf.flip(); channel.position(0); assertEquals(20, channel.write(new ByteBuffer[] {buf, buf2})); assertEquals(30, channel.position()); buf.flip(); buf2.flip(); channel.position(0); assertEquals(20, channel.write(new ByteBuffer[] {buf, buf2}, 0, 2)); assertEquals(50, channel.position()); buf.flip(); channel.position(0); assertEquals(10, channel.write(buf, 5)); assertEquals(60, channel.position()); buf.flip(); channel.position(0); assertEquals(10, channel.transferFrom(new ByteBufferChannel(buf), 0, 10)); assertEquals(70, channel.position()); }
public int write(ByteBuffer src) throws IOException { if (filePointer >= length && filePointer > maxLength) { // may need to extend and create files long oldFilePointer = filePointer; long x = length - (length % maxLength) + maxLength; for (; x < filePointer; x += maxLength) { if (x > length) { // expand the file size position(x - 1); write(ByteBuffer.wrap(new byte[1])); } filePointer = oldFilePointer; } } long offset = filePointer % maxLength; int len = src.remaining(); FileChannel channel = getFileChannel(); channel.position(offset); int l = (int) Math.min(len, maxLength - offset); if (l == len) { l = channel.write(src); } else { int oldLimit = src.limit(); src.limit(src.position() + l); l = channel.write(src); src.limit(oldLimit); } filePointer += l; length = Math.max(length, filePointer); return l; }
@Override public boolean renameTo(File dest) throws IOException { if (dest == null) { throw new NullPointerException("dest"); } if (byteBuf == null) { // empty file dest.createNewFile(); isRenamed = true; return true; } int length = byteBuf.readableBytes(); FileOutputStream outputStream = new FileOutputStream(dest); FileChannel fileChannel = outputStream.getChannel(); int written = 0; if (byteBuf.nioBufferCount() == 1) { ByteBuffer byteBuffer = byteBuf.nioBuffer(); while (written < length) { written += fileChannel.write(byteBuffer); } } else { ByteBuffer[] byteBuffers = byteBuf.nioBuffers(); while (written < length) { written += fileChannel.write(byteBuffers); } } fileChannel.force(false); fileChannel.close(); outputStream.close(); isRenamed = true; return written == length; }
/** * 使用FileChannel写2G大文件 * * @throws IOException */ public static void writeBiFileByFileChannel() throws IOException { long start = System.currentTimeMillis(); File fcFile = new File("fc.dat"); if (fcFile.exists()) { fcFile.delete(); } RandomAccessFile raf = new RandomAccessFile(fcFile, "rw"); FileChannel fileChannel = raf.getChannel(); byte[] data = null; long len = LEN; ByteBuffer byteBuffer = ByteBuffer.allocate(DATA_CHUNK); int dataChunk = DATA_CHUNK / (1024 * 1024); while (len >= DATA_CHUNK) { System.out.println("write a data chunk:" + dataChunk + "MB"); byteBuffer.clear(); data = new byte[DATA_CHUNK]; for (int i = 0; i < DATA_CHUNK; i++) { byteBuffer.put(data[i]); } data = null; byteBuffer.flip(); fileChannel.write(byteBuffer); fileChannel.force(true); len -= DATA_CHUNK; } if (len > 0) { System.out.println("write rest data chunk:" + len + "B"); byteBuffer = ByteBuffer.allocateDirect((int) len); data = new byte[(int) len]; for (int i = 0; i < len; i++) { byteBuffer.put(data[i]); } byteBuffer.flip(); fileChannel.write(byteBuffer); fileChannel.force(true); data = null; } // 关闭文件和通道流 fileChannel.close(); raf.close(); long end = System.currentTimeMillis(); System.out.println(String.format("===>写2G文件 " + fcFile.getName() + " 耗时:%s毫秒", end - start)); }
public void write(FileOutputStream fos) throws IOException { FileChannel chan = fos.getChannel(); // Create ByteBuffer for header in case the start of our // ByteBuffer isn't actually memory-mapped ByteBuffer hdr = ByteBuffer.allocate(Header.writtenSize()); hdr.order(ByteOrder.LITTLE_ENDIAN); header.write(hdr); hdr.rewind(); chan.write(hdr); buf.position(Header.writtenSize()); chan.write(buf); chan.force(true); chan.close(); }
public static boolean createFile(File file, long fileLength) { FileOutputStream fos = null; try { if (!file.exists()) { boolean ret = file.createNewFile(); if (!ret) return false; } long batchSize = 0; batchSize = fileLength; if (fileLength > KBSIZE) { batchSize = KBSIZE; } if (fileLength > MBSIZE1) { batchSize = MBSIZE1; } if (fileLength > MBSIZE10) { batchSize = MBSIZE10; } long count = fileLength / batchSize; long last = fileLength % batchSize; fos = new FileOutputStream(file); FileChannel fileChannel = fos.getChannel(); for (int i = 0; i < count; i++) { ByteBuffer buffer = ByteBuffer.allocate((int) batchSize); fileChannel.write(buffer); } ByteBuffer buffer = ByteBuffer.allocate((int) last); fileChannel.write(buffer); fos.close(); return true; } catch (IOException e) { e.printStackTrace(); } finally { try { if (fos != null) { fos.close(); } } catch (IOException e) { e.printStackTrace(); } } return false; }
public void write( String path, ReadableByteChannel data, ByteBuffer temporaryBuffer, boolean hasData) throws IOException { try { File file = new File(basePath, path); RandomAccessFile randomAccessFile = null; try { file.getParentFile().mkdirs(); randomAccessFile = new RandomAccessFile(file, "rw"); if (hasData) { FileChannel channel = randomAccessFile.getChannel(); while (data.read(temporaryBuffer) >= 0) { temporaryBuffer.flip(); channel.write(temporaryBuffer); temporaryBuffer.clear(); } } } finally { if (randomAccessFile != null) { randomAccessFile.close(); } } } catch (Throwable t) { t.printStackTrace(); throw new IOException(t); } }
/** * 使用FileChannel写文件 * * @throws IOException */ public static void writeByFileChannel() throws IOException { File file = new File("FileChannelWriterTest.txt"); if (file.exists()) { file.delete(); } FileOutputStream fos = new FileOutputStream(file); // 得到文件通道 FileChannel channel = fos.getChannel(); // 指定大小为1024的缓冲区 ByteBuffer buffer = ByteBuffer.allocate(1024); // 要写入文件的字符串 String greeting = "Hello Java NIO!"; // 把以上字符串逐字放入缓冲区 // for(int i = 0; i < greeting.length(); i++){ // buffer.putChar(greeting.charAt(i)); // } // 把以上字符一次性放入缓冲区 buffer.put(greeting.getBytes()); // 反转buffer buffer.flip(); // 缓冲区数据写入文件中 channel.write(buffer); // 关闭文件和通道流 channel.close(); fos.close(); System.out.println("Write file " + file.getName() + " Successfully!"); }
public void save(int pageNo, B_Tree.Page<Key> page) { try { ByteBuffer buffer = ByteBuffer.allocate(pageSize); List<B_Tree.KeyPointer<Key>> ptrs = page.keyPointers; boolean isBranch = !ptrs.isEmpty() && ptrs.get(0).t2 instanceof B_Tree.Branch; buffer.putChar(isBranch ? INTERNAL : LEAF); buffer.putInt(ptrs.size()); for (B_Tree.KeyPointer<Key> keyPtr : ptrs) { keyAccessor.write(buffer, keyPtr.t1); if (keyPtr.t2 instanceof B_Tree.Branch) { int branch = ((B_Tree.Branch) keyPtr.t2).branch; buffer.putInt(branch); } else if (keyPtr.t2 instanceof B_Tree.Leaf) { @SuppressWarnings("unchecked") Value value = ((B_Tree.Leaf<Value>) keyPtr.t2).value; valueAccessor.write(buffer, value); } } buffer.flip(); channel.write(buffer, pageNo * pageSize); } catch (IOException ex) { throw new RuntimeException(ex); } }
private static void writeFully(FileChannel file, long pos, ByteBuffer src) throws IOException { int off = 0; do { int len = file.write(src, pos + off); off += len; } while (src.remaining() > 0); }
public static DataFileAccessorImpl create( final File file, final Date startDate, final Date endDate) throws Exception { logger.debug("Creating new file: {}", file); if (!file.createNewFile()) { throw new IllegalStateException( String.format("Unable to create file %s, already exists", file)); } final FileOutputStream out = new FileOutputStream(file); try { final FileChannel channel = out.getChannel(); final ByteBuffer buffer = ByteBuffer.allocate(100); buffer.putInt(0x1202); // magic marker buffer.putInt(0x0101); // version buffer.putLong(startDate.getTime()); // start timestamp buffer.putLong(endDate.getTime()); // end timestamp buffer.flip(); while (buffer.hasRemaining()) { final int rc = channel.write(buffer); logger.debug("Header written - {} bytes", rc); } return new DataFileAccessorImpl(file); } finally { out.close(); } }
@Override public void close() throws IOException, InterruptedException { try { m_outstandingWriteTasksLock.lock(); try { while (m_outstandingWriteTasks.get() > 0) { m_noMoreOutstandingWriteTasksCondition.await(); } } finally { m_outstandingWriteTasksLock.unlock(); } m_syncTask.cancel(false); m_channel.force(false); } finally { m_bytesAllowedBeforeSync.release(m_bytesWrittenSinceLastSync.getAndSet(0)); } m_channel.position(8); ByteBuffer completed = ByteBuffer.allocate(1); if (m_writeFailed) { completed.put((byte) 0).flip(); } else { completed.put((byte) 1).flip(); } m_channel.write(completed); m_channel.force(false); m_channel.close(); if (m_onCloseHandler != null) { m_onCloseHandler.run(); } }
/** * transfer Stream * * @param ins * @param targetChannel */ private static void transferStream(InputStream ins, FileChannel targetChannel) { ByteBuffer byteBuffer = ByteBuffer.allocate(1024 * 10); ReadableByteChannel rbcInst = Channels.newChannel(ins); try { while (-1 != (rbcInst.read(byteBuffer))) { byteBuffer.flip(); targetChannel.write(byteBuffer); byteBuffer.clear(); } } catch (IOException ioe) { ioe.printStackTrace(); } finally { if (null != rbcInst) { try { rbcInst.close(); } catch (IOException e) { e.printStackTrace(); } } if (null != targetChannel) { try { targetChannel.close(); } catch (IOException e) { e.printStackTrace(); } } } }
public static void writeByteBuffer(ByteBuffer bbuf, String filename) { // Write bbuf to filename File file; try { // Get log file String logfile = "C:\\" + filename + ".txt"; file = new File(logfile); boolean exists = file.exists(); if (!exists) { // create a new, empty node file try { file = new File(logfile); boolean success = file.createNewFile(); } catch (IOException e) { System.out.println("Create Event Log file failed!"); } } try { // Create a writable file channel FileChannel wChannel = new FileOutputStream(file, true).getChannel(); // Write the ByteBuffer contents; the bytes between the ByteBuffer's // position and the limit is written to the file wChannel.write(bbuf); // Close the file wChannel.close(); } catch (IOException e) { } } catch (java.lang.Exception e) { } }
/* * Creates a file at filename if file doesn't exist. Writes * value to that file using FileChannel. */ public static void writeToFile(File filename, String value, String hideCommand) throws IOException, InterruptedException { if (!hideCommand.trim().equals("")) { // unhideFile(filename); } if (!filename.exists()) { filename.createNewFile(); } byte[] bytes = value.getBytes(); ByteBuffer buffer = ByteBuffer.allocate(bytes.length); for (int i = 0; i < bytes.length; i++) { buffer.put(bytes[i]); } buffer.rewind(); try { fileWriter = new FileOutputStream(filename).getChannel(); fileWriter.write(buffer); } finally { fileWriter.close(); } if (!hideCommand.trim().equals("")) { // hideFile(filename); } }
@Override public void clear() { resizeLock.writeLock().lock(); try { synchronized (entries) { synchronized (freeList) { // wait until all readers are done reading file entries for (FileEntry fe : entries.values()) fe.waitUnlocked(); for (FileEntry fe : freeList) fe.waitUnlocked(); // clear in-memory state entries.clear(); freeList.clear(); // reset file if (trace) log.tracef("Truncating file, current size is %d", filePos); channel.truncate(0); channel.write(ByteBuffer.wrap(MAGIC), 0); filePos = MAGIC.length; } } } catch (Exception e) { throw new PersistenceException(e); } finally { resizeLock.writeLock().unlock(); } }
/* @see IRandomAccess.write(ByteBuffer, int, int) */ public void write(ByteBuffer buf, int off, int len) throws IOException { writeSetup(len); buf.limit(off + len); buf.position(off); position += channel.write(buf, position); buffer = null; }
@Override public boolean putSubpiece(int piece, byte[] bs) throws IOException { // First check if the piece is correct. // If we were paranoid we could copy the array first. synchronized (bitfield) { if (bitfield.get(piece)) { return true; // No need to store twice. } else { bitfield.set(piece, false); needed--; } } assert (this.length != 0); int start = piece * metainfo.getSubpieceSize(0); FileChannel fc = getFileChannel(); long raflen = this.length; while (start > raflen) { start -= raflen; } ByteBuffer bb = ByteBuffer.wrap(bs); fc.position(start); while (bb.hasRemaining()) { fc.write(bb); } return true; }
private void writeHeader(LogSegment segment, byte[] buf) throws IOException { FileChannel channel = segment.getView().getSecond(); ByteBuffer buffer = ByteBuffer.wrap(buf); while (buffer.hasRemaining()) { channel.write(buffer, 0); } }
public static void main(String[] args) throws IOException { System.err.println(df.format(new Date(System.currentTimeMillis()))); File rfile = new File("D:/TDDOWNLOAD/17173_SC2-WingsOfLiberty-zhCN-Installer.rar"); File wfile = new File("E:/17173_SC2-WingsOfLiberty-zhCN-InstallerCopy.rar"); FileChannel rchannel = new FileInputStream(rfile).getChannel(); FileChannel wchannel = new FileOutputStream(wfile).getChannel(); ByteBuffer[] buffers = new ByteBuffer[1024]; for (int i = 0; i < buffers.length; i++) { buffers[i] = ByteBuffer.allocate(1024); } boolean isLoop = true; while (isLoop) { for (ByteBuffer buffer : buffers) { buffer.rewind(); } isLoop = rchannel.read(buffers) != -1L; for (ByteBuffer buffer : buffers) { buffer.flip(); } wchannel.write(buffers); } rchannel.close(); wchannel.close(); }
public static void copyFile(File srcFile, File destFile) throws Exception { int bufferSize = 2048; FileInputStream in = new FileInputStream(srcFile); FileOutputStream out = new FileOutputStream(destFile); FileChannel inChannel = in.getChannel(); FileChannel outChannel = out.getChannel(); ByteBuffer buffer = null; int length = -1; try { while (true) { if (inChannel.position() == inChannel.size()) { // finish copying break; } else if (inChannel.size() - inChannel.position() < length) { // copy last chunk of data length = (int) (inChannel.size() - inChannel.position()); } else { length = bufferSize; } buffer = ByteBuffer.allocateDirect(length); inChannel.read(buffer); buffer.flip(); outChannel.write(buffer); outChannel.force(false); } } finally { _close(inChannel); _close(in); _close(outChannel); _close(out); } }
/** * 监测复制进度(留意buffer的大小,对速度有很大影响) * * @param source * @param target */ public static void nioBufferCopy(File source, File target) { FileChannel in = null; FileChannel out = null; FileInputStream inStream = null; FileOutputStream outStream = null; try { inStream = new FileInputStream(source); outStream = new FileOutputStream(target); in = inStream.getChannel(); out = outStream.getChannel(); ByteBuffer buffer = ByteBuffer.allocate(4096); while (in.read(buffer) != -1) { buffer.flip(); out.write(buffer); buffer.clear(); } } catch (IOException e) { e.printStackTrace(); } finally { close(inStream); close(in); close(outStream); close(out); } }
public static void saveFile(File file, String content, String charsetName) { // if (Utils.isEmpty(fileName) || Utils.isEmpty(content)) { // return; // } // logger.info("save file:" + fileName + " charset:" + charsetName); file.getParentFile().mkdirs(); Charset cs; if (null == charsetName || "".equals(charsetName)) { cs = Charset.defaultCharset(); } else { cs = Charset.forName(charsetName); } CharsetEncoder encoder = cs.newEncoder(); FileOutputStream os = null; FileChannel out = null; try { os = new FileOutputStream(file); out = os.getChannel(); out.write(encoder.encode(CharBuffer.wrap(content))); } catch (CharacterCodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { close(out); close(os); } }
public static void createTestFiles() { try { System.out.println("Creating test files ... "); Random rand = new Random(); String rootname = "f-"; long[] sizes = {0, 1, 50000000}; File testdir = new File(dirname); FileUtil.mkdirs(testdir); for (int i = 0; i < sizes.length; i++) { long size = sizes[i]; File file = new File(testdir, rootname + String.valueOf(size)); System.out.println(file.getName() + "..."); FileChannel fc = new RandomAccessFile(file, "rw").getChannel(); long position = 0; while (position < size) { long remaining = size - position; if (remaining > 1024000) remaining = 1024000; byte[] buffer = new byte[new Long(remaining).intValue()]; rand.nextBytes(buffer); ByteBuffer bb = ByteBuffer.wrap(buffer); position += fc.write(bb); } fc.close(); } System.out.println("DONE\n"); } catch (Exception e) { Debug.printStackTrace(e); } }
public static void main(String args[]) throws Exception { String inputFile = "samplein.txt"; String outputFile = "sampleout.txt"; RandomAccessFile inf = new RandomAccessFile(inputFile, "r"); RandomAccessFile outf = new RandomAccessFile(outputFile, "rw"); long inputLength = new File(inputFile).length(); FileChannel inc = inf.getChannel(); FileChannel outc = outf.getChannel(); MappedByteBuffer inputData = inc.map(FileChannel.MapMode.READ_ONLY, 0, inputLength); Charset latin1 = Charset.forName("ISO-8859-1"); CharsetDecoder decoder = latin1.newDecoder(); CharsetEncoder encoder = latin1.newEncoder(); CharBuffer cb = decoder.decode(inputData); // Process char data here ByteBuffer outputData = encoder.encode(cb); outc.write(outputData); inf.close(); outf.close(); }
/** * Sets the version for the given neostore file in {@code storeDir}. * * @param storeDir the store dir to locate the neostore file in. * @param version the version to set. * @return the previous version before writing. */ public static long setVersion(String storeDir, long version) { RandomAccessFile file = null; try { file = new RandomAccessFile(new File(storeDir, NeoStore.DEFAULT_NAME), "rw"); FileChannel channel = file.getChannel(); channel.position(RECORD_SIZE * 2 + 1 /*inUse*/); ByteBuffer buffer = ByteBuffer.allocate(8); channel.read(buffer); buffer.flip(); long previous = buffer.getLong(); channel.position(RECORD_SIZE * 2 + 1 /*inUse*/); buffer.clear(); buffer.putLong(version).flip(); channel.write(buffer); return previous; } catch (IOException e) { throw new RuntimeException(e); } finally { try { if (file != null) file.close(); } catch (IOException e) { throw new RuntimeException(e); } } }
void save() throws Exception { ByteBuffer buffer = ByteBuffer.allocate(1); buffer.put(rootPage.getUnique() ? (byte) 1 : (byte) 0); buffer.position(0); raFile.write(buffer); ((FileIndexNode) rootPage).save(); }
/** * Write a file with the content assuming UTF8. * * @param content The content * @param file The file to write * @throws IOException e */ public static void writeFile(String content, File file) throws IOException { FileOutputStream outputStream = new FileOutputStream(file); FileChannel fc = outputStream.getChannel(); ByteBuffer buffer = ByteBuffer.wrap(Utf8.toBytes(content)); fc.write(buffer); outputStream.close(); }