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) {
|
||||
Object value = entry.getValue();
|
||||
|
||||
if (value instanceof String) {
|
||||
return ((String) value).getBytes(StandardCharsets.UTF_8).length + 1;
|
||||
return computeStringLength((String) value);
|
||||
}
|
||||
else if (value instanceof String[]) {
|
||||
int sum = 0;
|
||||
for (String string : (String[]) value) {
|
||||
sum += string.getBytes(StandardCharsets.UTF_8).length + 1;
|
||||
}
|
||||
return sum;
|
||||
return computeStringLength((String[]) value);
|
||||
}
|
||||
else {
|
||||
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 {
|
||||
if (value.getClass().isArray()) {
|
||||
switch (type) {
|
||||
@ -423,10 +430,7 @@ public final class TIFFWriter extends MetadataWriter {
|
||||
break;
|
||||
}
|
||||
case TIFF.TYPE_ASCII:
|
||||
String[] strings = (String[]) value;
|
||||
for (String string : strings) {
|
||||
writeString(stream, string);
|
||||
}
|
||||
writeStrings(stream, (String[]) value);
|
||||
break;
|
||||
default:
|
||||
throw new IllegalArgumentException("Unsupported TIFF type: " + type);
|
||||
@ -440,7 +444,7 @@ public final class TIFFWriter extends MetadataWriter {
|
||||
stream.writeByte(((Number) value).intValue());
|
||||
break;
|
||||
case TIFF.TYPE_ASCII:
|
||||
writeString(stream, (String) value);
|
||||
writeStrings(stream, (String) value);
|
||||
break;
|
||||
case TIFF.TYPE_SHORT:
|
||||
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(0);
|
||||
}
|
||||
}
|
||||
|
||||
private void writeValueAt(final long dataOffset, final Object value, final short type, final ImageOutputStream stream) throws IOException {
|
||||
writeOffset(stream, dataOffset);
|
||||
|
@ -47,10 +47,7 @@ import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
/**
|
||||
* TIFFWriterTest
|
||||
@ -86,11 +83,13 @@ public class TIFFWriterTest extends MetadataWriterAbstractTest {
|
||||
Directory directory = new AbstractDirectory(entries) {};
|
||||
|
||||
ByteArrayOutputStream output = new FastByteArrayOutputStream(1024);
|
||||
ImageOutputStream imageStream = ImageIO.createImageOutputStream(output);
|
||||
|
||||
try (ImageOutputStream imageStream = ImageIO.createImageOutputStream(output)) {
|
||||
createWriter().write(directory, imageStream);
|
||||
imageStream.flush();
|
||||
|
||||
assertEquals(output.size(), imageStream.getStreamPosition());
|
||||
}
|
||||
|
||||
byte[] data = output.toByteArray();
|
||||
|
||||
@ -131,14 +130,15 @@ public class TIFFWriterTest extends MetadataWriterAbstractTest {
|
||||
Directory directory = new AbstractDirectory(entries) {};
|
||||
|
||||
ByteArrayOutputStream output = new FastByteArrayOutputStream(1024);
|
||||
ImageOutputStream imageStream = ImageIO.createImageOutputStream(output);
|
||||
|
||||
try (ImageOutputStream imageStream = ImageIO.createImageOutputStream(output)) {
|
||||
imageStream.setByteOrder(ByteOrder.BIG_ENDIAN); // BE = Motorola
|
||||
|
||||
createWriter().write(directory, imageStream);
|
||||
imageStream.flush();
|
||||
|
||||
assertEquals(output.size(), imageStream.getStreamPosition());
|
||||
}
|
||||
|
||||
byte[] data = output.toByteArray();
|
||||
|
||||
@ -166,14 +166,15 @@ public class TIFFWriterTest extends MetadataWriterAbstractTest {
|
||||
Directory directory = new AbstractDirectory(entries) {};
|
||||
|
||||
ByteArrayOutputStream output = new FastByteArrayOutputStream(1024);
|
||||
ImageOutputStream imageStream = ImageIO.createImageOutputStream(output);
|
||||
|
||||
try (ImageOutputStream imageStream = ImageIO.createImageOutputStream(output)) {
|
||||
imageStream.setByteOrder(ByteOrder.LITTLE_ENDIAN); // LE = Intel
|
||||
|
||||
createWriter().write(directory, imageStream);
|
||||
imageStream.flush();
|
||||
|
||||
assertEquals(output.size(), imageStream.getStreamPosition());
|
||||
}
|
||||
|
||||
byte[] data = output.toByteArray();
|
||||
|
||||
@ -205,12 +206,13 @@ public class TIFFWriterTest extends MetadataWriterAbstractTest {
|
||||
Directory directory = new IFD(Collections.<Entry>singletonList(subIFD));
|
||||
|
||||
ByteArrayOutputStream output = new FastByteArrayOutputStream(1024);
|
||||
ImageOutputStream imageStream = ImageIO.createImageOutputStream(output);
|
||||
|
||||
try (ImageOutputStream imageStream = ImageIO.createImageOutputStream(output)) {
|
||||
createWriter().write(directory, imageStream);
|
||||
imageStream.flush();
|
||||
|
||||
assertEquals(output.size(), imageStream.getStreamPosition());
|
||||
}
|
||||
|
||||
Directory read = new TIFFReader().read(new ByteArrayImageInputStream(output.toByteArray()));
|
||||
|
||||
@ -224,14 +226,10 @@ public class TIFFWriterTest extends MetadataWriterAbstractTest {
|
||||
Directory original = createReader().read(getDataAsIIS());
|
||||
|
||||
ByteArrayOutputStream output = new FastByteArrayOutputStream(256);
|
||||
ImageOutputStream imageOutput = ImageIO.createImageOutputStream(output);
|
||||
|
||||
try {
|
||||
try (ImageOutputStream imageOutput = ImageIO.createImageOutputStream(output)) {
|
||||
createWriter().write(original, imageOutput);
|
||||
}
|
||||
finally {
|
||||
imageOutput.close();
|
||||
}
|
||||
|
||||
Directory read = createReader().read(new ByteArrayImageInputStream(output.toByteArray()));
|
||||
|
||||
@ -260,21 +258,22 @@ public class TIFFWriterTest extends MetadataWriterAbstractTest {
|
||||
public void testWriteASCIIArray() throws IOException {
|
||||
ArrayList<Entry> entries = new ArrayList<>();
|
||||
String[] strings = new String [] {"Twelve", "Monkeys", "ImageIO"};
|
||||
entries.add(new AbstractEntry(TIFF.TAG_SOFTWARE, strings) {});
|
||||
Directory directory = new AbstractDirectory(entries) {};
|
||||
entries.add(new TIFFEntry(TIFF.TAG_SOFTWARE, strings));
|
||||
Directory directory = new IFD(entries);
|
||||
|
||||
ByteArrayOutputStream output = new FastByteArrayOutputStream(1024);
|
||||
ImageOutputStream imageStream = ImageIO.createImageOutputStream(output);
|
||||
|
||||
try (ImageOutputStream imageStream = ImageIO.createImageOutputStream(output)) {
|
||||
imageStream.setByteOrder(ByteOrder.LITTLE_ENDIAN); // LE = Intel
|
||||
createWriter().write(directory, imageStream);
|
||||
imageStream.flush();
|
||||
}
|
||||
|
||||
byte[] data = output.toByteArray();
|
||||
Directory read = new TIFFReader().read(new ByteArrayImageInputStream(data));
|
||||
|
||||
assertNotNull(read.getEntryById(TIFF.TAG_SOFTWARE));
|
||||
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
|
||||
|
Loading…
x
Reference in New Issue
Block a user