}
}
+project(':northside-io') {
+ apply plugin: 'java-library'
+ version = rootProject.version
+ dependencies {
+ implementation 'commons-io:commons-io:2.11.0'
+ }
+}
+
project(':dictzip-lib') {
apply plugin: 'java-library'
version = rootProject.version
+
+ dependencies {
+ testImplementation project(':northside-io')
+ }
+
publishing {
publications {
mavenJava(MavenPublication) {
dependencies {
implementation project(':dictzip-lib')
implementation 'gnu.getopt:java-getopt:1.0.13'
+ testImplementation project(':northside-io')
}
task mandoc(type: Copy) {
from "doc/dictzip.1.in"
* @param name input filename.
* @return output filename.
*/
- protected static String uncompressedFileName(final String name) {
+ static String uncompressedFileName(final String name) {
String result;
if (name.endsWith(".dz") || name.endsWith(".gz")) {
result = name.substring(0, name.length() - 3);
* @param name input file name.
* @return output filename.
*/
- protected static String compressedFileName(final String name) {
+ static String compressedFileName(final String name) {
return name + ".dz";
}
/**
* Test of doZip method, of class DictData.
+ * @param tempDir JUnit5 temporary directory support
* @throws java.lang.Exception if file operation failed.
*/
@Test
- public void testDoZip(@TempDir Path tempDir) throws Exception {
+ public void testDoZip(@TempDir final Path tempDir) throws Exception {
Path testFile = Paths.get(Objects.requireNonNull(
this.getClass().getResource("/test_dozip.dict")).toURI());
Path zippedFile = tempDir.resolve("test_dozip.dict.dz");
/**
* Test of doUnzip method, of class DictData.
+ * @param tempDir JUnit5 temporary directory support
* @throws java.lang.Exception if file operation failed.
*/
@Test
- public void testDoUnzip(@TempDir Path tempDir) throws Exception {
+ public void testDoUnzip(@TempDir final Path tempDir) throws Exception {
String dzFile = this.getClass().getResource("/test.dict.dz").getFile();
Path decompressed = tempDir.resolve("test.dict");
long start = 0L;
+++ /dev/null
-/*
- * FileUtils library.
- *
- * Copyright (C) 2016 Hiroshi Miura
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package tokyo.northside.io;
-
-import java.io.File;
-import java.io.FileInputStream;
-import java.io.IOException;
-
-import org.apache.commons.io.FileUtils;
-
-
-/**
- * General File manipulation utility.
- * <p>
- * This class provides static utility methods for input/output operations.
- * <ul>
- * <li>contentEquals - these methods compare the content of two files
- * </ul>
- * <p>
- * The methods in this class that read a file are buffered internally.
- * The default buffer size of 4K has been shown to be efficient in tests.
- * <p>
- * Created by Hiroshi Miura on 16/04/09.
- *
- * @author Hiroshi Miura
- */
-public final class FileUtils2 extends FileUtils {
-
- /**
- * Compare file contents in range. Both files must be files (not directories) and exist.
- *
- * @param first first file
- * @param second second file
- * @param off compare from offset
- * @param len comparison length
- * @return boolean true if files are equal, otherwise false
- * @throws IOException error in function
- */
- public static boolean contentEquals(final File first, final File second, final long off,
- final long len) throws IOException {
- boolean result = false;
-
- if (len < 1) {
- throw new IllegalArgumentException();
- }
- if (off < 0) {
- throw new IllegalArgumentException();
- }
-
- if ((first.exists()) && (second.exists())
- && (first.isFile()) && (second.isFile())) {
- if (first.getCanonicalPath().equals(second.getCanonicalPath())) {
- result = true;
- } else {
- FileInputStream firstInput = null;
- FileInputStream secondInput = null;
-
- try {
- firstInput = new FileInputStream(first);
- secondInput = new FileInputStream(second);
- result = IOUtils2.contentEquals(firstInput, secondInput, off, len);
- } catch (RuntimeException e) {
- throw e;
- } catch (IOException ioe) {
- throw ioe;
- } finally {
- org.apache.commons.io.IOUtils.closeQuietly(firstInput);
- org.apache.commons.io.IOUtils.closeQuietly(secondInput);
- }
- }
- }
- return result;
- }
-
-}
+++ /dev/null
-/*
- * FileUtils library.
- *
- * Copyright (C) 2016 Hiroshi Miura
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package tokyo.northside.io;
-
-import java.io.InputStream;
-import java.io.IOException;
-import java.util.Arrays;
-
-import org.apache.commons.io.IOUtils;
-
-/**
- * General IO stream manipulation utility.
- * <p>
- * This class provides static utility methods for input/output operations.
- * <ul>
- * <li>contentEquals - these methods compare the content of two streams
- * </ul>
- * <p>
- * The methods in this class that read a stream are buffered internally.
- * This means that there is no cause to use a <code>BufferedInputStream</code>
- * or <code>BufferedReader</code>. The default buffer size of 4K has been shown
- * to be efficient in tests.
- * <p>
- * Wherever possible, the methods in this class do <em>not</em> flush or close
- * the stream. This is to avoid making non-portable assumptions about the
- * streams' origin and further use. Thus the caller is still responsible for
- * closing streams after use.
- * <p>
- * Created by Hiroshi Miura on 16/04/09.
- *
- * @author Hiroshi Miura
- */
-public final class IOUtils2 extends IOUtils {
-
- private static final int BUF_LEN = 4096;
-
- /**
- * Compare the contents of two Streams to determine if they are equal or not.
- *
- * @param first first input stream.
- * @param second second input stream.
- * @param off compare from offset
- * @param len comparison length
- * @return boolean true if content of input streams are equal, true if streams are equal,
- * otherwise false.
- * @throws IOException when I/O error occurred.
- */
- public static boolean contentEquals(final InputStream first, final InputStream second,
- final long off, final long len) throws IOException {
- boolean result;
-
- if (len < 1) {
- throw new IllegalArgumentException();
- }
- if (off < 0) {
- throw new IllegalArgumentException();
- }
- if (first.equals(second)) {
- return false;
- }
-
- try {
- byte[] firstBytes = new byte[BUF_LEN];
- byte[] secondBytes = new byte[BUF_LEN];
-
- if (off > 0) {
- long totalSkipped = 0;
- while (totalSkipped < off) {
- long skipped = first.skip(off - totalSkipped);
- if (skipped == 0) {
- throw new IOException("Cannot seek offset bytes.");
- }
- totalSkipped += skipped;
- }
- totalSkipped = 0;
- while (totalSkipped < off) {
- long skipped = second.skip(off - totalSkipped);
- if (skipped == 0) {
- throw new IOException("Cannot seek offset bytes.");
- }
- totalSkipped += skipped;
- }
- }
-
- long readLengthTotal = 0;
- result = true;
- while (readLengthTotal < len) {
- int readLength = BUF_LEN;
- if (len - readLengthTotal < (long) BUF_LEN) {
- readLength = (int) (len - readLengthTotal);
- }
- int lenFirst = first.read(firstBytes, 0, readLength);
- int lenSecond = second.read(secondBytes, 0, readLength);
- if (lenFirst != lenSecond) {
- result = false;
- break;
- }
- if ((lenFirst < 0) && (lenSecond < 0)) {
- result = true;
- break;
- }
- readLengthTotal += lenFirst;
- if (lenFirst < firstBytes.length) {
- byte[] a = Arrays.copyOfRange(firstBytes, 0, lenFirst);
- byte[] b = Arrays.copyOfRange(secondBytes, 0, lenSecond);
- if (!Arrays.equals(a, b)) {
- result = false;
- break;
- }
- } else if (!Arrays.equals(firstBytes, secondBytes)) {
- result = false;
- break;
- }
- }
- } catch (RuntimeException e) {
- throw e;
- } catch (IOException ioe) {
- throw ioe;
- }
- return result;
- }
-
- /**
- * Static utility should not be instantiated.
- */
- private IOUtils2() {
- }
-}
dout.finish();
}
// check archive
+ String[] command = {"/usr/bin/dictzip", "-d", "-c", "-s", null, "-e", "10", zippedPath.toAbsolutePath().toString()};
for (int i = 0; i < positions.length; i++) {
System.out.printf("seek position: %d%n", positions[i]);
- Process process = Runtime.getRuntime().exec(
- String.format("dictzip -d -c -k -s %d -e %d %s",
- positions[i], 10, zippedPath.toAbsolutePath()));
+ command[4] = Integer.toString(positions[i]);
+ Process process = Runtime.getRuntime().exec(command);
int b = process.getInputStream().read();
int returnCode = process.waitFor();
assertEquals(0, returnCode);
}
}
// create dictzip archive with dictzip command
- Process process = Runtime.getRuntime().exec(String.format("dictzip %s", outTextPath.toAbsolutePath()));
+ String[] command = {"/usr/bin/dictzip", outTextPath.toAbsolutePath().toString()};
+ Process process = Runtime.getRuntime().exec(command);
int returnCode = process.waitFor();
assertEquals(0, returnCode);
File zippedFile = tempDir.resolve("DictZipText.txt.dz").toFile();
dout.finish();
}
// check archive
+ String[] command = {"/usr/bin/dictzip", "-d", "-c", "-s", null, "-e", "10", zippedPath.toAbsolutePath().toString()};
for (int i = 0; i < positions.length; i++) {
System.out.printf("seek position: %d%n", positions[i]);
- Process process = Runtime.getRuntime().exec(
- String.format("dictzip -d -c -k -s %d -e %d %s",
- positions[i], 10, zippedPath.toAbsolutePath()));
+ command[4] = Integer.toString(positions[i]);
+ Process process = Runtime.getRuntime().exec(command);
int b = process.getInputStream().read();
int returnCode = process.waitFor();
assertEquals(0, returnCode);
--- /dev/null
+package org.dict.zip;
+
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.io.IOException;
+
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class DictZipFileUtilsTest {
+
+ /**
+ * Check dictzip inputstream.
+ * @throws Exception when fails.
+ */
+ @Test
+ public void testCheckDictZipInputStreamString() throws Exception {
+ String targetFile = this.getClass().getResource("/test.dict.dz").getFile();
+ Assertions.assertTrue(DictZipFileUtils.checkDictZipInputStream(targetFile));
+ }
+
+ /**
+ * Check dictzip input streasm which is not exist.
+ */
+ @Test
+ public void testCheckDictZipInputStreamStringNoExist() {
+ String targetFile = "false.dict.dz";
+ boolean result;
+ try {
+ DictZipFileUtils.checkDictZipInputStream(targetFile);
+ result = false;
+ } catch (IOException e) {
+ // expected.
+ result = true;
+ }
+ assertTrue(result);
+ }
+
+ /**
+ * Check dictzip input stream.
+ * @throws Exception when fails.
+ */
+ @Test
+ public void testCheckDictZipInputStream() throws Exception {
+ String targetFile = this.getClass().getResource("/test.dict.dz").getFile();
+ try (DictZipInputStream dzin = new DictZipInputStream(new
+ RandomAccessInputStream(targetFile, "r"))) {
+ Assertions.assertTrue(DictZipFileUtils.checkDictZipInputStream(dzin));
+ }
+ }
+}
projectOwner=miurahr
developerEmail=miurahr@linux.com
developerName='Hiroshi Miura'
-projectYears=2016-2021
+projectYears=2016-2022
projectReleaseBody="Fix release for critical bug on seek."
* obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
-package org.dict.zip;
-
+package tokyo.northside;
import org.junit.jupiter.api.Test;
import java.io.File;
-import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Objects;
File secondFile = new File(this.getClass().getResource("/test_util2.txt").getFile());
assertFalse(contentEquals(firstFile, secondFile, 0, 64));
}
-
- /**
- * Check dictzip inputstream.
- * @throws Exception when fails.
- */
- @Test
- public void testCheckDictZipInputStreamString() throws Exception {
- String targetFile = this.getClass().getResource("/test.dict.dz").getFile();
- assertTrue(DictZipFileUtils.checkDictZipInputStream(targetFile));
- }
-
- /**
- * Check dictzip input streasm which is not exist.
- */
- @Test
- public void testCheckDictZipInputStreamStringNoExist() {
- String targetFile = "false.dict.dz";
- boolean result;
- try {
- DictZipFileUtils.checkDictZipInputStream(targetFile);
- result = false;
- } catch (IOException e) {
- // expected.
- result = true;
- }
- assertTrue(result);
- }
-
- /**
- * Check dictzip input stream.
- * @throws Exception when fails.
- */
- @Test
- public void testCheckDictZipInputStream() throws Exception {
- String targetFile = this.getClass().getResource("/test.dict.dz").getFile();
- try (DictZipInputStream dzin = new DictZipInputStream(new
- RandomAccessInputStream(targetFile, "r"))) {
- assertTrue(DictZipFileUtils.checkDictZipInputStream(dzin));
- }
- }
}
rootProject.name = 'dictzip'
-include 'dictzip-lib', 'dictzip-cli'
+include 'northside-io', 'dictzip-lib', 'dictzip-cli'