Skip to content

Commit

Permalink
#391: Fix reading files with invalid encoding (#392)
Browse files Browse the repository at this point in the history
Co-authored-by: kaklakariada <[email protected]>
  • Loading branch information
kaklakariada and kaklakariada authored Feb 26, 2024
1 parent 966668b commit ea8baaf
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 20 deletions.
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,10 @@ OpenFastTrace at it's core is a Java Archive (short "[JAR](https://docs.oracle.c

### Getting Pre-Built Packages

Pre-Built JAR files (called `openfasttrace-3.7.1.jar`) are available from the following places:
Pre-Built JAR files (called `openfasttrace-3.8.0.jar`) are available from the following places:

* [Maven Central](https://repo1.maven.org/maven2/org/itsallcode/openfasttrace/openfasttrace/3.7.1/openfasttrace-3.7.1.jar)
* [GitHub](https://github.com/itsallcode/openfasttrace/releases/download/3.7.1/openfasttrace-3.7.1.jar)
* [Maven Central](https://repo1.maven.org/maven2/org/itsallcode/openfasttrace/openfasttrace/3.8.0/openfasttrace-3.8.0.jar)
* [GitHub](https://github.com/itsallcode/openfasttrace/releases/download/3.8.0/openfasttrace-3.8.0.jar)

Check our [developer guide](doc/developer_guide.md#getting-the-openfasttrace-library) to learn how to use the OFT JAR as dependency in your own code with popular build tools.

Expand All @@ -94,7 +94,7 @@ If you just want to run OFT:
The most basic variant to run OpenFastTrace is directly from the JAR file via the command line:

```bash
java -jar product/target/openfasttrace-3.7.1.jar trace /path/to/directory/being/traced
java -jar product/target/openfasttrace-3.8.0.jar trace /path/to/directory/being/traced
```

If you want to run OFT automatically as part of a continuous build, we recommend using our plugins for [Gradle](https://github.com/itsallcode/openfasttrace-gradle) and [Maven](https://github.com/itsallcode/openfasttrace-maven-plugin).
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.itsallcode.openfasttrace.api.importer.input;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.*;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
Expand Down Expand Up @@ -51,7 +50,12 @@ public static InputFile forPath(final Path path, final Charset charset)
@Override
public BufferedReader createReader() throws IOException
{
return Files.newBufferedReader(this.path, this.charset);
// Don't fail when reading files with invalid encoding.
// Files.newBufferedReader(this.path, this.charset) won't work here.
// See https://stackoverflow.com/a/43446789 for details.
final InputStream stream = Files.newInputStream(this.path);
final InputStreamReader reader = new InputStreamReader(stream, this.charset);
return new BufferedReader(reader);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,7 @@
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.*;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -100,6 +98,15 @@ void testReadWithUtf8Encoding() throws IOException
assertThat(readContent(inputFile), equalTo(CONTENT));
}

@Test
void testReadFileWithInvalidEncoding2() throws IOException
{
final Path path = tempDir.resolve("file");
Files.write(path, new byte[] { (byte) 0x9F, (byte) 0x88 });
final InputFile inputFile = RealFileInput.forPath(path, StandardCharsets.UTF_8);
assertThat(readContent(inputFile), equalTo("��"));
}

private Path writeTempFile(final String content, final Charset charset) throws IOException
{
final Path path = this.tempDir.resolve("test");
Expand Down
3 changes: 2 additions & 1 deletion doc/changes/changes_3.8.0.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# OpenFastTrace 3.8.0, released 2024-02-??
# OpenFastTrace 3.8.0, released 2024-02-26

Code name: RST Importer

Expand Down Expand Up @@ -29,6 +29,7 @@ Now you can also specify a revision for coverage tags instead of the default rev
* #373: Ignore spaces after items in "Needs:" and "Tags:" lists (thanks to [@sambishop](https://github.com/sambishop) for his contribution!)
* #378: Merged integration test coverage with unit test coverage for representative overall figure
* #303: Escape special characters in HTML report to avoid broken HTML when a specification item contains text like `<section>`
* #391: Fixed reading files with invalid encoding (thanks to [@ayankuma](https://github.com/ayankuma) for the bug report!)

## Refactoring

Expand Down
4 changes: 2 additions & 2 deletions doc/developer_guide.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ To use OpenFastTrace as a dependency in your [Maven](https://maven.apache.org) p
<dependency>
<groupId>org.itsallcode.openfasttrace</groupId>
<artifactId>openfasttrace</artifactId>
<version>3.7.1</version>
<version>3.8.0</version>
<scope>compile</scope>
</dependency>
</dependencies>
Expand All @@ -27,7 +27,7 @@ To use OpenFastTrace as a dependency in your [Gradle](https://gradle.org/) proje

```groovy
dependencies {
compile "org.itsallcode.openfasttrace:openfasttrace:3.7.1"
compile "org.itsallcode.openfasttrace:openfasttrace:3.8.0"
}
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ void testImportZipWithMultipleFilesAssertFileContent() throws IOException
assertThat(this.actualFileContent.get(1), equalTo(FILE_CONTENT2_STRING));
}

@Test
void testImportZipWithInvalidEncoding() throws IOException
{
initializeZipFile();
addEntryToZip("file1.c", new byte[] { (byte) 0x9F, (byte) 0x88 });
final List<InputFile> importedFiles = runImporter(1);
assertThat(importedFiles.get(0).getPath(), equalTo(this.zipFile.getPath() + "!file1.c"));
assertThat(this.actualFileContent.get(0), equalTo("��"));
}

private void addZipEntryDirectory(final String name) throws IOException
{
assertThat(name, not(Matchers.endsWith("/")));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,10 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.*;
import java.nio.charset.StandardCharsets;
import java.nio.file.Path;
import java.util.zip.ZipEntry;
import java.util.zip.ZipFile;
import java.util.zip.ZipOutputStream;
import java.util.zip.*;

import org.itsallcode.openfasttrace.api.importer.ImporterException;
import org.itsallcode.openfasttrace.api.importer.input.InputFile;
Expand Down Expand Up @@ -118,6 +114,18 @@ void testReadContentIso() throws IOException
}
}

@Test
void testReadInvalidEncoding() throws IOException
{
addEntryToZip("file", new byte[] { (byte) 0x9F, (byte) 0x88 });
try (final ZipFile zip = getZipFile())
{
final InputFile inputFile = ZipEntryInput.forZipEntry(zip, new ZipEntry("file"),
StandardCharsets.UTF_8);
assertThat(readContent(inputFile), equalTo("��"));
}
}

private String readContent(final InputFile inputFile) throws IOException
{
return inputFile.createReader().lines().collect(joining("\n"));
Expand Down
2 changes: 1 addition & 1 deletion parent/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
<description>Free requirement tracking suite</description>
<url>https://github.com/itsallcode/openfasttrace</url>
<properties>
<revision>3.7.2</revision>
<revision>3.8.0</revision>
<java.version>11</java.version>
<junit.version>5.10.2</junit.version>
<maven.surefire.version>3.2.5</maven.surefire.version>
Expand Down
7 changes: 7 additions & 0 deletions product/src/test/resources/example/src/invalid_encoding.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Test case for https://github.com/itsallcode/openfasttrace/issues/391
This is used in test case org.itsallcode.openfasttrace.ITestExampleProject
This is based on https://xenbits.xen.org/gitweb/?p=xen.git;a=blob;f=xen/drivers/video/font_8x14.c;h=e80d178c3869a4b506313cdea19c76b18fe2b4c2;hb=HEAD

Ensure to not modify the rest of this file when editing it!

/* 128 0x80 '�� */

0 comments on commit ea8baaf

Please sign in to comment.