org.jpedal.io.filter.ASCIIHex Maven / Gradle / Ivy
/*
* ===========================================
* Java Pdf Extraction Decoding Access Library
* ===========================================
*
* Project Info: http://www.idrsolutions.com
* Help section for developers at http://www.idrsolutions.com/support/
*
* (C) Copyright 1997-2017 IDRsolutions and Contributors.
*
* This file is part of JPedal/JPDF2HTML5
*
@LICENSE@
*
* ---------------
* ASCIIHex.java
* ---------------
*/
package org.jpedal.io.filter;
import java.io.*;
import java.util.Map;
import org.jpedal.objects.raw.PdfObject;
import org.jpedal.utils.LogWriter;
import org.jpedal.utils.repositories.FastByteArrayOutputStream;
/**
*
*/
public class ASCIIHex extends BaseFilter implements PdfFilter {
public ASCIIHex(final PdfObject decodeParms) {
super(decodeParms);
}
/**
* asciihexdecode using our own implementation
*/
@Override
public byte[] decode(final byte[] data) throws IOException {
String line;
StringBuilder value = new StringBuilder();
final StringBuilder valuesRead = new StringBuilder();
BufferedReader mappingStream = null;
ByteArrayInputStream bis = null;
// read in ASCII mode to handle line returns
try {
bis = new ByteArrayInputStream(data);
mappingStream = new BufferedReader(new InputStreamReader(bis));
// read values into lookup table
if (mappingStream != null) {
while (true) {
line = mappingStream.readLine();
if (line == null) {
break;
}
// append to data
valuesRead.append(line);
}
}
} catch (final Exception e) {
LogWriter.writeLog("Exception " + e + " reading ASCII stream ");
}
if (mappingStream != null) {
try {
mappingStream.close();
bis.close();
} catch (final IOException e) {
LogWriter.writeLog("Exception: " + e.getMessage());
}
}
final int data_size = valuesRead.length();
int i = 0, count = 0;
char current;
final FastByteArrayOutputStream bos = new FastByteArrayOutputStream(data.length);
//loop to read and process
while (true) {
current = valuesRead.charAt(i);
if ((current >= '0' && current <= '9')
|| (current >= 'a' && current <= 'f')
|| (current >= 'A' && current <= 'F')) {
value.append(current);
if (count == 1) {
bos.write(Integer.valueOf(value.toString(), 16));
count = 0;
value = new StringBuilder();
} else {
count++;
}
}
if (current == '>') {
break;
}
i++;
if (i == data_size) {
break;
}
}
// write any last char
if (count == 1) {
value.append('0');
bos.write(Integer.valueOf(value.toString(), 16));
}
return bos.toByteArray();
}
/**
* asciihexdecode using our own implementation
*/
@Override
public void decode(final BufferedInputStream bis, final BufferedOutputStream streamCache, final String cacheName, final Map cachedObjects) {
this.bis = bis;
this.streamCache = streamCache;
this.cachedObjects = cachedObjects;
try {
StringBuffer value = new StringBuffer();
char current;
// loop to read and process
int count = bis.available();
for (int i = 0; i < count; i++) {
current = (char) bis.read();
while (current == '\n') {
current = (char) bis.read();
}
if ((current >= '0' && current <= '9')
|| (current >= 'a' && current <= 'f')
|| (current >= 'A' && current <= 'F')) {
value.append(current);
if (count == 1) {
streamCache.write(Integer.valueOf(value.toString(), 16));
count = 0;
value = new StringBuffer();
} else {
count++;
}
}
if (current == '>') {
break;
}
}
// write any last char
if (count == 1) {
value.append('0');
streamCache.write(Integer.valueOf(value.toString(), 16));
}
} catch (final IOException e1) {
LogWriter.writeLog("IO exception in RunLength " + e1);
}
}
}