1
0
mirror of https://codeberg.org/Freeyourgadget/Gadgetbridge synced 2024-07-22 14:52:25 +02:00

fix indentation

This commit is contained in:
MPeter 2022-11-05 11:26:24 +01:00
parent e75f80c3f9
commit 4c14dd5f72
3 changed files with 224 additions and 224 deletions

View File

@ -17,100 +17,100 @@ import androidx.annotation.Nullable;
* Utility class for recognition and reading of ZIP archives. * Utility class for recognition and reading of ZIP archives.
*/ */
public class ZipFile { public class ZipFile {
private static final Logger LOG = LoggerFactory.getLogger(ZipFile.class); private static final Logger LOG = LoggerFactory.getLogger(ZipFile.class);
public static final byte[] ZIP_HEADER = new byte[]{ public static final byte[] ZIP_HEADER = new byte[]{
0x50, 0x4B, 0x03, 0x04 0x50, 0x4B, 0x03, 0x04
}; };
private final byte[] zipBytes; private final byte[] zipBytes;
/** /**
* Open ZIP file from byte array already in memory. * Open ZIP file from byte array already in memory.
* @param zipBytes data to handle as a ZIP file. * @param zipBytes data to handle as a ZIP file.
*/ */
public ZipFile(byte[] zipBytes) { public ZipFile(byte[] zipBytes) {
this.zipBytes = zipBytes; this.zipBytes = zipBytes;
} }
/** /**
* Open ZIP file from InputStream.<br> * Open ZIP file from InputStream.<br>
* This will read the entire file into memory at once. * This will read the entire file into memory at once.
* @param inputStream data to handle as a ZIP file. * @param inputStream data to handle as a ZIP file.
*/ */
public ZipFile(InputStream inputStream) throws IOException { public ZipFile(InputStream inputStream) throws IOException {
this.zipBytes = readAllBytes(inputStream); this.zipBytes = readAllBytes(inputStream);
} }
/** /**
* Checks if data resembles a ZIP file.<br> * Checks if data resembles a ZIP file.<br>
* The check is not infallible: it may report self-extracting or other exotic ZIP archives as not a ZIP file, and it may report a corrupted ZIP file as a ZIP file. * The check is not infallible: it may report self-extracting or other exotic ZIP archives as not a ZIP file, and it may report a corrupted ZIP file as a ZIP file.
* @param data The data to check. * @param data The data to check.
* @return Whether data resembles a ZIP file. * @return Whether data resembles a ZIP file.
*/ */
public static boolean isZipFile(byte[] data) { public static boolean isZipFile(byte[] data) {
return ArrayUtils.equals(data, ZIP_HEADER, 0); return ArrayUtils.equals(data, ZIP_HEADER, 0);
} }
/** /**
* Reads the contents of file at path into a byte array. * Reads the contents of file at path into a byte array.
* @param path Path of the file in the ZIP file. * @param path Path of the file in the ZIP file.
* @return byte array contatining the contents of the requested file. * @return byte array contatining the contents of the requested file.
* @throws ZipFileException If the specified path does not exist or references a directory, or if some other I/O error occurs. In other words, if return value would otherwise be null. * @throws ZipFileException If the specified path does not exist or references a directory, or if some other I/O error occurs. In other words, if return value would otherwise be null.
*/ */
public byte[] getFileFromZip(final String path) throws ZipFileException { public byte[] getFileFromZip(final String path) throws ZipFileException {
try (InputStream is = new ByteArrayInputStream(zipBytes); ZipInputStream zipInputStream = new ZipInputStream(is)) { try (InputStream is = new ByteArrayInputStream(zipBytes); ZipInputStream zipInputStream = new ZipInputStream(is)) {
ZipEntry zipEntry; ZipEntry zipEntry;
while ((zipEntry = zipInputStream.getNextEntry()) != null) { while ((zipEntry = zipInputStream.getNextEntry()) != null) {
if (!zipEntry.getName().equals(path)) continue; // TODO: is this always a path? The documentation is very vague. if (!zipEntry.getName().equals(path)) continue; // TODO: is this always a path? The documentation is very vague.
if (zipEntry.isDirectory()) { if (zipEntry.isDirectory()) {
throw new ZipFileException(String.format("Path in ZIP file is a directory: %s", path)); throw new ZipFileException(String.format("Path in ZIP file is a directory: %s", path));
}
return readAllBytes(zipInputStream);
} }
return readAllBytes(zipInputStream); throw new ZipFileException(String.format("Path in ZIP file was not found: %s", path));
}
throw new ZipFileException(String.format("Path in ZIP file was not found: %s", path)); } catch (ZipException e) {
throw new ZipFileException("The ZIP file might be corrupted");
} catch (IOException e) {
throw new ZipFileException("General IO error");
}
}
} catch (ZipException e) { /**
throw new ZipFileException("The ZIP file might be corrupted"); * Tries to obtain file from ZIP file without much hassle, but is not safe.<br>
} catch (IOException e) { * Please only use this in place of old code where correctness of the result is checked only later on.<br>
throw new ZipFileException("General IO error"); * Use getFileFromZip of ZipFile instance instead.
} * @param zipBytes
} * @param path Path of the file in the ZIP file.
* @return Contents of requested file or null.
*/
@Deprecated
@Nullable
public static byte[] tryReadFileQuick(final byte[] zipBytes, final String path) {
try {
return new ZipFile(zipBytes).getFileFromZip(path);
} catch (ZipFileException e) {
LOG.error("Quick ZIP reading failed.", e);
} catch (Exception e) {
LOG.error("Unable to close ZipFile.", e);
}
/** return null;
* Tries to obtain file from ZIP file without much hassle, but is not safe.<br> }
* Please only use this in place of old code where correctness of the result is checked only later on.<br>
* Use getFileFromZip of ZipFile instance instead.
* @param zipBytes
* @param path Path of the file in the ZIP file.
* @return Contents of requested file or null.
*/
@Deprecated
@Nullable
public static byte[] tryReadFileQuick(final byte[] zipBytes, final String path) {
try {
return new ZipFile(zipBytes).getFileFromZip(path);
} catch (ZipFileException e) {
LOG.error("Quick ZIP reading failed.", e);
} catch (Exception e) {
LOG.error("Unable to close ZipFile.", e);
}
return null; private static byte[] readAllBytes(final InputStream is) throws IOException {
} final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
private static byte[] readAllBytes(final InputStream is) throws IOException { int n;
final ByteArrayOutputStream buffer = new ByteArrayOutputStream(); byte[] buf = new byte[16384];
int n; while ((n = is.read(buf, 0, buf.length)) != -1) {
byte[] buf = new byte[16384]; buffer.write(buf, 0, n);
}
while ((n = is.read(buf, 0, buf.length)) != -1) { return buffer.toByteArray();
buffer.write(buf, 0, n); }
}
return buffer.toByteArray();
}
} }

View File

@ -1,7 +1,7 @@
package nodomain.freeyourgadget.gadgetbridge.util; package nodomain.freeyourgadget.gadgetbridge.util;
public class ZipFileException extends Exception { public class ZipFileException extends Exception {
public ZipFileException(String message) { public ZipFileException(String message) {
super(String.format("Error while reading ZIP file: %s", message)); super(String.format("Error while reading ZIP file: %s", message));
} }
} }

View File

@ -16,152 +16,152 @@ import nodomain.freeyourgadget.gadgetbridge.util.ZipFile;
import nodomain.freeyourgadget.gadgetbridge.util.ZipFileException; import nodomain.freeyourgadget.gadgetbridge.util.ZipFileException;
public class ZipFileTest extends TestBase { public class ZipFileTest extends TestBase {
private static final String TEST_FILE_NAME = "manifest.json"; private static final String TEST_FILE_NAME = "manifest.json";
private static final String TEST_NESTED_FILE_NAME = "directory/manifest.json"; private static final String TEST_NESTED_FILE_NAME = "directory/manifest.json";
private static final String TEST_FILE_CONTENTS_1 = "{ \"mykey\": \"myvalue\", \"myarr\": [0, 1, 2, 3] }"; private static final String TEST_FILE_CONTENTS_1 = "{ \"mykey\": \"myvalue\", \"myarr\": [0, 1, 2, 3] }";
private static final String TEST_FILE_CONTENTS_2 = "{\n" + private static final String TEST_FILE_CONTENTS_2 = "{\n" +
" \"manifest\": {\n" + " \"manifest\": {\n" +
" \"application\": {\n" + " \"application\": {\n" +
" \"bin_file\": \"pinetime-mcuboot-app-image-1.10.0.bin\",\n" + " \"bin_file\": \"pinetime-mcuboot-app-image-1.10.0.bin\",\n" +
" \"dat_file\": \"pinetime-mcuboot-app-image-1.10.0.dat\",\n" + " \"dat_file\": \"pinetime-mcuboot-app-image-1.10.0.dat\",\n" +
" \"init_packet_data\": {\n" + " \"init_packet_data\": {\n" +
" \"application_version\": 4294967295,\n" + " \"application_version\": 4294967295,\n" +
" \"device_revision\": 65535,\n" + " \"device_revision\": 65535,\n" +
" \"device_type\": 82,\n" + " \"device_type\": 82,\n" +
" \"firmware_crc16\": 21770,\n" + " \"firmware_crc16\": 21770,\n" +
" \"softdevice_req\": [\n" + " \"softdevice_req\": [\n" +
" 65534\n" + " 65534\n" +
" ]\n" + " ]\n" +
" }\n" + " }\n" +
" },\n" + " },\n" +
" \"dfu_version\": 0.5\n" + " \"dfu_version\": 0.5\n" +
" }\n" + " }\n" +
"}"; "}";
@Test @Test
public void testZipSize1() throws IOException, ZipFileException { public void testZipSize1() throws IOException, ZipFileException {
final String contents = TEST_FILE_CONTENTS_1; final String contents = TEST_FILE_CONTENTS_1;
byte[] zipArchive = createZipArchive(TEST_FILE_NAME, contents); byte[] zipArchive = createZipArchive(TEST_FILE_NAME, contents);
ZipFile zipFile = new ZipFile(zipArchive); ZipFile zipFile = new ZipFile(zipArchive);
String readContents = new String(zipFile.getFileFromZip(TEST_FILE_NAME)); String readContents = new String(zipFile.getFileFromZip(TEST_FILE_NAME));
Assert.assertEquals(contents, readContents); Assert.assertEquals(contents, readContents);
}
@Test
public void testZipSize2() throws IOException, ZipFileException {
final String contents = TEST_FILE_CONTENTS_2;
byte[] zipArchive = createZipArchive(TEST_FILE_NAME, contents);
ZipFile zipFile = new ZipFile(zipArchive);
String readContents = new String(zipFile.getFileFromZip(TEST_FILE_NAME));
Assert.assertEquals(contents, readContents);
}
@Test
public void testZipSize3() throws IOException, ZipFileException, JSONException {
String contents = makeLargeJsonObject(new JSONObject(TEST_FILE_CONTENTS_2), 4).toString(4);
byte[] zipArchive = createZipArchive(TEST_FILE_NAME, contents);
ZipFile zipFile = new ZipFile(zipArchive);
String readContents = new String(zipFile.getFileFromZip(TEST_FILE_NAME));
Assert.assertEquals(contents, readContents);
}
@Test
public void testZipSize4() throws IOException, ZipFileException, JSONException {
String contents = makeLargeJsonObject(new JSONObject(TEST_FILE_CONTENTS_2), 32).toString(4);
byte[] zipArchive = createZipArchive(TEST_FILE_NAME, contents);
ZipFile zipFile = new ZipFile(zipArchive);
String readContents = new String(zipFile.getFileFromZip(TEST_FILE_NAME));
Assert.assertEquals(contents, readContents);
}
@Test
public void testZipFileInDir() throws IOException, ZipFileException {
String contents = TEST_FILE_CONTENTS_1;
byte[] zipArchive = createZipArchive(TEST_NESTED_FILE_NAME, contents);
ZipFile zipFile = new ZipFile(zipArchive);
String readContents = new String(zipFile.getFileFromZip(TEST_NESTED_FILE_NAME));
Assert.assertEquals(contents, readContents);
}
@Test
public void testZipFilesUnorderedAccess() throws IOException, ZipFileException {
String contents1 = TEST_FILE_CONTENTS_1;
String contents2 = TEST_FILE_CONTENTS_2;
String contents3 = "zbuMyWvIxeKgcWnsSYOd8CTLgjc9x7ti21OlLlGduMJVXlKc835WEUKJ3xR6GDA5d0tHSXnYxZkDlznFQVyueHhwYywsMO9PlkJqjOCA2Mn8uTuTliIKUNPBraFipOodb6rW31HdKLOd7gmniLF5mvdRPHOUKIXSMciqogOsZnvGXylMx6TegesGBWAeHFhTSQ5xXOrOEUsDHK78M3A0yFXzLE0XgwI90Tl87OHyWfE0y0yINv5PjxgGCLUB7mHYFpgPW1C5yyIkb2JA6CePE3hHv369khwmLumW7P9ErZhzdGgeskz6Os0p5HMrTFuySc0PWxsIfru1HldIH9TZTSMCbd91G5jCCikyx2zrzDKaasuQZyBGZcMjr1zcCLpPQiKT7ELSoUBCKhiFODxbFA06MC5bLXh2WvyP8W2kVxT2T4AnDX6pwf1BKs4nbHpAjvMmHrzlhQp7Q6VWBEiniY5M9QW4ExRcMGIBYXvY7vu5p";
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ZipOutputStream zipWriteStream = new ZipOutputStream(baos);
writeFileToZip(contents1, "file1", zipWriteStream);
writeFileToZip(contents2, "file2", zipWriteStream);
writeFileToZip(contents3, "file3", zipWriteStream);
zipWriteStream.close();
ZipFile zipFile = new ZipFile(baos.toByteArray());
String readContents2 = new String(zipFile.getFileFromZip("file2"));
String readContents1 = new String(zipFile.getFileFromZip("file1"));
String readContents3 = new String(zipFile.getFileFromZip("file3"));
Assert.assertEquals(contents1, readContents1);
Assert.assertEquals(contents2, readContents2);
Assert.assertEquals(contents3, readContents3);
}
/**
* Create a ZIP archive with a single text file.
* The archive will not be saved to a file, it is kept in memory.
*
* @return the ZIP archive
*/
private byte[] createZipArchive(String path, String fileContents) throws IOException {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ZipOutputStream zipFile = new ZipOutputStream(baos);
writeFileToZip(fileContents, path, zipFile);
zipFile.close();
return baos.toByteArray();
}
/**
* Make a larger JSON object for testing purposes, based on a preexisting JSON object.
*/
private JSONObject makeLargeJsonObject(JSONObject base, int repetitions) throws JSONException {
JSONObject manifestObj = base.getJSONObject("manifest");
JSONArray array = new JSONArray();
for (int i = 0; i < repetitions; i++) {
array.put(manifestObj);
} }
return base.put("array", array); @Test
} public void testZipSize2() throws IOException, ZipFileException {
final String contents = TEST_FILE_CONTENTS_2;
/** byte[] zipArchive = createZipArchive(TEST_FILE_NAME, contents);
* Write given data to file at given path into an already opened ZIP archive.
* Allows to create an archive with multiple files.
*/
private void writeFileToZip(String fileContents, String path, ZipOutputStream zipFile) throws IOException {
byte[] data = fileContents.getBytes(StandardCharsets.UTF_8);
ZipEntry zipEntry = new ZipEntry(path); ZipFile zipFile = new ZipFile(zipArchive);
zipFile.putNextEntry(zipEntry); String readContents = new String(zipFile.getFileFromZip(TEST_FILE_NAME));
zipFile.write(data, 0, fileContents.length());
zipFile.closeEntry(); Assert.assertEquals(contents, readContents);
} }
@Test
public void testZipSize3() throws IOException, ZipFileException, JSONException {
String contents = makeLargeJsonObject(new JSONObject(TEST_FILE_CONTENTS_2), 4).toString(4);
byte[] zipArchive = createZipArchive(TEST_FILE_NAME, contents);
ZipFile zipFile = new ZipFile(zipArchive);
String readContents = new String(zipFile.getFileFromZip(TEST_FILE_NAME));
Assert.assertEquals(contents, readContents);
}
@Test
public void testZipSize4() throws IOException, ZipFileException, JSONException {
String contents = makeLargeJsonObject(new JSONObject(TEST_FILE_CONTENTS_2), 32).toString(4);
byte[] zipArchive = createZipArchive(TEST_FILE_NAME, contents);
ZipFile zipFile = new ZipFile(zipArchive);
String readContents = new String(zipFile.getFileFromZip(TEST_FILE_NAME));
Assert.assertEquals(contents, readContents);
}
@Test
public void testZipFileInDir() throws IOException, ZipFileException {
String contents = TEST_FILE_CONTENTS_1;
byte[] zipArchive = createZipArchive(TEST_NESTED_FILE_NAME, contents);
ZipFile zipFile = new ZipFile(zipArchive);
String readContents = new String(zipFile.getFileFromZip(TEST_NESTED_FILE_NAME));
Assert.assertEquals(contents, readContents);
}
@Test
public void testZipFilesUnorderedAccess() throws IOException, ZipFileException {
String contents1 = TEST_FILE_CONTENTS_1;
String contents2 = TEST_FILE_CONTENTS_2;
String contents3 = "zbuMyWvIxeKgcWnsSYOd8CTLgjc9x7ti21OlLlGduMJVXlKc835WEUKJ3xR6GDA5d0tHSXnYxZkDlznFQVyueHhwYywsMO9PlkJqjOCA2Mn8uTuTliIKUNPBraFipOodb6rW31HdKLOd7gmniLF5mvdRPHOUKIXSMciqogOsZnvGXylMx6TegesGBWAeHFhTSQ5xXOrOEUsDHK78M3A0yFXzLE0XgwI90Tl87OHyWfE0y0yINv5PjxgGCLUB7mHYFpgPW1C5yyIkb2JA6CePE3hHv369khwmLumW7P9ErZhzdGgeskz6Os0p5HMrTFuySc0PWxsIfru1HldIH9TZTSMCbd91G5jCCikyx2zrzDKaasuQZyBGZcMjr1zcCLpPQiKT7ELSoUBCKhiFODxbFA06MC5bLXh2WvyP8W2kVxT2T4AnDX6pwf1BKs4nbHpAjvMmHrzlhQp7Q6VWBEiniY5M9QW4ExRcMGIBYXvY7vu5p";
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ZipOutputStream zipWriteStream = new ZipOutputStream(baos);
writeFileToZip(contents1, "file1", zipWriteStream);
writeFileToZip(contents2, "file2", zipWriteStream);
writeFileToZip(contents3, "file3", zipWriteStream);
zipWriteStream.close();
ZipFile zipFile = new ZipFile(baos.toByteArray());
String readContents2 = new String(zipFile.getFileFromZip("file2"));
String readContents1 = new String(zipFile.getFileFromZip("file1"));
String readContents3 = new String(zipFile.getFileFromZip("file3"));
Assert.assertEquals(contents1, readContents1);
Assert.assertEquals(contents2, readContents2);
Assert.assertEquals(contents3, readContents3);
}
/**
* Create a ZIP archive with a single text file.
* The archive will not be saved to a file, it is kept in memory.
*
* @return the ZIP archive
*/
private byte[] createZipArchive(String path, String fileContents) throws IOException {
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ZipOutputStream zipFile = new ZipOutputStream(baos);
writeFileToZip(fileContents, path, zipFile);
zipFile.close();
return baos.toByteArray();
}
/**
* Make a larger JSON object for testing purposes, based on a preexisting JSON object.
*/
private JSONObject makeLargeJsonObject(JSONObject base, int repetitions) throws JSONException {
JSONObject manifestObj = base.getJSONObject("manifest");
JSONArray array = new JSONArray();
for (int i = 0; i < repetitions; i++) {
array.put(manifestObj);
}
return base.put("array", array);
}
/**
* Write given data to file at given path into an already opened ZIP archive.
* Allows to create an archive with multiple files.
*/
private void writeFileToZip(String fileContents, String path, ZipOutputStream zipFile) throws IOException {
byte[] data = fileContents.getBytes(StandardCharsets.UTF_8);
ZipEntry zipEntry = new ZipEntry(path);
zipFile.putNextEntry(zipEntry);
zipFile.write(data, 0, fileContents.length());
zipFile.closeEntry();
}
} }