Java Code Examples for java.io.RandomAccessFile#setLength()
The following examples show how to use
java.io.RandomAccessFile#setLength() .
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: HttpServerTest.java From msf4j with Apache License 2.0 | 8 votes |
@Test public void testChunkAggregatedUpload() throws IOException { //create a random file to be uploaded. int size = 69 * 1024; File fname = new File(tmpFolder, "testChunkAggregatedUpload.txt"); fname.createNewFile(); RandomAccessFile randf = new RandomAccessFile(fname, "rw"); randf.setLength(size); randf.close(); //test chunked upload HttpURLConnection urlConn = request("/test/v1/aggregate/upload", HttpMethod.PUT); urlConn.setChunkedStreamingMode(1024); Files.copy(Paths.get(fname.toURI()), urlConn.getOutputStream()); assertEquals(200, urlConn.getResponseCode()); assertEquals(size, Integer.parseInt(getContent(urlConn).split(":")[1].trim())); urlConn.disconnect(); fname.delete(); }
Example 2
Source File: PythonSender.java From Flink-CEPplus with Apache License 2.0 | 6 votes |
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); }
Example 3
Source File: FileBuffer.java From cyberduck with GNU General Public License v3.0 | 6 votes |
@Override public void truncate(final Long length) { this.length = length; if(temporary.exists()) { try { final RandomAccessFile file = random(); if(length < file.length()) { // Truncate current file.setLength(length); } } catch(IOException e) { log.warn(String.format("Failure truncating file %s to %d", temporary, length)); } } }
Example 4
Source File: TestJobControlCompiler.java From spork with Apache License 2.0 | 6 votes |
public static POLoad createPOLoadWithSize(long size, LoadFunc loadFunc) throws Exception { File file = File.createTempFile("tempFile", ".tmp"); file.deleteOnExit(); RandomAccessFile f = new RandomAccessFile(file, "rw"); f.setLength(size); f.close(); loadFunc.setLocation(file.getAbsolutePath(), new org.apache.hadoop.mapreduce.Job(CONF)); FuncSpec funcSpec = new FuncSpec(loadFunc.getClass().getCanonicalName()); POLoad poLoad = new POLoad(new OperatorKey(), loadFunc); poLoad.setLFile(new FileSpec(file.getAbsolutePath(), funcSpec)); poLoad.setPc(new PigContext()); poLoad.setUp(); return poLoad; }
Example 5
Source File: AbstractFileCheckpointCollection.java From tracecompass with Eclipse Public License 2.0 | 6 votes |
private CheckpointCollectionFileHeader initialize() { CheckpointCollectionFileHeader header = null; try { RandomAccessFile randomAccessFile = new RandomAccessFile(fFile, "rw"); //$NON-NLS-1$ fFileChannel = randomAccessFile.getChannel(); header = createHeader(); // Reserve space for header randomAccessFile.setLength(header.getSize()); TmfCoreTracer.traceIndexer(CheckpointCollectionFileHeader.class.getSimpleName() + " initialize " + "nbEvents: " + header.fNbEvents + " fTimeRange: " + header.fTimeRange); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ fRandomAccessFile = randomAccessFile; } catch (IOException e) { Activator.logError(MessageFormat.format(Messages.ErrorOpeningIndex, fFile), e); return null; } return header; }
Example 6
Source File: PinotFSBenchmarkDriver.java From incubator-pinot with Apache License 2.0 | 5 votes |
private File createFileWithSize(String fileName, long sizeInBytes) throws IOException { File tmpLargeFile = new File(_localTempDir, fileName); tmpLargeFile.createNewFile(); RandomAccessFile raf = new RandomAccessFile(tmpLargeFile, "rw"); raf.setLength(sizeInBytes); raf.close(); return tmpLargeFile; }
Example 7
Source File: SpillFile.java From phoenix with Apache License 2.0 | 5 votes |
private TempFile createTempFile() throws IOException { // Create temp file in temp dir or custom dir if provided File tempFile = File.createTempFile(UUID.randomUUID().toString(), null, spillFilesDirectory); if (logger.isDebugEnabled()) { logger.debug("Creating new SpillFile: " + tempFile.getAbsolutePath()); } RandomAccessFile file = new RandomAccessFile(tempFile, "rw"); file.setLength(SPILL_FILE_SIZE); return new TempFile(tempFile, file); }
Example 8
Source File: MemoryMappedFiles.java From bboxdb with Apache License 2.0 | 5 votes |
public MemoryMappedFile(final int filesize) throws IOException { file = File.createTempFile("mmap", ".bin"); file.deleteOnExit(); randomAccessFile = new RandomAccessFile(file, "rw"); randomAccessFile.setLength(filesize); randomAccessFile.close(); }
Example 9
Source File: NexusFileLock.java From nexus-public with Eclipse Public License 1.0 | 5 votes |
private boolean doLock() { if (lock != null) { return true; } try { lockFile = new RandomAccessFile(file, "rws"); lock = lockFile.getChannel().tryLock(0L, 1L, false); if (lock != null) { byte[] payload = ManagementFactory.getRuntimeMXBean().getName().getBytes("UTF-8"); lockFile.setLength(0); lockFile.seek(0); lockFile.write(payload); } } catch (Exception e) { // logging is not configured yet, so use console System.err.println("Failed to write lock file: " + file.getAbsolutePath()); e.printStackTrace(); // handle it as null result lock = null; } finally { if (lock == null) { release(); return false; } } return true; }
Example 10
Source File: MappedResizeableBufferTest.java From agrona with Apache License 2.0 | 5 votes |
@BeforeAll public static void setUp() throws IOException { final RandomAccessFile file = new RandomAccessFile(PATH, "rw"); file.setLength(SIZE); channel = file.getChannel(); }
Example 11
Source File: ByteArrayBackupToFileOutputStream.java From json-data-generator with Apache License 2.0 | 5 votes |
/** * remove one byte from the written outputstream * @throws IOException if there is no more buffer to unwrite from */ public void unwrite() throws IOException { if (count == 0) { throw new IOException("Pushback buffer overflow"); } if (buf == null) { @SuppressWarnings("resource") RandomAccessFile randomAccessFile = new RandomAccessFile(file, "rw"); randomAccessFile.setLength(file.length() - 1); return; } buf[--count] = (byte) 0; }
Example 12
Source File: ShpFile.java From mrgeo with Apache License 2.0 | 5 votes |
public void save(RandomAccessFile out) throws IOException { // write data if (index.offset.length > 0) { saveRows(out, index.getCachePos()); } // recalc header vars // calculate fileLength first (written in header) if (index.offset.length > 0) { int filePos = 0; if (index.cachepos == 0) { filePos = 100; } for (int j = 0; j < index.offset.length; j++) { // load geometry to get size JShape tempShape = data.getShape(j + index.cachepos); filePos += 8 + tempShape.getRecordLength(); } header.fileLength += filePos / 2; out.setLength(2 * header.fileLength); // write header out.seek(0); header.save(out); } }
Example 13
Source File: DeltaStoreTest.java From incubator-retired-wave with Apache License 2.0 | 5 votes |
public void testRecoverFromTruncatedDeltas() throws Exception { // Create an entry with one record. Shrink the file byte by byte and ensure // we can read it without crashing. DeltaStore store = newDeltaStore(); WaveletDeltaRecord written = createRecord(); File deltaFile = FileDeltaCollection.deltasFile(path.getAbsolutePath(), WAVE1_WAVELET1); long toRemove = 1; while (true) { // This generates the full file. DeltasAccess wavelet = store.open(WAVE1_WAVELET1); wavelet.append(ImmutableList.of(written)); wavelet.close(); RandomAccessFile file = new RandomAccessFile(deltaFile, "rw"); if (toRemove > file.length()) { file.close(); break; } // eat the planned number of bytes LOG.info("trying to remove " + toRemove + " bytes"); file.setLength(file.length() - toRemove); file.close(); wavelet = store.open(WAVE1_WAVELET1); WaveletDeltaRecord read = wavelet.getDelta(0); assertNull("got an unexpected record " + read, read); wavelet.close(); toRemove++; } }
Example 14
Source File: ArchiveFileSaveTest.java From birt with Eclipse Public License 1.0 | 5 votes |
void copyFile( String src, String tgt ) throws IOException { RandomAccessFile srcFile = new RandomAccessFile( src, "r" ); RandomAccessFile tgtFile = new RandomAccessFile( tgt, "rw" ); byte[] bytes = new byte[(int) srcFile.length( )]; srcFile.read( bytes ); tgtFile.setLength( 0 ); tgtFile.write( bytes ); srcFile.close( ); tgtFile.close( ); }
Example 15
Source File: StringArrayFile.java From learntosolveit with BSD 3-Clause "New" or "Revised" License | 5 votes |
static void writeStrings(String filename, Iterable<String> strGenerator) throws IOException { RandomAccessFile raf = new RandomAccessFile(filename, "rw"); raf.setLength(0); ArrayList<Long> offsetttable = new ArrayList<Long>(); for(String s: strGenerator) { offsetttable.add(raf.getFilePointer()); raf.writeUTF(s); } for(long offset: offsetttable) raf.writeLong(offset); raf.writeInt(offsetttable.size()); raf.close(); }
Example 16
Source File: MainActivity.java From Android-Basics-Codes with Artistic License 2.0 | 4 votes |
private void requestNet() { // (1). �����ͷ�������Դ�ļ�һ����С�Ŀ��ļ� try { // 1. ��ʼ��Url URL url = new URL(path); // 2. ͨ��Url��ȡHttp���� HttpURLConnection conn = (HttpURLConnection) url.openConnection(); // 3. �����������������ʽ conn.setRequestMethod("GET"); // 4. ��ȡ������ 200:�ɹ� 3xxx���� 4xxx�ͻ��˴��� 500���������� int code = conn.getResponseCode(); // 5. �õ��ӷ������˷��ص���Դ�ļ��Ĵ�С int fileLength = conn.getContentLength(); if (code == 200) { System.out.println("��������Դ�ļ��Ĵ�С��" + fileLength); RandomAccessFile raf = new RandomAccessFile(getFileName(), "rw"); // ��Ҫ�����ļ��Ĵ�С raf.setLength(fileLength); raf.close(); } // (2).��������߳����� // ÿ������Ĵ�С int blockSize = fileLength / threadCount; for (int threadId = 0; threadId < threadCount; threadId++) { int startIndex = threadId * blockSize; int endIndex = (threadId + 1) * blockSize; // ���һ���߳� if (threadId == threadCount - 1) { // �����ļ�������λ�� endIndex = fileLength - 1; } // ��ʼ�߳� new DownLoadThread(startIndex, endIndex, threadId).start(); } } catch (Exception e) { e.printStackTrace(); } }
Example 17
Source File: IndexHashTest.java From sparkey-java with Apache License 2.0 | 4 votes |
private void corruptFile(File indexFile) throws IOException { RandomAccessFile randomAccessFile = new RandomAccessFile(indexFile, "rw"); randomAccessFile.setLength(randomAccessFile.length() - 100); randomAccessFile.close(); }
Example 18
Source File: ExpandingMap.java From jdk8u-jdk with GNU General Public License v2.0 | 4 votes |
public static void main(String[] args) throws Exception { int initialSize = 20480*1024; int maximumMapSize = 16*1024*1024; int maximumFileSize = 300000000; File file = File.createTempFile("exp", "tmp"); file.deleteOnExit(); RandomAccessFile f = new RandomAccessFile(file, "rw"); f.setLength(initialSize); FileChannel fc = f.getChannel(); ByteBuffer[] buffers = new ByteBuffer[128]; System.out.format("map %d -> %d\n", 0, initialSize); buffers[0] = fc.map(FileChannel.MapMode.READ_WRITE, 0, initialSize); int currentBuffer = 0; int currentSize = initialSize; int currentPosition = 0; ArrayList<String> junk = new ArrayList<String>(); while (currentPosition+currentSize < maximumFileSize) { int inc = Math.max(1000*1024, (currentPosition+currentSize)/8); int size = currentPosition+currentSize+inc; f.setLength(size); while (currentSize+inc > maximumMapSize) { if (currentSize < maximumMapSize) { System.out.format("map %d -> %d\n", currentPosition, (currentPosition + maximumMapSize)); buffers[currentBuffer] = fc.map(FileChannel.MapMode.READ_WRITE, currentPosition, maximumMapSize); fillBuffer(buffers[currentBuffer], currentSize); } currentPosition += maximumMapSize; inc = currentSize+inc-maximumMapSize; currentSize = 0; currentBuffer++; if (currentBuffer == buffers.length) { ByteBuffer[] old = buffers; buffers = new ByteBuffer[currentBuffer+currentBuffer/2]; System.arraycopy(old, 0, buffers, 0, currentBuffer); } } currentSize += inc; if (currentSize > 0) { System.out.format("map %d -> %d\n", currentPosition, (currentPosition + currentSize)); buffers[currentBuffer] = fc.map(FileChannel.MapMode.READ_WRITE, currentPosition, currentSize); fillBuffer(buffers[currentBuffer], currentSize-inc); } // busy loop needed to reproduce issue long t = System.currentTimeMillis(); while (System.currentTimeMillis() < t+500) { junk.add(String.valueOf(t)); if (junk.size() > 100000) junk.clear(); } } fc.close(); // cleanup the ref to mapped buffers so they can be GCed for (int i = 0; i < buffers.length; i++) buffers[i] = null; System.gc(); // Take a nap to wait for the Cleaner to cleanup those unrefed maps Thread.sleep(1000); System.out.println("TEST PASSED"); }
Example 19
Source File: DownloadFileFromFtpToTable.java From MicroCommunity with Apache License 2.0 | 4 votes |
/** * 下载文件并且存至tfs文件系统 * * @param remoteFileNameTmp * @param * @return */ private Map downLoadFileToTable(String remoteFileNameTmp, FTPClientTemplate ftpClientTemplate, Map ftpItemConfigInfo) { Map resultInfo = new HashMap(); String tfsReturnFileName = null; long block = 10 * 1024;// 默认 if (ftpClientTemplate == null) { resultInfo.put("SAVE_FILE_FLAG", "E"); return resultInfo; } String localPathName = ftpItemConfigInfo.get("localPath").toString().endsWith("/") ? ftpItemConfigInfo.get("localPath").toString() + ftpItemConfigInfo.get("newFileName").toString() : ftpItemConfigInfo.get("localPath").toString() + "/" + ftpItemConfigInfo.get("newFileName").toString(); ftpItemConfigInfo.put("localfilename", localPathName);// 本地带路径的文件名回写,后面读文件时使用 try { File file = new File(localPathName); RandomAccessFile accessFile = new RandomAccessFile(file, "rwd");// 建立随机访问 FTPClient ftpClient = new FTPClient(); ftpClient.connect(ftpClientTemplate.getHost(), ftpClientTemplate.getPort()); if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) { if (!ftpClient.login(ftpClientTemplate.getUsername(), ftpClientTemplate.getPassword())) { resultInfo.put("SAVE_FILE_FLAG", "E"); resultInfo.put("remark", "登录失败,用户名【" + ftpClientTemplate.getUsername() + "】密码【" + ftpClientTemplate.getPassword() + "】"); return resultInfo; } } ftpClient.setControlEncoding("UTF-8"); ftpClient.setFileType(FTP.BINARY_FILE_TYPE); // 二进制 ftpClient.enterLocalPassiveMode(); // 被动模式 ftpClient.sendCommand("PASV"); ftpClient.sendCommand("SIZE " + remoteFileNameTmp + "\r\n"); String replystr = ftpClient.getReplyString(); String[] replystrL = replystr.split(" "); long filelen = 0; if (Integer.valueOf(replystrL[0]) == 213) { filelen = Long.valueOf(replystrL[1].trim()); } else { resultInfo.put("SAVE_FILE_FLAG", "E"); resultInfo.put("remark", "无法获取要下载的文件的大小!"); return resultInfo; } accessFile.setLength(filelen); accessFile.close(); ftpClient.disconnect(); int tnum = Integer.valueOf(ftpItemConfigInfo.get("TNUM").toString()); block = (filelen + tnum - 1) / tnum;// 每个线程下载的快大小 ThreadPoolExecutor cachedThreadPool = (ThreadPoolExecutor) Executors.newCachedThreadPool(); List<Future<Map>> threadR = new ArrayList<Future<Map>>(); for (int i = 0; i < tnum; i++) { logger.debug("发起线程:" + i); // 保存线程日志 ftpItemConfigInfo.put("threadrunstate", "R"); ftpItemConfigInfo.put("remark", "开始下载文件"); ftpItemConfigInfo.put("data", "文件名:" + remoteFileNameTmp); long start = i * block; long end = (i + 1) * block - 1; ftpItemConfigInfo.put("begin", start); ftpItemConfigInfo.put("end", end); saveTaskLogDetail(ftpItemConfigInfo); Map para = new HashMap(); para.putAll(ftpItemConfigInfo); para.put("serverfilename", remoteFileNameTmp); para.put("filelength", filelen); para.put("tnum", i + 0); para.put("threadDownSize", block); para.put("transferflag", FTPClientTemplate.TransferType.download); FTPClientTemplate dumpThread = new FTPClientTemplate(para); Future<Map> runresult = cachedThreadPool.submit(dumpThread); threadR.add(runresult); } do { // 等待下载完成 Thread.sleep(1000); } while (cachedThreadPool.getCompletedTaskCount() < threadR.size()); saveDownFileData(ftpItemConfigInfo); // 下载已经完成,多线程保存数据至表中 } catch (Exception e) { // TODO Auto-generated catch block logger.error("保存文件失败:", e); resultInfo.put("SAVE_FILE_FLAG", "E"); resultInfo.put("remark", "保存文件失败:" + e); return resultInfo; } resultInfo.put("SAVE_FILE_FLAG", "S"); return resultInfo; }
Example 20
Source File: Lobstack.java From jelectrum with MIT License | 4 votes |
public Lobstack(File dir, String name, boolean compress, int key_step_size) throws IOException { this.key_step_size = key_step_size; this.dir = dir; this.stack_name = name; this.compress = compress; if (!dir.exists()) { throw new java.io.IOException("Directory does not exist: " + dir); } if (!dir.isDirectory()) { throw new java.io.IOException("Location is not a directory: " + dir); } data_files = new AutoCloseLRUCache<Long, FileChannel>(MAX_OPEN_FILES); RandomAccessFile root_file = new RandomAccessFile(new File(dir, name + ".root"), MODE); root_file_channel = root_file.getChannel(); if (root_file.length()==0) { root_file.setLength(16); reset(); } else { synchronized(ptr_lock) { root_file.seek(ROOT_ROOT_LOCATION); current_root = root_file.readLong(); root_file.seek(ROOT_WRITE_LOCATION); current_write_location = root_file.readLong(); } } showSize(); }