mirror of
https://github.com/haraldk/TwelveMonkeys.git
synced 2025-08-04 03:55:28 -04:00
#656 Code clean-up + minor refactorings.
This commit is contained in:
parent
74611e4e52
commit
eced5b8efd
@ -292,21 +292,28 @@ public final class TIFFWriter extends MetadataWriter {
|
|||||||
|
|
||||||
private int getCount(final Entry entry) {
|
private int getCount(final Entry entry) {
|
||||||
Object value = entry.getValue();
|
Object value = entry.getValue();
|
||||||
|
|
||||||
if (value instanceof String) {
|
if (value instanceof String) {
|
||||||
return ((String) value).getBytes(StandardCharsets.UTF_8).length + 1;
|
return computeStringLength((String) value);
|
||||||
}
|
}
|
||||||
else if (value instanceof String[]) {
|
else if (value instanceof String[]) {
|
||||||
int sum = 0;
|
return computeStringLength((String[]) value);
|
||||||
for (String string : (String[]) value) {
|
|
||||||
sum += string.getBytes(StandardCharsets.UTF_8).length + 1;
|
|
||||||
}
|
|
||||||
return sum;
|
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
return entry.valueCount();
|
return entry.valueCount();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private int computeStringLength(String... values) {
|
||||||
|
int sum = 0;
|
||||||
|
|
||||||
|
for (String value : values) {
|
||||||
|
sum += value.getBytes(StandardCharsets.UTF_8).length + 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
private void writeValueInline(final Object value, final short type, final ImageOutputStream stream) throws IOException {
|
private void writeValueInline(final Object value, final short type, final ImageOutputStream stream) throws IOException {
|
||||||
if (value.getClass().isArray()) {
|
if (value.getClass().isArray()) {
|
||||||
switch (type) {
|
switch (type) {
|
||||||
@ -423,10 +430,7 @@ public final class TIFFWriter extends MetadataWriter {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case TIFF.TYPE_ASCII:
|
case TIFF.TYPE_ASCII:
|
||||||
String[] strings = (String[]) value;
|
writeStrings(stream, (String[]) value);
|
||||||
for (String string : strings) {
|
|
||||||
writeString(stream, string);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
throw new IllegalArgumentException("Unsupported TIFF type: " + type);
|
throw new IllegalArgumentException("Unsupported TIFF type: " + type);
|
||||||
@ -440,7 +444,7 @@ public final class TIFFWriter extends MetadataWriter {
|
|||||||
stream.writeByte(((Number) value).intValue());
|
stream.writeByte(((Number) value).intValue());
|
||||||
break;
|
break;
|
||||||
case TIFF.TYPE_ASCII:
|
case TIFF.TYPE_ASCII:
|
||||||
writeString(stream, (String) value);
|
writeStrings(stream, (String) value);
|
||||||
break;
|
break;
|
||||||
case TIFF.TYPE_SHORT:
|
case TIFF.TYPE_SHORT:
|
||||||
case TIFF.TYPE_SSHORT:
|
case TIFF.TYPE_SSHORT:
|
||||||
@ -477,10 +481,12 @@ public final class TIFFWriter extends MetadataWriter {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void writeString(ImageOutputStream stream, String value) throws IOException {
|
private void writeStrings(ImageOutputStream stream, String... values) throws IOException {
|
||||||
|
for (String value : values) {
|
||||||
stream.write(value.getBytes(StandardCharsets.UTF_8));
|
stream.write(value.getBytes(StandardCharsets.UTF_8));
|
||||||
stream.write(0);
|
stream.write(0);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private void writeValueAt(final long dataOffset, final Object value, final short type, final ImageOutputStream stream) throws IOException {
|
private void writeValueAt(final long dataOffset, final Object value, final short type, final ImageOutputStream stream) throws IOException {
|
||||||
writeOffset(stream, dataOffset);
|
writeOffset(stream, dataOffset);
|
||||||
|
@ -47,10 +47,7 @@ import java.util.ArrayList;
|
|||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import static org.junit.Assert.assertArrayEquals;
|
import static org.junit.Assert.*;
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
import static org.junit.Assert.assertNotNull;
|
|
||||||
import static org.junit.Assert.assertTrue;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* TIFFWriterTest
|
* TIFFWriterTest
|
||||||
@ -86,11 +83,13 @@ public class TIFFWriterTest extends MetadataWriterAbstractTest {
|
|||||||
Directory directory = new AbstractDirectory(entries) {};
|
Directory directory = new AbstractDirectory(entries) {};
|
||||||
|
|
||||||
ByteArrayOutputStream output = new FastByteArrayOutputStream(1024);
|
ByteArrayOutputStream output = new FastByteArrayOutputStream(1024);
|
||||||
ImageOutputStream imageStream = ImageIO.createImageOutputStream(output);
|
|
||||||
|
try (ImageOutputStream imageStream = ImageIO.createImageOutputStream(output)) {
|
||||||
createWriter().write(directory, imageStream);
|
createWriter().write(directory, imageStream);
|
||||||
imageStream.flush();
|
imageStream.flush();
|
||||||
|
|
||||||
assertEquals(output.size(), imageStream.getStreamPosition());
|
assertEquals(output.size(), imageStream.getStreamPosition());
|
||||||
|
}
|
||||||
|
|
||||||
byte[] data = output.toByteArray();
|
byte[] data = output.toByteArray();
|
||||||
|
|
||||||
@ -131,14 +130,15 @@ public class TIFFWriterTest extends MetadataWriterAbstractTest {
|
|||||||
Directory directory = new AbstractDirectory(entries) {};
|
Directory directory = new AbstractDirectory(entries) {};
|
||||||
|
|
||||||
ByteArrayOutputStream output = new FastByteArrayOutputStream(1024);
|
ByteArrayOutputStream output = new FastByteArrayOutputStream(1024);
|
||||||
ImageOutputStream imageStream = ImageIO.createImageOutputStream(output);
|
|
||||||
|
|
||||||
|
try (ImageOutputStream imageStream = ImageIO.createImageOutputStream(output)) {
|
||||||
imageStream.setByteOrder(ByteOrder.BIG_ENDIAN); // BE = Motorola
|
imageStream.setByteOrder(ByteOrder.BIG_ENDIAN); // BE = Motorola
|
||||||
|
|
||||||
createWriter().write(directory, imageStream);
|
createWriter().write(directory, imageStream);
|
||||||
imageStream.flush();
|
imageStream.flush();
|
||||||
|
|
||||||
assertEquals(output.size(), imageStream.getStreamPosition());
|
assertEquals(output.size(), imageStream.getStreamPosition());
|
||||||
|
}
|
||||||
|
|
||||||
byte[] data = output.toByteArray();
|
byte[] data = output.toByteArray();
|
||||||
|
|
||||||
@ -166,14 +166,15 @@ public class TIFFWriterTest extends MetadataWriterAbstractTest {
|
|||||||
Directory directory = new AbstractDirectory(entries) {};
|
Directory directory = new AbstractDirectory(entries) {};
|
||||||
|
|
||||||
ByteArrayOutputStream output = new FastByteArrayOutputStream(1024);
|
ByteArrayOutputStream output = new FastByteArrayOutputStream(1024);
|
||||||
ImageOutputStream imageStream = ImageIO.createImageOutputStream(output);
|
|
||||||
|
|
||||||
|
try (ImageOutputStream imageStream = ImageIO.createImageOutputStream(output)) {
|
||||||
imageStream.setByteOrder(ByteOrder.LITTLE_ENDIAN); // LE = Intel
|
imageStream.setByteOrder(ByteOrder.LITTLE_ENDIAN); // LE = Intel
|
||||||
|
|
||||||
createWriter().write(directory, imageStream);
|
createWriter().write(directory, imageStream);
|
||||||
imageStream.flush();
|
imageStream.flush();
|
||||||
|
|
||||||
assertEquals(output.size(), imageStream.getStreamPosition());
|
assertEquals(output.size(), imageStream.getStreamPosition());
|
||||||
|
}
|
||||||
|
|
||||||
byte[] data = output.toByteArray();
|
byte[] data = output.toByteArray();
|
||||||
|
|
||||||
@ -205,12 +206,13 @@ public class TIFFWriterTest extends MetadataWriterAbstractTest {
|
|||||||
Directory directory = new IFD(Collections.<Entry>singletonList(subIFD));
|
Directory directory = new IFD(Collections.<Entry>singletonList(subIFD));
|
||||||
|
|
||||||
ByteArrayOutputStream output = new FastByteArrayOutputStream(1024);
|
ByteArrayOutputStream output = new FastByteArrayOutputStream(1024);
|
||||||
ImageOutputStream imageStream = ImageIO.createImageOutputStream(output);
|
|
||||||
|
|
||||||
|
try (ImageOutputStream imageStream = ImageIO.createImageOutputStream(output)) {
|
||||||
createWriter().write(directory, imageStream);
|
createWriter().write(directory, imageStream);
|
||||||
imageStream.flush();
|
imageStream.flush();
|
||||||
|
|
||||||
assertEquals(output.size(), imageStream.getStreamPosition());
|
assertEquals(output.size(), imageStream.getStreamPosition());
|
||||||
|
}
|
||||||
|
|
||||||
Directory read = new TIFFReader().read(new ByteArrayImageInputStream(output.toByteArray()));
|
Directory read = new TIFFReader().read(new ByteArrayImageInputStream(output.toByteArray()));
|
||||||
|
|
||||||
@ -224,14 +226,10 @@ public class TIFFWriterTest extends MetadataWriterAbstractTest {
|
|||||||
Directory original = createReader().read(getDataAsIIS());
|
Directory original = createReader().read(getDataAsIIS());
|
||||||
|
|
||||||
ByteArrayOutputStream output = new FastByteArrayOutputStream(256);
|
ByteArrayOutputStream output = new FastByteArrayOutputStream(256);
|
||||||
ImageOutputStream imageOutput = ImageIO.createImageOutputStream(output);
|
|
||||||
|
|
||||||
try {
|
try (ImageOutputStream imageOutput = ImageIO.createImageOutputStream(output)) {
|
||||||
createWriter().write(original, imageOutput);
|
createWriter().write(original, imageOutput);
|
||||||
}
|
}
|
||||||
finally {
|
|
||||||
imageOutput.close();
|
|
||||||
}
|
|
||||||
|
|
||||||
Directory read = createReader().read(new ByteArrayImageInputStream(output.toByteArray()));
|
Directory read = createReader().read(new ByteArrayImageInputStream(output.toByteArray()));
|
||||||
|
|
||||||
@ -259,22 +257,23 @@ public class TIFFWriterTest extends MetadataWriterAbstractTest {
|
|||||||
@Test
|
@Test
|
||||||
public void testWriteASCIIArray() throws IOException {
|
public void testWriteASCIIArray() throws IOException {
|
||||||
ArrayList<Entry> entries = new ArrayList<>();
|
ArrayList<Entry> entries = new ArrayList<>();
|
||||||
String[] strings = new String []{"Twelve", "Monkeys", "ImageIO"};
|
String[] strings = new String [] {"Twelve", "Monkeys", "ImageIO"};
|
||||||
entries.add(new AbstractEntry(TIFF.TAG_SOFTWARE, strings) {});
|
entries.add(new TIFFEntry(TIFF.TAG_SOFTWARE, strings));
|
||||||
Directory directory = new AbstractDirectory(entries) {};
|
Directory directory = new IFD(entries);
|
||||||
|
|
||||||
ByteArrayOutputStream output = new FastByteArrayOutputStream(1024);
|
ByteArrayOutputStream output = new FastByteArrayOutputStream(1024);
|
||||||
ImageOutputStream imageStream = ImageIO.createImageOutputStream(output);
|
|
||||||
|
try (ImageOutputStream imageStream = ImageIO.createImageOutputStream(output)) {
|
||||||
imageStream.setByteOrder(ByteOrder.LITTLE_ENDIAN); // LE = Intel
|
imageStream.setByteOrder(ByteOrder.LITTLE_ENDIAN); // LE = Intel
|
||||||
createWriter().write(directory, imageStream);
|
createWriter().write(directory, imageStream);
|
||||||
imageStream.flush();
|
}
|
||||||
|
|
||||||
byte[] data = output.toByteArray();
|
byte[] data = output.toByteArray();
|
||||||
Directory read = new TIFFReader().read(new ByteArrayImageInputStream(data));
|
Directory read = new TIFFReader().read(new ByteArrayImageInputStream(data));
|
||||||
|
|
||||||
assertNotNull(read.getEntryById(TIFF.TAG_SOFTWARE));
|
assertNotNull(read.getEntryById(TIFF.TAG_SOFTWARE));
|
||||||
assertTrue("value not an string array", read.getEntryById(TIFF.TAG_SOFTWARE).getValue() instanceof String[]);
|
assertTrue("value not an string array", read.getEntryById(TIFF.TAG_SOFTWARE).getValue() instanceof String[]);
|
||||||
assertArrayEquals("", strings, (String[]) read.getEntryById(TIFF.TAG_SOFTWARE).getValue());
|
assertArrayEquals(strings, (String[]) read.getEntryById(TIFF.TAG_SOFTWARE).getValue());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
Loading…
x
Reference in New Issue
Block a user