<commit_before>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.flink.core.fs.local;

import org.apache.flink.annotation.Internal;
import org.apache.flink.core.fs.RecoverableFsDataOutputStream;
import org.apache.flink.core.fs.RecoverableWriter.CommitRecoverable;
import org.apache.flink.core.fs.RecoverableWriter.ResumeRecoverable;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.AtomicMoveNotSupportedException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;

import static org.apache.flink.util.Preconditions.checkNotNull;

/**
 * A {@link RecoverableFsDataOutputStream} for the {@link LocalFileSystem}.
 */
@Internal
class LocalRecoverableFsDataOutputStream extends RecoverableFsDataOutputStream {

	private final File targetFile;

	private final File tempFile;

	private final FileOutputStream fos;

	LocalRecoverableFsDataOutputStream(File targetFile, File tempFile) throws IOException {
		this.targetFile = checkNotNull(targetFile);
		this.tempFile = checkNotNull(tempFile);
		this.fos = new FileOutputStream(tempFile);
	}

	LocalRecoverableFsDataOutputStream(LocalRecoverable resumable) throws IOException {
		this.targetFile = checkNotNull(resumable.targetFile());
		this.tempFile = checkNotNull(resumable.tempFile());

		if (!tempFile.exists()) {
			throw new FileNotFoundException("File Not Found: " + tempFile);
		}

		if (tempFile.length() < resumable.offset()) {
			throw new IOException("Missing data in tmp file: " + tempFile);
		}

		this.fos = new FileOutputStream(this.tempFile, true);
		this.fos.getChannel().truncate(resumable.offset());
	}

	@Override
	public void write(int b) throws IOException {
		fos.write(b);
	}

	@Override
	public void write(byte[] b, int off, int len) throws IOException {
		fos.write(b, off, len);
	}

	@Override
	public void flush() throws IOException {
		fos.flush();
	}

	@Override
	public void sync() throws IOException {
		fos.getFD().sync();
	}

	@Override
	public long getPos() throws IOException {
		return fos.getChannel().position();
	}

	@Override
	public ResumeRecoverable persist() throws IOException {
		// we call both flush and sync in order to ensure persistence on mounted
		// file systems, like NFS, EBS, EFS, ...
		flush();
		sync();

		return new LocalRecoverable(targetFile, tempFile, getPos());
	}
<commit_msg>[FLINK-9751] [filesystem] Use FileChannel directly in LocalRecoverableFsDataOutputStream<commit_after>/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.flink.core.fs.local;

import org.apache.flink.annotation.Internal;
import org.apache.flink.core.fs.RecoverableFsDataOutputStream;
import org.apache.flink.core.fs.RecoverableWriter.CommitRecoverable;
import org.apache.flink.core.fs.RecoverableWriter.ResumeRecoverable;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.channels.Channels;
import java.nio.channels.FileChannel;
import java.nio.file.AtomicMoveNotSupportedException;
import java.nio.file.FileAlreadyExistsException;
import java.nio.file.Files;
import java.nio.file.StandardCopyOption;
import java.nio.file.StandardOpenOption;

import static org.apache.flink.util.Preconditions.checkNotNull;

/**
 * A {@link RecoverableFsDataOutputStream} for the {@link LocalFileSystem}.
 */
@Internal
class LocalRecoverableFsDataOutputStream extends RecoverableFsDataOutputStream {

	private final File targetFile;

	private final File tempFile;

	private final FileChannel fileChannel;

	private final OutputStream fos;

	LocalRecoverableFsDataOutputStream(File targetFile, File tempFile) throws IOException {
		this.targetFile = checkNotNull(targetFile);
		this.tempFile = checkNotNull(tempFile);

		this.fileChannel = FileChannel.open(tempFile.toPath(), StandardOpenOption.WRITE, StandardOpenOption.CREATE_NEW);
		this.fos = Channels.newOutputStream(fileChannel);
	}

	LocalRecoverableFsDataOutputStream(LocalRecoverable resumable) throws IOException {
		this.targetFile = checkNotNull(resumable.targetFile());
		this.tempFile = checkNotNull(resumable.tempFile());

		if (!tempFile.exists()) {
			throw new FileNotFoundException("File Not Found: " + tempFile.getAbsolutePath());
		}

		this.fileChannel = FileChannel.open(tempFile.toPath(), StandardOpenOption.WRITE, StandardOpenOption.APPEND);
		if (this.fileChannel.position() < resumable.offset()) {
			throw new IOException("Missing data in tmp file: " + tempFile.getAbsolutePath());
		}
		this.fileChannel.truncate(resumable.offset());
		this.fos = Channels.newOutputStream(fileChannel);
	}

	@Override
	public void write(int b) throws IOException {
		fos.write(b);
	}

	@Override
	public void write(byte[] b, int off, int len) throws IOException {
		fos.write(b, off, len);
	}

	@Override
	public void flush() throws IOException {
		fos.flush();
	}

	@Override
	public void sync() throws IOException {
		fileChannel.force(true);
	}

	@Override
	public long getPos() throws IOException {
		return fileChannel.position();
	}

	@Override
	public ResumeRecoverable persist() throws IOException {
		// we call both flush and sync in order to ensure persistence on mounted
		// file systems, like NFS, EBS, EFS, ...
		flush();
		sync();

		return new LocalRecoverable(targetFile, tempFile, getPos());
	}

{'fraction_non_alphanumeric': 0.09539520872184766, 'fraction_numerical': 0.0017214173002438674, 'mean_word_length': 4.943734015345268, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7492686', 'n_tokens_mistral': 2071, 'n_tokens_neox': 1966, 'n_words': 597}