Skip to content

Commit

Permalink
Adding Luwrain.createTempFile()
Browse files Browse the repository at this point in the history
  • Loading branch information
marigostra committed May 9, 2024
1 parent d10f038 commit 54ad455
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main/java/org/luwrain/core/Base.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ interface StopCondition
protected final InterfaceManager interfaces = new InterfaceManager(this);
protected final ExtensionsManager extensions = new ExtensionsManager(this, interfaces);
protected final ObjRegistry objRegistry = new ObjRegistry();
final TempFiles tempFiles = new TempFiles();
protected final CommandManager commands = new CommandManager();//FIXME:must be merged into objRegistry
protected final EventQueue eventQueue = new EventQueue();
protected final MainStopCondition mainStopCondition = new MainStopCondition();
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/org/luwrain/core/Luwrain.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ public enum JobFlags { TRACKING };
*/
Path getAppDataDir(String appName);

java.io.File createTempFile(String prefix);

//May return -1 if area is not shown on the screen;
int getAreaVisibleHeight(Area area);

Expand Down
6 changes: 6 additions & 0 deletions src/main/java/org/luwrain/core/LuwrainImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,12 @@ final class LuwrainImpl implements Luwrain
return core.os.escapeString(style, text);
}

@Override public java.io.File createTempFile(String prefix)
{
notNull(prefix, "prefix");
return core.tempFiles.createTempFile(!prefix.trim().isEmpty()?prefix.trim():"unknown");
}

private void sayHint(Hint hint)
{
NullCheck.notNull(hint, "hint");
Expand Down
76 changes: 76 additions & 0 deletions src/main/java/org/luwrain/core/TempFiles.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
/*
Copyright 2012-2024 Michael Pozhidaev <[email protected]>
This file is part of LUWRAIN.
LUWRAIN is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public
License as published by the Free Software Foundation; either
version 3 of the License, or (at your option) any later version.
LUWRAIN is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
*/

package org.luwrain.core;

import java.io.*;
import java.nio.file.*;
import org.apache.logging.log4j.*;

import static java.nio.file.Files.*;

final class TempFiles
{
static private final Logger log = LogManager.getLogger();
static private final String
ENV_TMP_DIR = System.getenv("TMPDIR"),
PROP_TMP_DIR = System.getProperty("java.io.tmpdir"),
PROP_USER_HOME = System.getProperty("user.home");
private Path tmpDir = null;

void init()
{
if (tmpDir != null)
return;
try {
if (ENV_TMP_DIR != null && !ENV_TMP_DIR.trim().isEmpty() && new File(ENV_TMP_DIR).exists())
{
log.trace("Using the value from $TMPDIR for LUWRAIN temporary directory, $TMPDIR=" + ENV_TMP_DIR);
tmpDir = createTempDirectory(Paths.get(ENV_TMP_DIR), ".luwrain-");
} else
if (PROP_TMP_DIR != null && !PROP_TMP_DIR.trim().isEmpty() && new File(PROP_TMP_DIR).exists())
{
log.trace("Using the value from the java.io.tmpdir system property for LUWRAIN temporary directory, java.io.tmpdir=" + PROP_TMP_DIR);
tmpDir = createTempDirectory(Paths.get(PROP_TMP_DIR), ".luwrain-");
} else
{
log.warn("Using the value from user.home system property for LUWRAIN temporary directory, user.home=" + PROP_USER_HOME);
tmpDir = createTempDirectory(Paths.get(PROP_USER_HOME), ".luwrain-");
}
tmpDir.toFile().deleteOnExit();
}
catch(IOException e)
{
throw new RuntimeException(e);
}
}

File createTempFile(String prefix)
{
NullCheck.notNull(prefix, "prefix");
init();
try {
final var res = Files.createTempFile(tmpDir, prefix + "-", ".tmp");
log.trace("Created temporary file " + res.toString());
res.toFile().deleteOnExit();
return res.toFile();
}
catch(IOException e)
{
throw new RuntimeException(e);
}
}
}

0 comments on commit 54ad455

Please sign in to comment.