/** * Creates a new file output stream. * * @param path the file path * @param options the client options * @throws IOException if an I/O error occurs */ public FileOutStream(AlluxioURI path, OutStreamOptions options) throws IOException { mUri = Preconditions.checkNotNull(path); mNonce = IdUtils.getRandomNonNegativeLong(); mBlockSize = options.getBlockSizeBytes(); mAlluxioStorageType = options.getAlluxioStorageType(); mUnderStorageType = options.getUnderStorageType(); mContext = FileSystemContext.INSTANCE; mPreviousBlockOutStreams = new LinkedList<BufferedBlockOutStream>(); if (mUnderStorageType.isSyncPersist()) { updateUfsPath(); String tmpPath = PathUtils.temporaryFileName(mNonce, mUfsPath); UnderFileSystem ufs = UnderFileSystem.get(tmpPath, ClientContext.getConf()); // TODO(jiri): Implement collection of temporary files left behind by dead clients. mUnderStorageOutputStream = ufs.create(tmpPath, (int) mBlockSize); } else { mUfsPath = null; mUnderStorageOutputStream = null; } mClosed = false; mCanceled = false; mShouldCacheCurrentBlock = mAlluxioStorageType.isStore(); mBytesWritten = 0; mLocationPolicy = Preconditions.checkNotNull( options.getLocationPolicy(), PreconditionMessage.FILE_WRITE_LOCATION_POLICY_UNSPECIFIED); }
// Creates a block file and write an increasing byte array into it private void createBlockFile(String filename, int len) throws IOException, InvalidPathException { UnderFileSystem ufs = UnderFileSystem.get(filename, mMasterConfiguration); ufs.mkdirs(PathUtils.getParent(filename), true); OutputStream out = ufs.create(filename); out.write(BufferUtils.getIncreasingByteArray(len), 0, len); out.close(); }
/** Tests if file creation is atomic. */ @Test public void createAtomic() throws IOException { String testFile = PathUtils.concatPath(mUnderfsAddress, "testFile"); OutputStream stream = mUfs.create(testFile); stream.write(TEST_BYTES); Assert.assertFalse(mUfs.exists(testFile)); stream.close(); }
private void createTestBytesFile(String path) throws IOException { OutputStream o = mUfs.create(path); o.write(TEST_BYTES); o.close(); }
private void createEmptyFile(String path) throws IOException { OutputStream o = mUfs.create(path); o.close(); }