Skip to content

Commit

Permalink
Use NIO's Files API to replace FileInputStream/FileOutputStream in so…
Browse files Browse the repository at this point in the history
…me paths
  • Loading branch information
zuston committed Jul 21, 2022
1 parent bdffcaa commit e82169b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Constructor;
Expand All @@ -33,6 +32,7 @@
import java.nio.BufferUnderflowException;
import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
Expand Down Expand Up @@ -86,7 +86,8 @@ public static Map<String, String> getPropertiesFromFile(String filename) {
LOGGER.info("Load config from {}", filename);
final Map<String, String> result = new HashMap<>();

try (InputStreamReader inReader = new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8)) {
try (InputStreamReader inReader =
new InputStreamReader(Files.newInputStream(file.toPath()), StandardCharsets.UTF_8)) {
Properties properties = new Properties();
properties.load(inReader);
properties.stringPropertyNames().forEach(k -> result.put(k, properties.getProperty(k).trim()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
package org.apache.uniffle.server;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.util.List;

import com.google.common.annotations.VisibleForTesting;
Expand Down Expand Up @@ -164,7 +165,7 @@ boolean checkStorageReadAndWrite() {
}
byte[] readData = new byte[1024];
int readBytes = -1;
try (FileInputStream fis = new FileInputStream(writeFile)) {
try (InputStream fis = Files.newInputStream(writeFile.toPath())) {
int hasReadBytes = 0;
do {
readBytes = fis.read(readData);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
package org.apache.uniffle.storage.handler.impl;

import java.io.Closeable;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.ByteBuffer;
import java.util.List;

Expand Down Expand Up @@ -141,7 +141,7 @@ public synchronized void close() throws IOException {
}
}

public long copy(FileInputStream inputStream, int bufferSize) throws IOException {
public long copy(InputStream inputStream, int bufferSize) throws IOException {
long start = fsDataOutputStream.getPos();
IOUtils.copyBytes(inputStream, fsDataOutputStream, bufferSize);
return fsDataOutputStream.getPos() - start;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,19 +20,23 @@
import java.io.Closeable;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.nio.file.Files;

import org.apache.uniffle.storage.common.FileBasedShuffleSegment;

import static java.nio.file.StandardOpenOption.APPEND;
import static java.nio.file.StandardOpenOption.CREATE;

public class LocalFileWriter implements Closeable {

private DataOutputStream dataOutputStream;
private FileOutputStream fileOutputStream;
private OutputStream fileOutputStream;
private long nextOffset;

public LocalFileWriter(File file) throws IOException {
fileOutputStream = new FileOutputStream(file, true);
fileOutputStream = Files.newOutputStream(file.toPath(), CREATE, APPEND);
// init fsDataOutputStream
dataOutputStream = new DataOutputStream(fileOutputStream);
nextOffset = file.length();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,10 @@
package org.apache.uniffle.storage.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.util.Collections;
import java.util.List;

Expand Down Expand Up @@ -232,7 +233,7 @@ public static int getHeaderCrcLen() {
}

public static long uploadFile(File file, HdfsFileWriter writer, int bufferSize) throws IOException {
try (FileInputStream inputStream = new FileInputStream(file)) {
try (InputStream inputStream = Files.newInputStream(file.toPath())) {
return writer.copy(inputStream, bufferSize);
} catch (IOException e) {
LOG.error("Fail to upload file {}, {}", file.getAbsolutePath(), e);
Expand Down

0 comments on commit e82169b

Please sign in to comment.