import java.io.*; class Test { public static void main(String args[]) throws Exception { RandomAccessFile f = new RandomAccessFile("t", "rw"); f.setLength(1024 * 1024 * 1024); } }
public void preBlow(String path, long maxSize, boolean preAllocate) throws IOException { RandomAccessFile raf = new RandomAccessFile(path, "rw"); try { raf.setLength(maxSize); } finally { raf.close(); } }
public void open(File outputFile) throws IOException { outputFile.mkdirs(); if (outputFile.exists()) { outputFile.delete(); } outputFile.createNewFile(); outputRAF = new RandomAccessFile(outputFile, "rw"); outputRAF.setLength(mappedFileSizeBytes); outputRAF.seek(mappedFileSizeBytes - 1); outputRAF.writeByte(0); outputRAF.seek(0); outputChannel = outputRAF.getChannel(); fileBuffer = outputChannel.map(FileChannel.MapMode.READ_WRITE, 0, mappedFileSizeBytes); }
RandomAccessFile f = new RandomAccessFile(fileName, "rw"); long length = f.length() - 1; do { length -= 1; f.seek(length); byte b = f.readByte(); } while(b != 10); f.setLength(length+1); f.close();
FileBytes(File file, String mode, int size) { if (file == null) { throw new NullPointerException("file cannot be null"); } if (mode == null) { mode = DEFAULT_MODE; } if (size < 0) { throw new IllegalArgumentException("size must be positive"); } this.file = file; this.mode = mode; this.size = size; try { this.randomAccessFile = new RandomAccessFile(file, mode); if (size > randomAccessFile.length()) { randomAccessFile.setLength(size); } } catch (IOException e) { throw new RuntimeException(e); } }
private void preblow() { this.dirHolder.incrementTotalOplogSize(this.maxOplogSize); final OplogFile olf = getOLF(); try { olf.raf.setLength(this.maxOplogSize); olf.raf.seek(0); } catch (IOException ignore) { // TODO: need a warning since this can impact perf. // I don't think I need any of this. If setLength throws then // the file is still ok. } }
@Private static RandomAccessFile initializeFromFile(File file, boolean forceLegacy) throws IOException { if (!file.exists()) { // Use a temp file so we don't leave a partially-initialized file. File tempFile = new File(file.getPath() + ".tmp"); RandomAccessFile raf = open(tempFile); try { raf.setLength(INITIAL_LENGTH); raf.seek(0); if (forceLegacy) { raf.writeInt(INITIAL_LENGTH); } else { raf.writeInt(VERSIONED_HEADER); raf.writeLong(INITIAL_LENGTH); } } finally { raf.close(); } // A rename is atomic. if (!tempFile.renameTo(file)) { throw new IOException("Rename failed!"); } } return open(file); }
protected long readUntil(long position) throws IOException { if (position < _length) { return position; } if (_foundEOF) { return _length; } long lengthToRead = position - _length; _randomAccessFileCache.seek(_length); byte[] buffer = new byte[1024]; while (lengthToRead > 0) { int bytesRead = _inputStream.read( buffer, 0, (int)Math.min(lengthToRead, buffer.length)); if (bytesRead == -1) { _foundEOF = true; return _length; } _randomAccessFileCache.setLength( _randomAccessFileCache.length() + bytesRead); _randomAccessFileCache.write(buffer, 0, bytesRead); lengthToRead -= bytesRead; _length += bytesRead; } return position; }
/** * @param name File path. * @param size Expected file size. If size is 0, the existing size will be used. * @throws IOException If failed to open the file or memory-map it. */ public MappedFile(File name, long size) throws IOException { file = new RandomAccessFile(name, "rw"); try { if (size == 0) size = file.length(); else file.setLength(size); addr = map(file, MAP_RW, 0, size); this.size = size; } catch (IOException e) { file.close(); throw e; } }
/** * Creates a new ZIP OutputStream writing to a File. Will use * random access if possible. * @param file the file to zip to * @since 1.14 * @throws IOException on error */ public ZipOutputStream(File file) throws IOException { super(null); RandomAccessFile _raf = null; try { _raf = new RandomAccessFile(file, "rw"); _raf.setLength(0); } catch (IOException e) { if (_raf != null) { try { _raf.close(); } catch (IOException inner) { // NOPMD // ignore } _raf = null; } out = new FileOutputStream(file); } raf = _raf; }
AbstractLongMap(int size,int idSize,int foffsetSize,int valueSize) throws FileNotFoundException, IOException { assert idSize == 4 || idSize == 8; assert foffsetSize == 4 || foffsetSize == 8; keys = (size * 4L) / 3L; ID_SIZE = idSize; FOFFSET_SIZE = foffsetSize; KEY_SIZE = ID_SIZE; VALUE_SIZE = valueSize; ENTRY_SIZE = KEY_SIZE + VALUE_SIZE; fileSize = keys * ENTRY_SIZE; tempFile = File.createTempFile("NBProfiler", ".map"); // NOI18N RandomAccessFile file = new RandomAccessFile(tempFile, "rw"); // NOI18N if (Boolean.getBoolean("org.netbeans.lib.profiler.heap.zerofile")) { // NOI18N byte[] zeros = new byte[512*1024]; while(file.length()<fileSize) { file.write(zeros); } file.write(zeros,0,(int)(fileSize-file.length())); } file.setLength(fileSize); setDumpBuffer(file); tempFile.deleteOnExit(); }
/** * Open a channel for the given file * For windows NTFS and some old LINUX file system, set preallocate to true and initFileSize * with one value (for example 512 * 1025 *1024 ) can improve the kafka produce performance. * @param file File path * @param mutable mutable * @param fileAlreadyExists File already exists or not * @param initFileSize The size used for pre allocate file, for example 512 * 1025 *1024 * @param preallocate Pre-allocate file or not, gotten from configuration. */ private static FileChannel openChannel(File file, boolean mutable, boolean fileAlreadyExists, int initFileSize, boolean preallocate) throws IOException { if (mutable) { if (fileAlreadyExists || !preallocate) { return FileChannel.open(file.toPath(), StandardOpenOption.CREATE, StandardOpenOption.READ, StandardOpenOption.WRITE); } else { RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw"); randomAccessFile.setLength(initFileSize); return randomAccessFile.getChannel(); } } else { return FileChannel.open(file.toPath()); } }
@SuppressWarnings("unchecked") public void open(File inputFile) throws IOException { deserializer = (Deserializer<OUT>) (readAsByteArray ? new ByteArrayDeserializer() : new TupleDeserializer()); inputFile.getParentFile().mkdirs(); if (inputFile.exists()) { inputFile.delete(); } inputFile.createNewFile(); inputRAF = new RandomAccessFile(inputFile, "rw"); inputRAF.setLength(mappedFileSizeBytes); inputRAF.seek(mappedFileSizeBytes - 1); inputRAF.writeByte(0); inputRAF.seek(0); inputChannel = inputRAF.getChannel(); fileBuffer = inputChannel.map(FileChannel.MapMode.READ_WRITE, 0, mappedFileSizeBytes); }
private synchronized void writeChunkMetaData(int lastChunkId, TreeSet<Long> removedPages) { try { chunkMetaData.setLength(0); chunkMetaData.seek(0); chunkMetaData.writeInt(lastChunkId); chunkMetaData.writeInt(removedPages.size()); for (long pos : removedPages) { chunkMetaData.writeLong(pos); } chunkMetaData.writeInt(hashCodeToHostIdMap.size()); for (String hostId : hashCodeToHostIdMap.values()) { chunkMetaData.writeUTF(hostId); } // chunkMetaData.setLength(4 + 4 + removedPages.size() * 8); chunkMetaData.getFD().sync(); } catch (IOException e) { throw panic(DataUtils.newIllegalStateException(DataUtils.ERROR_WRITING_FAILED, "Failed to writeChunkMetaData", e)); } }
RandomAccessFile raf = new RandomAccessFile(file, "rw"); if (skip < 0 || skip > raf.length()) { System.out.println("skip error"); return; raf.setLength(raf.length() + b.length); for (long i = raf.length() - 1; i > b.length + skip - 1; i--) { raf.seek(i - b.length); byte temp = raf.readByte(); raf.seek(i); raf.writeByte(temp); raf.seek(skip); raf.write(b); raf.close(); } catch (Exception e) { e.printStackTrace();