Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crashes_on_Android_version_7_and_older #55

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@
<javadoc.dir>docs/javadoc</javadoc.dir>

<junit.version>5.9.2</junit.version>
<mockito.version>5.3.1</mockito.version>

<maven.enforcer.plugin>3.3.0</maven.enforcer.plugin>
<maven.compiler.plugin>3.11.0</maven.compiler.plugin>
Expand Down Expand Up @@ -96,6 +97,12 @@
<version>${junit.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
19 changes: 16 additions & 3 deletions src/main/java/io/github/cdimascio/dotenv/DotenvBuilder.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
package io.github.cdimascio.dotenv;

import io.github.cdimascio.dotenv.internal.DotenvFileReader;
import io.github.cdimascio.dotenv.internal.DotenvParser;
import io.github.cdimascio.dotenv.internal.DotenvReader;
import io.github.cdimascio.dotenv.internal.DotenvPathReader;

import java.util.*;

Expand Down Expand Up @@ -64,15 +65,27 @@ public DotenvBuilder systemProperties() {
return this;
}

protected boolean pathsApiAvailable() {
try {
final String packageName = "java.nio.file";
Class.forName(packageName + "." + "Paths");
return true;
} catch (Exception e) {
}
return false;
}

/**
* Load the contents of .env into the virtual environment.
* @return a new {@link Dotenv} instance
* @throws DotenvException when an error occurs
*/
public Dotenv load() throws DotenvException {
final var fileReader = pathsApiAvailable() ?
new DotenvPathReader(directoryPath, filename) : new DotenvFileReader(directoryPath, filename);
final var reader = new DotenvParser(
new DotenvReader(directoryPath, filename),
throwIfMissing, throwIfMalformed);
fileReader,
throwIfMissing, throwIfMalformed);
final List<DotenvEntry> env = reader.parse();
if (systemProperties) {
env.forEach(it -> System.setProperty(it.getKey(), it.getValue()));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package io.github.cdimascio.dotenv.internal;

import io.github.cdimascio.dotenv.DotenvException;

import java.io.IOException;
import java.util.List;

public abstract class BaseDotenvReader implements DotenvReader {
protected final String directory;
protected final String filename;

/**
* Creates a dotenv reader
* @param directory the directory containing the .env file
* @param filename the file name of the .env file e.g. .env
*/
public BaseDotenvReader(String directory, String filename) {
this.directory = directory;
this.filename = filename;
}

public abstract List<String> read() throws DotenvException, IOException;



protected String sanitizeDirectory() {
String dir = directory
.replaceAll("\\\\", "/")
.replaceFirst("\\.env$", "")
.replaceFirst("/$", "");
return dir;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import io.github.cdimascio.dotenv.DotenvException;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Scanner;
Expand All @@ -12,6 +13,18 @@
*/
public class ClasspathHelper {
static Stream<String> loadFileFromClasspath(String location) {
InputStream inputStream = getInputStream(location);

final var scanner = new Scanner(inputStream, StandardCharsets.UTF_8);
final var lines = new ArrayList<String>();
while (scanner.hasNext()) {
lines.add(scanner.nextLine());
}

return lines.stream();
}

static InputStream getInputStream(String location) {
final var loader = ClasspathHelper.class;
var inputStream = loader.getResourceAsStream(location);
if (inputStream == null) {
Expand All @@ -22,15 +35,8 @@ static Stream<String> loadFileFromClasspath(String location) {
}

if (inputStream == null) {
throw new DotenvException("Could not find "+location+" on the classpath");
throw new DotenvException("Could not find "+ location +" on the classpath");
}

final var scanner = new Scanner(inputStream, StandardCharsets.UTF_8);
final var lines = new ArrayList<String>();
while (scanner.hasNext()) {
lines.add(scanner.nextLine());
}

return lines.stream();
return inputStream;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package io.github.cdimascio.dotenv.internal;

import io.github.cdimascio.dotenv.DotenvException;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.net.URI;
import java.util.ArrayList;
import java.util.List;

/**
* (Internal) Reads a .env file
*/
public class DotenvFileReader extends BaseDotenvReader {
/**
* Creates a dotenv reader bases on File api
* @param directory the directory containing the .env file
* @param filename the file name of the .env file e.g. .env
*/
public DotenvFileReader(String directory, String filename) {
super(directory, filename);
}

/**
* (Internal) Reads the .env file
* @return a list containing the contents of each line in the .env file
* @throws DotenvException if a dotenv error occurs
*/
public List<String> read() throws DotenvException {
final String dir = sanitizeDirectory();

final String location = dir + "/" + filename;
final String lowerLocation = location.toLowerCase();

try {
final File file = lowerLocation.startsWith("file:") || lowerLocation.startsWith("android.resource:")
? new File(URI.create(location))
: new File(location);

if (file.exists()) {
return FileClasspathHelper.readLines(new FileInputStream(file));
}
var classpathLocation = file.exists() ? file.getPath() : location.replaceFirst("^\\./", "/");
return new ArrayList<>(FileClasspathHelper
.loadFileFromClasspath(classpathLocation));
} catch (DotenvException | FileNotFoundException e) {
e.addSuppressed(new DotenvException("Could not find " + location + " on the file system "));
throw new DotenvException(e);
}
}




}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package io.github.cdimascio.dotenv.internal;

import java.io.IOException;
import java.net.URI;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

import io.github.cdimascio.dotenv.DotenvException;

/**
* (Internal) Reads a .env file
*/
public class DotenvPathReader extends BaseDotenvReader {
/**
* Creates a dotenv reader based on the Path and Files
*
* @param directory the directory containing the .env file
* @param filename the file name of the .env file e.g. .env
*/
public DotenvPathReader(String directory, String filename) {
super(directory, filename);
}

/**
* (Internal) Reads the .env file
* @return a list containing the contents of each line in the .env file
* @throws DotenvException if a dotenv error occurs
* @throws IOException if an I/O error occurs
*/
public List<String> read() throws DotenvException, IOException {
String dir = sanitizeDirectory();

String location = dir + "/" + filename;
String lowerLocation = location.toLowerCase();

Path path = lowerLocation.startsWith("file:") || lowerLocation.startsWith("android.resource:")
? Paths.get(URI.create(location))
: Paths.get(location);

if (Files.exists(path)) {
return Files.readAllLines(path);
}

try {
return ClasspathHelper
.loadFileFromClasspath(location.replaceFirst("^\\./", "/"))
.collect(Collectors.toList());
} catch (DotenvException e) {
Path cwd = FileSystems.getDefault().getPath(".").toAbsolutePath().normalize();
String cwdMessage = !path.isAbsolute() ? "(working directory: " + cwd + ")" : "";
e.addSuppressed(new DotenvException("Could not find " + path + " on the file system " + cwdMessage));
throw e;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,62 +3,14 @@
import io.github.cdimascio.dotenv.DotenvException;

import java.io.IOException;
import java.net.URI;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

/**
* (Internal) Reads a .env file
*/
public class DotenvReader {
private final String directory;
private final String filename;

/**
* Creates a dotenv reader
* @param directory the directory containing the .env file
* @param filename the file name of the .env file e.g. .env
*/
public DotenvReader(String directory, String filename) {
this.directory = directory;
this.filename = filename;
}

public interface DotenvReader {
/**
* (Internal) Reads the .env file
* Reads the .env file
* @return a list containing the contents of each line in the .env file
* @throws DotenvException if a dotenv error occurs
* @throws IOException if an I/O error occurs
*/
public List<String> read() throws DotenvException, IOException {
String dir = directory
.replaceAll("\\\\", "/")
.replaceFirst("\\.env$", "")
.replaceFirst("/$", "");

String location = dir + "/" + filename;
String lowerLocation = location.toLowerCase();
Path path = lowerLocation.startsWith("file:") || lowerLocation.startsWith("android.resource:")
? Paths.get(URI.create(location))
: Paths.get(location);

if (Files.exists(path)) {
return Files.readAllLines(path);
}

try {
return ClasspathHelper
.loadFileFromClasspath(location.replaceFirst("^\\./", "/"))
.collect(Collectors.toList());
} catch (DotenvException e) {
Path cwd = FileSystems.getDefault().getPath(".").toAbsolutePath().normalize();
String cwdMessage = !path.isAbsolute() ? "(working directory: " + cwd + ")" : "";
e.addSuppressed(new DotenvException("Could not find " + path + " on the file system " + cwdMessage));
throw e;
}
}
List<String> read() throws DotenvException, IOException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package io.github.cdimascio.dotenv.internal;

import io.github.cdimascio.dotenv.DotenvException;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List;

/**
* Classpath helper
*/
public class FileClasspathHelper {
static List<String> loadFileFromClasspath(String location) {
InputStream inputStream = ClasspathHelper.getInputStream(location);
return readLines(inputStream);
}

public static List<String> readLines(InputStream inputStream) {
var list = new LinkedList<String>();
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line = reader.readLine();
while (line != null) {
line = reader.readLine();
if (line == null) {
break;
}
list.add(line);
}
return list;
} catch (Exception e) {
}
throw new DotenvException("could not read file content");
}
}
28 changes: 28 additions & 0 deletions src/test/java/io/github/cdimascio/dotenv/DotenvReaderTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package io.github.cdimascio.dotenv;

import org.junit.jupiter.api.Test;
import org.mockito.Mockito;

import static org.junit.jupiter.api.Assertions.assertEquals;

class DotenvReaderTests {

@Test
void verifyDotenvFileReader() {
DotenvBuilder builder = Mockito.mock(DotenvBuilder.class);
Mockito.when(builder.pathsApiAvailable()).thenReturn(false);
final var dotenv = Dotenv.configure().ignoreIfMalformed().load();
final String value = dotenv.get("MY_TEST_EV2");
assertEquals("my test ev 2", value);
}

@Test
void verifyDotenvPathReader() {
DotenvBuilder builder = Mockito.mock(DotenvBuilder.class);
Mockito.when(builder.pathsApiAvailable()).thenReturn(true);
final var dotenv = Dotenv.configure().ignoreIfMalformed().load();
final String value = dotenv.get("MY_TEST_EV1");
assertEquals("my test ev 1", value);
}

}