Skip to content

Commit

Permalink
0002960: Add reference to file back to staged resource because we no
Browse files Browse the repository at this point in the history
longer keep so many instances in memory.
  • Loading branch information
chenson42 committed Jan 17, 2017
1 parent 792adf7 commit abee2dc
Showing 1 changed file with 17 additions and 23 deletions.
Expand Up @@ -48,6 +48,8 @@ public class StagedResource implements IStagedResource {
static final Logger log = LoggerFactory.getLogger(StagedResource.class);

private File directory;

private File file;

private String path;

Expand All @@ -71,11 +73,12 @@ public StagedResource(File directory, File file, StagingManager stagingManager)
this.directory = directory;
this.stagingManager = stagingManager;
this.path = toPath(directory, file);
if (file.exists()) {
if (file.exists()) {
lastUpdateTime = file.lastModified();
String fileName = file.getName();
String extension = fileName.substring(fileName.lastIndexOf(".") + 1, fileName.length());
this.state = State.valueOf(extension.toUpperCase());
this.file = file;
} else {
throw new IllegalStateException(String.format("The passed in file, %s, does not exist",
file.getAbsolutePath()));
Expand All @@ -92,8 +95,9 @@ public StagedResource(File directory, String path, StagingManager stagingManager
} else if (buildFile(State.DONE).exists()){
this.state = State.DONE;
} else {
this.state = State.CREATE;
this.state = State.CREATE;
}
this.file = buildFile(state);
}

protected static String toPath(File directory, File file) {
Expand All @@ -112,8 +116,7 @@ public boolean isInUse() {
}

public boolean isFileResource() {
File file = buildFile(state);
return (memoryBuffer == null || memoryBuffer.length() == 0) && file.exists();
return file != null && file.exists();
}

protected File buildFile(State state) {
Expand All @@ -125,8 +128,7 @@ public State getState() {
}

public void setState(State state) {
File file = buildFile(this.state);
if (file.exists()) {
if (file != null && file.exists()) {
File newFile = buildFile(state);
if (!newFile.equals(file)) {
if (newFile.exists()) {
Expand Down Expand Up @@ -172,8 +174,7 @@ public synchronized BufferedReader getReader() {
Thread thread = Thread.currentThread();
BufferedReader reader = readers != null ? readers.get(thread) : null;
if (reader == null) {
File file = buildFile(state);
if (file.exists()) {
if (file != null && file.exists()) {
try {
reader = new BufferedReader(new InputStreamReader(new FileInputStream(file),
IoConstants.ENCODING));
Expand Down Expand Up @@ -259,8 +260,7 @@ public void close() {
public OutputStream getOutputStream() {
try {
if (outputStream == null) {
File file = buildFile(state);
if (file.exists()) {
if (file != null && file.exists()) {
log.warn("We had to delete {} because it already existed",
file.getAbsolutePath());
file.delete();
Expand All @@ -278,8 +278,7 @@ public synchronized InputStream getInputStream() {
Thread thread = Thread.currentThread();
InputStream reader = inputStreams != null ? inputStreams.get(thread) : null;
if (reader == null) {
File file = buildFile(state);
if (file.exists()) {
if (file != null && file.exists()) {
try {
reader = new BufferedInputStream(new FileInputStream(file));
createInputStreamsMap();
Expand All @@ -297,8 +296,7 @@ public synchronized InputStream getInputStream() {

public BufferedWriter getWriter(long threshold) {
if (writer == null) {
File file = buildFile(state);
if (file.exists()) {
if (file != null && file.exists()) {
log.warn("We had to delete {} because it already existed", file.getAbsolutePath());
file.delete();
} else if (this.memoryBuffer != null) {
Expand All @@ -313,8 +311,7 @@ public BufferedWriter getWriter(long threshold) {
}

public long getSize() {
File file = buildFile(state);
if (file.exists()) {
if (file != null && file.exists()) {
return file.length();
} else if (memoryBuffer != null) {
return memoryBuffer.length();
Expand All @@ -324,8 +321,7 @@ public long getSize() {
}

public boolean exists() {
File file = buildFile(state);
return (file.exists() && file.length() > 0) || (memoryBuffer != null && memoryBuffer.length() > 0);
return (file != null && file.exists() && file.length() > 0) || (memoryBuffer != null && memoryBuffer.length() > 0);
}

public long getLastUpdateTime() {
Expand All @@ -341,8 +337,7 @@ public boolean delete() {
boolean deleted = true;

close();
File file = buildFile(state);
if (file.exists()) {
if (file != null && file.exists()) {
FileUtils.deleteQuietly(file);
deleted = !file.exists();
}
Expand All @@ -362,7 +357,7 @@ public boolean delete() {
}

public File getFile() {
return buildFile(state);
return file;
}

public String getPath() {
Expand All @@ -371,8 +366,7 @@ public String getPath() {

@Override
public String toString() {
File file = buildFile(state);
return file.exists() ? file.getAbsolutePath() : String.format("%d bytes in memory",
return (file != null && file.exists()) ? file.getAbsolutePath() : String.format("%d bytes in memory",
memoryBuffer != null ? memoryBuffer.length() : 0);
}

Expand Down

0 comments on commit abee2dc

Please sign in to comment.