Skip to content

Commit

Permalink
[refactoring] remove few more code duplication for I/O operations
Browse files Browse the repository at this point in the history
  • Loading branch information
asolntsev committed Dec 30, 2023
1 parent 3289eed commit bf879ca
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 55 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,52 +8,44 @@
/**
* This class wraps an input stream and detects if it contains certain content using "magic numbers".
*
* http://en.wikipedia.org/wiki/Magic_number_(programming)
*
* <a href="http://en.wikipedia.org/wiki/Magic_number_(programming)">...</a>
* <p>
* currently only pdf detection is implemented
*
* @author mwyraz
*/
public class ContentTypeDetectingInputStreamWrapper extends BufferedInputStream
{
public class ContentTypeDetectingInputStreamWrapper extends BufferedInputStream {
protected static final int MAX_MAGIC_BYTES=4;
protected final byte[] MAGIC_BYTES;

public ContentTypeDetectingInputStreamWrapper(InputStream source) throws IOException
{
public ContentTypeDetectingInputStreamWrapper(InputStream source) throws IOException {
super(source);
byte[] MAGIC_BYTES=new byte[MAX_MAGIC_BYTES];
mark(MAX_MAGIC_BYTES);

try
{
try {
int bytesRead=read(MAGIC_BYTES);
if (bytesRead<MAX_MAGIC_BYTES) // Not enough data in stream
{
if (bytesRead<MAX_MAGIC_BYTES) { // Not enough data in stream
if (bytesRead<=0) MAGIC_BYTES=new byte[0]; // no data
else MAGIC_BYTES=Arrays.copyOf(MAGIC_BYTES, bytesRead); // fewer bytes
}
this.MAGIC_BYTES=MAGIC_BYTES;
}
finally
{
finally {
reset();
}
}

protected boolean streamStartsWithMagicBytes(byte[] bytes)
{
protected boolean streamStartsWithMagicBytes(byte[] bytes) {
if (MAGIC_BYTES.length<bytes.length) return false;
for (int i=0;i<bytes.length;i++)
{
for (int i=0;i<bytes.length;i++) {
if (MAGIC_BYTES[i]!=bytes[i]) return false;
}
return true;
}

protected final static byte[] MAGIC_BYTES_PDF="%PDF".getBytes();
public boolean isPdf()
{
public boolean isPdf() {
return streamStartsWithMagicBytes(MAGIC_BYTES_PDF);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.nio.file.Path;
import java.util.logging.Level;

import static java.nio.file.Files.newInputStream;
Expand Down Expand Up @@ -125,6 +126,14 @@ public static byte[] readBytes(String uri) {
}
}

@Nonnull
@CheckReturnValue
public static byte[] readBytes(Path file) throws IOException {
try (InputStream is = newInputStream(file)) {
return readBytes(is);
}
}

@Nonnull
@CheckReturnValue
public static byte[] readBytes(InputStream is) throws IOException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,12 @@
import org.xhtmlrenderer.extend.UserAgentCallback;
import org.xhtmlrenderer.layout.SharedContext;
import org.xhtmlrenderer.render.FSFont;
import org.xhtmlrenderer.util.IOUtil;
import org.xhtmlrenderer.util.XRLog;
import org.xhtmlrenderer.util.XRRuntimeException;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
Expand All @@ -48,7 +47,6 @@
import java.util.Map;
import java.util.Set;

import static java.nio.file.Files.newInputStream;
import static java.util.Collections.singletonList;
import static java.util.Comparator.comparingInt;
import static java.util.Objects.requireNonNull;
Expand Down Expand Up @@ -313,21 +311,7 @@ private void addFontFaceFont(
}

private byte[] readFile(String path) throws IOException {
File f = new File(path);
if (f.exists()) {
ByteArrayOutputStream result = new ByteArrayOutputStream((int)f.length());

try (InputStream is = newInputStream(Paths.get(path))) {
byte[] buf = new byte[10240];
int i;
while ( (i = is.read(buf)) != -1) {
result.write(buf, 0, i);
}
return result.toByteArray();
}
} else {
throw new IOException("File " + path + " does not exist or is not accessible");
}
return IOUtil.readBytes(Paths.get(path));
}

private FontFamily getFontFamily(String fontFamilyName) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,13 @@
import org.xhtmlrenderer.layout.SharedContext;
import org.xhtmlrenderer.render.FSFont;
import org.xhtmlrenderer.util.FontUtil;
import org.xhtmlrenderer.util.IOUtil;
import org.xhtmlrenderer.util.SupportedEmbeddedFontTypes;
import org.xhtmlrenderer.util.XRLog;
import org.xhtmlrenderer.util.XRRuntimeException;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -326,22 +324,7 @@ private void addFontFaceFont(
}

private byte[] readFile(String path) throws IOException {
File f = new File(path);
if (f.exists()) {
ByteArrayOutputStream result = new ByteArrayOutputStream((int)f.length());

try (InputStream is = Files.newInputStream(Paths.get(path))) {
byte[] buf = new byte[10240];
int i;
while ( (i = is.read(buf)) != -1) {
result.write(buf, 0, i);
}

return result.toByteArray();
}
} else {
throw new IOException("File " + path + " does not exist or is not accessible");
}
return IOUtil.readBytes(Paths.get(path));
}

private FontFamily getFontFamily(String fontFamilyName) {
Expand Down

0 comments on commit bf879ca

Please sign in to comment.