This product contains the chunks_parse_cmds.tbl file from the vsdump program.
Copyright (C) 2006-2007 Valek Filippov (frob@df.ru)
+
+This product contains parts of the eID Applet project
+(http://eid-applet.googlecode.com). Copyright (c) 2009-2014
+FedICT (federal ICT department of Belgium), e-Contract.be BVBA (https://www.e-contract.be),
+Bart Hanssens from FedICT
\ No newline at end of file
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
-import java.io.UnsupportedEncodingException;
import java.io.Writer;
+import java.nio.charset.Charset;
import org.apache.poi.hwpf.model.StyleDescription;
import org.apache.poi.hwpf.model.StyleSheet;
HWPFDocument _doc;
@SuppressWarnings("unused")
- public Word2Forrest(HWPFDocument doc, OutputStream stream)
- throws IOException, UnsupportedEncodingException
+ public Word2Forrest(HWPFDocument doc, OutputStream stream) throws IOException
{
- OutputStreamWriter out = new OutputStreamWriter (stream, "UTF-8");
+ OutputStreamWriter out = new OutputStreamWriter (stream, Charset.forName("UTF-8"));
_out = out;
_doc = doc;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
+import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
private static final Set<String> EXPECTED_FAILURES = new HashSet<String>();
static {
// password protected files
- EXPECTED_FAILURES.add("poifs/protect.xlsx");
EXPECTED_FAILURES.add("spreadsheet/password.xls");
EXPECTED_FAILURES.add("spreadsheet/51832.xls");
EXPECTED_FAILURES.add("document/PasswordProtected.doc");
EXPECTED_FAILURES.add("slideshow/Password_Protected-56-hello.ppt");
EXPECTED_FAILURES.add("slideshow/Password_Protected-np-hello.ppt");
EXPECTED_FAILURES.add("slideshow/cryptoapi-proc2356.ppt");
- EXPECTED_FAILURES.add("document/bug53475-password-is-pass.docx");
- EXPECTED_FAILURES.add("document/bug53475-password-is-solrcell.docx");
+ //EXPECTED_FAILURES.add("document/bug53475-password-is-pass.docx");
+ //EXPECTED_FAILURES.add("document/bug53475-password-is-solrcell.docx");
EXPECTED_FAILURES.add("spreadsheet/xor-encryption-abc.xls");
EXPECTED_FAILURES.add("spreadsheet/35897-type4.xls");
+ //EXPECTED_FAILURES.add("poifs/protect.xlsx");
+ //EXPECTED_FAILURES.add("poifs/protected_sha512.xlsx");
+ //EXPECTED_FAILURES.add("poifs/extenxls_pwd123.xlsx");
+ //EXPECTED_FAILURES.add("poifs/protected_agile.docx");
// TODO: fails XMLExportTest, is this ok?
EXPECTED_FAILURES.add("spreadsheet/CustomXMLMapping-singleattributenamespace.xlsx");
// TODO: good to ignore?
EXPECTED_FAILURES.add("spreadsheet/sample-beta.xlsx");
EXPECTED_FAILURES.add("spreadsheet/49931.xls");
- EXPECTED_FAILURES.add("poifs/protected_sha512.xlsx");
- EXPECTED_FAILURES.add("poifs/extenxls_pwd123.xlsx");
EXPECTED_FAILURES.add("openxml4j/ContentTypeHasParameters.ooxml");
// This is actually a spreadsheet!
EXPECTED_FAILURES.add("hpsf/TestRobert_Flaherty.doc");
// some files that are broken, Excel 5.0/95, Word 95, ...
- EXPECTED_FAILURES.add("poifs/protected_agile.docx");
EXPECTED_FAILURES.add("spreadsheet/43493.xls");
EXPECTED_FAILURES.add("spreadsheet/46904.xls");
EXPECTED_FAILURES.add("document/56880.doc");
List<Object[]> files = new ArrayList<Object[]>();
for(String file : scanner.getIncludedFiles()) {
+ file = file.replace('\\', '/'); // ... failures/handlers lookup doesn't work on windows otherwise
files.add(new Object[] { file, HANDLERS.get(getExtension(file)) });
}
@Test
public void testAllFiles() throws Exception {
assertNotNull("Unknown file extension for file: " + file + ": " + getExtension(file), handler);
- InputStream stream = new FileInputStream(new File("test-data", file));
+ InputStream stream = new BufferedInputStream(new FileInputStream(new File("test-data", file)),100);
try {
handler.handleFile(stream);
import static org.junit.Assert.assertNotNull;
+import java.io.IOException;
+import java.io.InputStream;
+
import org.apache.poi.POIXMLDocument;
+import org.apache.poi.poifs.filesystem.POIFSFileSystem;
public final class POIXMLDocumentHandler {
protected void handlePOIXMLDocument(POIXMLDocument doc) throws Exception {
assertNotNull(doc.getProperties());
assertNotNull(doc.getRelations());
}
+
+ protected static boolean isEncrypted(InputStream stream) throws IOException {
+ if (POIFSFileSystem.hasPOIFSHeader(stream)) {
+ POIFSFileSystem poifs = new POIFSFileSystem(stream);
+ if (poifs.getRoot().hasEntry("EncryptedPackage")) {
+ return true;
+ }
+ throw new IOException("wrong file format or file extension for OO XML file");
+ }
+ return false;
+ }
}
public class XSLFFileHandler implements FileHandler {
@Override
public void handleFile(InputStream stream) throws Exception {
- XSLFSlideShow slide = new XSLFSlideShow(OPCPackage.open(stream));
+ // ignore password protected files
+ if (POIXMLDocumentHandler.isEncrypted(stream)) return;
+
+ XSLFSlideShow slide = new XSLFSlideShow(OPCPackage.open(stream));
assertNotNull(slide.getPresentation());
assertNotNull(slide.getSlideMasterReferences());
assertNotNull(slide.getSlideReferences());
public class XSSFFileHandler extends SpreadsheetHandler {
@Override
public void handleFile(InputStream stream) throws Exception {
+ // ignore password protected files
+ if (POIXMLDocumentHandler.isEncrypted(stream)) return;
+
XSSFWorkbook wb = new XSSFWorkbook(stream);
// use the combined handler for HSSF/XSSF
exporter.exportToXML(os, true);
}
}
-
+
// a test-case to test this locally without executing the full TestAllFiles
@Test
public void test() throws Exception {
public class XWPFFileHandler implements FileHandler {
@Override
public void handleFile(InputStream stream) throws Exception {
- XWPFDocument doc = new XWPFDocument(stream);
+ // ignore password protected files
+ if (POIXMLDocumentHandler.isEncrypted(stream)) return;
+
+ XWPFDocument doc = new XWPFDocument(stream);
new POIXMLDocumentHandler().handlePOIXMLDocument(doc);
}
}
public void readFully(byte[] buf, int off, int len) {
- checkRecordPosition(len);
- _dataInput.readFully(buf, off, len);
- _currentDataOffset+=len;
+ int origLen = len;
+ if (buf == null) {
+ throw new NullPointerException();
+ } else if (off < 0 || len < 0 || len > buf.length - off) {
+ throw new IndexOutOfBoundsException();
+ }
+
+ while (len > 0) {
+ int nextChunk = Math.min(available(),len);
+ if (nextChunk == 0) {
+ if (!hasNextRecord()) {
+ throw new RecordFormatException("Can't read the remaining "+len+" bytes of the requested "+origLen+" bytes. No further record exists.");
+ } else {
+ nextRecord();
+ nextChunk = Math.min(available(),len);
+ assert(nextChunk > 0);
+ }
+ }
+ checkRecordPosition(nextChunk);
+ _dataInput.readFully(buf, off, nextChunk);
+ _currentDataOffset+=nextChunk;
+ off += nextChunk;
+ len -= nextChunk;
+ }
}
public String readString() {
nextRecord();
// note - the compressed flag may change on the fly
byte compressFlag = readByte();
+ assert(compressFlag == 0 || compressFlag == 1);
isCompressedEncoding = (compressFlag == 0);
}
}
import java.awt.Dimension;
import java.io.ByteArrayInputStream;
-import java.io.UnsupportedEncodingException;
-import java.nio.charset.Charset;
import org.apache.poi.ddf.DefaultEscherRecordFactory;
import org.apache.poi.ddf.EscherBSERecord;
import org.apache.poi.ss.util.ImageUtils;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
+import org.apache.poi.util.StringUtil;
/**
* Represents a escher picture. Eg. A GIF, JPEG etc...
*/
public class HSSFPicture extends HSSFSimpleShape implements Picture {
- private static POILogger logger = POILogFactory.getLogger(HSSFPicture.class);
+ @SuppressWarnings("unused")
+ private static POILogger logger = POILogFactory.getLogger(HSSFPicture.class);
public static final int PICTURE_TYPE_EMF = HSSFWorkbook.PICTURE_TYPE_EMF; // Windows Enhanced Metafile
public static final int PICTURE_TYPE_WMF = HSSFWorkbook.PICTURE_TYPE_WMF; // Windows Metafile
EscherProperties.BLIP__BLIPFILENAME);
return (null == propFile)
? ""
- : new String(propFile.getComplexData(), Charset.forName("UTF-16LE")).trim();
+ : StringUtil.getFromUnicodeLE(propFile.getComplexData()).trim();
}
public void setFileName(String data){
- try {
- EscherComplexProperty prop = new EscherComplexProperty(EscherProperties.BLIP__BLIPFILENAME, true, data.getBytes("UTF-16LE"));
- setPropertyValue(prop);
- } catch (UnsupportedEncodingException e) {
- logger.log( POILogger.ERROR, "Unsupported encoding: UTF-16LE");
- }
+ // TODO: add trailing \u0000?
+ byte bytes[] = StringUtil.getToUnicodeLE(data);
+ EscherComplexProperty prop = new EscherComplexProperty(EscherProperties.BLIP__BLIPFILENAME, true, bytes);
+ setPropertyValue(prop);
}
@Override
import org.apache.poi.EncryptedDocumentException;\r
import org.apache.poi.util.LittleEndian;\r
import org.apache.poi.util.LittleEndianConsts;\r
+import org.apache.poi.util.StringUtil;\r
\r
/**\r
* Helper functions used for standard and agile encryption\r
MessageDigest hashAlg = getMessageDigest(hashAlgorithm);\r
\r
hashAlg.update(salt);\r
- byte[] hash = hashAlg.digest(getUtf16LeString(password));\r
+ byte[] hash = hashAlg.digest(StringUtil.getToUnicodeLE(password));\r
byte[] iterator = new byte[LittleEndianConsts.INT_SIZE];\r
\r
byte[] first = (iteratorFirst ? iterator : hash);\r
return result;\r
}\r
\r
- public static byte[] getUtf16LeString(String str) {\r
- return str.getBytes(Charset.forName("UTF-16LE"));\r
- }\r
- \r
public static MessageDigest getMessageDigest(HashAlgorithm hashAlgorithm) {\r
try {\r
if (hashAlgorithm.needsBouncyCastle) {\r
package org.apache.poi.poifs.crypt;\r
\r
import java.io.IOException;\r
-import java.io.UnsupportedEncodingException;\r
+import java.nio.charset.Charset;\r
\r
import org.apache.poi.EncryptedDocumentException;\r
import org.apache.poi.poifs.crypt.standard.EncryptionRecord;\r
import org.apache.poi.util.LittleEndianConsts;\r
import org.apache.poi.util.LittleEndianInput;\r
import org.apache.poi.util.LittleEndianOutput;\r
+import org.apache.poi.util.StringUtil;\r
\r
public class DataSpaceMapUtils {\r
public static void addDefaultDataSpace(DirectoryEntry dir) throws IOException {\r
\r
public static String readUnicodeLPP4(LittleEndianInput is) {\r
int length = is.readInt();\r
- byte data[] = new byte[length];\r
- is.readFully(data);\r
+ if (length%2 != 0) {\r
+ throw new EncryptedDocumentException(\r
+ "UNICODE-LP-P4 structure is a multiple of 4 bytes. "\r
+ + "If Padding is present, it MUST be exactly 2 bytes long");\r
+ }\r
+ \r
+ String result = StringUtil.readUnicodeLE(is, length/2);\r
if (length%4==2) {\r
// Padding (variable): A set of bytes that MUST be of the correct size such that the size of the \r
// UNICODE-LP-P4 structure is a multiple of 4 bytes. If Padding is present, it MUST be exactly \r
// 2 bytes long, and each byte MUST be 0x00. \r
is.readShort();\r
}\r
- try {\r
- return new String(data, 0, data.length, "UTF-16LE");\r
- } catch (UnsupportedEncodingException e) {\r
- throw new EncryptedDocumentException(e);\r
- }\r
+ \r
+ return result;\r
}\r
\r
- public static void writeUnicodeLPP4(LittleEndianOutput os, String str) {\r
- try {\r
- byte buf[] = str.getBytes("UTF-16LE");\r
- os.writeInt(buf.length);\r
- os.write(buf);\r
- if (buf.length%4==2) {\r
- os.writeShort(0);\r
- }\r
- } catch (UnsupportedEncodingException e) {\r
- throw new EncryptedDocumentException(e);\r
+ public static void writeUnicodeLPP4(LittleEndianOutput os, String string) {\r
+ byte buf[] = StringUtil.getToUnicodeLE(string);\r
+ os.writeInt(buf.length);\r
+ os.write(buf);\r
+ if (buf.length%4==2) {\r
+ os.writeShort(0);\r
}\r
}\r
\r
is.readByte();\r
}\r
}\r
- try {\r
- return new String(data, 0, data.length, "UTF-8");\r
- } catch (UnsupportedEncodingException e) {\r
- throw new EncryptedDocumentException(e);\r
- }\r
+\r
+ return new String(data, 0, data.length, Charset.forName("UTF-8"));\r
}\r
\r
public static void writeUtf8LPP4(LittleEndianOutput os, String str) {\r
os.writeInt(str == null ? 0 : 4);\r
os.writeInt(0);\r
} else {\r
- try {\r
- byte buf[] = str.getBytes("UTF-8");\r
- os.writeInt(buf.length);\r
- os.write(buf);\r
- int scratchBytes = buf.length%4;\r
- if (scratchBytes > 0) {\r
- for (int i=0; i<(4-scratchBytes); i++) {\r
- os.writeByte(0);\r
- }\r
+ byte buf[] = str.getBytes(Charset.forName("UTF-8"));\r
+ os.writeInt(buf.length);\r
+ os.write(buf);\r
+ int scratchBytes = buf.length%4;\r
+ if (scratchBytes > 0) {\r
+ for (int i=0; i<(4-scratchBytes); i++) {\r
+ os.writeByte(0);\r
}\r
- } catch (UnsupportedEncodingException e) {\r
- throw new EncryptedDocumentException(e);\r
}\r
} \r
}\r
import java.security.GeneralSecurityException;\r
import java.security.MessageDigest;\r
import java.util.Arrays;\r
+\r
import javax.crypto.Cipher;\r
import javax.crypto.SecretKey;\r
import javax.crypto.spec.SecretKeySpec;\r
+\r
import org.apache.poi.EncryptedDocumentException;\r
import org.apache.poi.poifs.crypt.*;\r
import org.apache.poi.poifs.filesystem.DirectoryNode;\r
import org.apache.poi.poifs.filesystem.DocumentInputStream;\r
import org.apache.poi.util.LittleEndian;\r
+import org.apache.poi.util.StringUtil;\r
\r
public class BinaryRC4Decryptor extends Decryptor {\r
private long _length = -1L;\r
password = password.substring(0, 255);\r
HashAlgorithm hashAlgo = ver.getHashAlgorithm();\r
MessageDigest hashAlg = CryptoFunctions.getMessageDigest(hashAlgo);\r
- byte hash[] = hashAlg.digest(CryptoFunctions.getUtf16LeString(password));\r
+ byte hash[] = hashAlg.digest(StringUtil.getToUnicodeLE(password));\r
byte salt[] = ver.getSalt();\r
hashAlg.reset();\r
for (int i = 0; i < 16; i++) {\r
import java.io.ByteArrayOutputStream;\r
import java.io.IOException;\r
import java.io.InputStream;\r
-import java.nio.charset.Charset;\r
import java.security.GeneralSecurityException;\r
import java.security.MessageDigest;\r
import java.util.Arrays;\r
import org.apache.poi.util.IOUtils;\r
import org.apache.poi.util.LittleEndian;\r
import org.apache.poi.util.LittleEndianInputStream;\r
+import org.apache.poi.util.StringUtil;\r
\r
public class CryptoAPIDecryptor extends Decryptor {\r
\r
HashAlgorithm hashAlgo = ver.getHashAlgorithm();\r
MessageDigest hashAlg = CryptoFunctions.getMessageDigest(hashAlgo);\r
hashAlg.update(ver.getSalt());\r
- byte hash[] = hashAlg.digest(CryptoFunctions.getUtf16LeString(password));\r
+ byte hash[] = hashAlg.digest(StringUtil.getToUnicodeLE(password));\r
SecretKey skey = new SecretKeySpec(hash, ver.getCipherAlgorithm().jceId);\r
return skey;\r
}\r
entry.flags = leis.readUByte();\r
boolean isStream = StreamDescriptorEntry.flagStream.isSet(entry.flags);\r
entry.reserved2 = leis.readInt();\r
- byte nameBuf[] = new byte[nameSize * 2];\r
- leis.read(nameBuf);\r
- entry.streamName = new String(nameBuf, Charset.forName("UTF-16LE"));\r
+ entry.streamName = StringUtil.readUnicodeLE(leis, nameSize);\r
leis.readShort();\r
assert(entry.streamName.length() == nameSize);\r
}\r
import java.io.ByteArrayOutputStream;\r
import java.io.IOException;\r
import java.io.OutputStream;\r
-import java.nio.charset.Charset;\r
import java.security.GeneralSecurityException;\r
import java.security.MessageDigest;\r
import java.security.SecureRandom;\r
import org.apache.poi.util.IOUtils;\r
import org.apache.poi.util.LittleEndian;\r
import org.apache.poi.util.LittleEndianByteArrayOutputStream;\r
+import org.apache.poi.util.StringUtil;\r
\r
public class CryptoAPIEncryptor extends Encryptor {\r
private final CryptoAPIEncryptionInfoBuilder builder;\r
bos.write(buf, 0, 1);\r
LittleEndian.putUInt(buf, 0, sde.reserved2);\r
bos.write(buf, 0, 4);\r
- byte nameBytes[] = sde.streamName.getBytes(Charset.forName("UTF-16LE"));\r
+ byte nameBytes[] = StringUtil.getToUnicodeLE(sde.streamName);\r
bos.write(nameBytes, 0, nameBytes.length);\r
LittleEndian.putShort(buf, 0, (short)0); // null-termination\r
bos.write(buf, 0, 2);\r
==================================================================== */\r
package org.apache.poi.poifs.crypt.standard;\r
\r
-import static org.apache.poi.poifs.crypt.CryptoFunctions.getUtf16LeString;\r
import static org.apache.poi.poifs.crypt.EncryptionInfo.flagAES;\r
import static org.apache.poi.poifs.crypt.EncryptionInfo.flagCryptoAPI;\r
\r
import org.apache.poi.util.LittleEndianConsts;\r
import org.apache.poi.util.LittleEndianInput;\r
import org.apache.poi.util.LittleEndianOutput;\r
+import org.apache.poi.util.StringUtil;\r
\r
public class StandardEncryptionHeader extends EncryptionHeader implements EncryptionRecord {\r
\r
bos.writeInt(0); // reserved2\r
String cspName = getCspName();\r
if (cspName == null) cspName = getCipherProvider().cipherProviderName;\r
- bos.write(getUtf16LeString(cspName));\r
+ bos.write(StringUtil.getToUnicodeLE(cspName));\r
bos.writeShort(0);\r
int headerSize = bos.getWriteIndex()-startIdx-LittleEndianConsts.INT_SIZE;\r
sizeOutput.writeInt(headerSize); \r
package org.apache.poi.util;
-import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
import java.text.FieldPosition;
import java.text.NumberFormat;
import java.util.Iterator;
/**
* Title: String Utility Description: Collection of string handling utilities<p/>
*
- * Note - none of the methods in this class deals with {@link org.apache.poi.hssf.record.ContinueRecord}s. For such
- * functionality, consider using {@link RecordInputStream
-} *
+ * Note - none of the methods in this class deals with {@link org.apache.poi.hssf.record.ContinueRecord}s.
+ * For such functionality, consider using {@link RecordInputStream}
+ *
*
*@author Andrew C. Oliver
*@author Sergei Kozello (sergeikozello at mail.ru)
*@author Toshiaki Kamoshida (kamoshida.toshiaki at future dot co dot jp)
*/
public class StringUtil {
- private static final String ENCODING_ISO_8859_1 = "ISO-8859-1";
+ private static final Charset ISO_8859_1 = Charset.forName("ISO-8859-1");
+ private static final Charset UTF16LE = Charset.forName("UTF-16LE");
private StringUtil() {
// no instances of this class
throw new IllegalArgumentException("Illegal length " + len);
}
- try {
- return new String(string, offset, len * 2, "UTF-16LE");
- } catch (UnsupportedEncodingException e) {
- throw new RuntimeException(e);
- }
+ return new String(string, offset, len * 2, UTF16LE);
}
/**
* @return the converted string, never <code>null</code>
*/
public static String getFromUnicodeLE(byte[] string) {
- if(string.length == 0) { return ""; }
- return getFromUnicodeLE(string, 0, string.length / 2);
+ if(string.length == 0) { return ""; }
+ return getFromUnicodeLE(string, 0, string.length / 2);
+ }
+
+ /**
+ * Convert String to 16-bit unicode characters in little endian format
+ *
+ * @param string the string
+ * @return the byte array of 16-bit unicode characters
+ */
+ public static byte[] getToUnicodeLE(String string) {
+ return string.getBytes(UTF16LE);
}
/**
final byte[] string,
final int offset,
final int len) {
- try {
- int len_to_use = Math.min(len, string.length - offset);
- return new String(string, offset, len_to_use, ENCODING_ISO_8859_1);
- } catch (UnsupportedEncodingException e) {
- throw new RuntimeException(e);
- }
+ int len_to_use = Math.min(len, string.length - offset);
+ return new String(string, offset, len_to_use, ISO_8859_1);
}
+
public static String readCompressedUnicode(LittleEndianInput in, int nChars) {
- char[] buf = new char[nChars];
- for (int i = 0; i < buf.length; i++) {
- buf[i] = (char) in.readUByte();
- }
- return new String(buf);
+ byte[] buf = new byte[nChars];
+ in.readFully(buf);
+ return new String(buf, ISO_8859_1);
}
+
/**
* InputStream <tt>in</tt> is expected to contain:
* <ol>
* when written
*/
public static void putCompressedUnicode(String input, byte[] output, int offset) {
- byte[] bytes;
- try {
- bytes = input.getBytes(ENCODING_ISO_8859_1);
- } catch (UnsupportedEncodingException e) {
- throw new RuntimeException(e);
- }
+ byte[] bytes = input.getBytes(ISO_8859_1);
System.arraycopy(bytes, 0, output, offset, bytes.length);
}
+
public static void putCompressedUnicode(String input, LittleEndianOutput out) {
- byte[] bytes;
- try {
- bytes = input.getBytes(ENCODING_ISO_8859_1);
- } catch (UnsupportedEncodingException e) {
- throw new RuntimeException(e);
- }
+ byte[] bytes = input.getBytes(ISO_8859_1);
out.write(bytes);
}
* @param offset the offset to start writing into the byte array
*/
public static void putUnicodeLE(String input, byte[] output, int offset) {
- byte[] bytes;
- try {
- bytes = input.getBytes("UTF-16LE");
- } catch (UnsupportedEncodingException e) {
- throw new RuntimeException(e);
- }
+ byte[] bytes = input.getBytes(UTF16LE);
System.arraycopy(bytes, 0, output, offset, bytes.length);
}
public static void putUnicodeLE(String input, LittleEndianOutput out) {
- byte[] bytes;
- try {
- bytes = input.getBytes("UTF-16LE");
- } catch (UnsupportedEncodingException e) {
- throw new RuntimeException(e);
- }
+ byte[] bytes = input.getBytes(UTF16LE);
out.write(bytes);
}
public static String readUnicodeLE(LittleEndianInput in, int nChars) {
- char[] buf = new char[nChars];
- for (int i = 0; i < buf.length; i++) {
- buf[i] = (char) in.readUShort();
- }
- return new String(buf);
+ byte[] bytes = new byte[nChars*2];
+ in.readFully(bytes);
+ return new String(bytes, UTF16LE);
}
/**
* @return the encoding we want to use, currently hardcoded to ISO-8859-1
*/
public static String getPreferredEncoding() {
- return ENCODING_ISO_8859_1;
+ return ISO_8859_1.name();
}
/**
* @return true if string needs Unicode to be represented.
*/
public static boolean isUnicodeString(final String value) {
- try {
- return !value.equals(new String(value.getBytes(ENCODING_ISO_8859_1),
- ENCODING_ISO_8859_1));
- } catch (UnsupportedEncodingException e) {
- return true;
- }
+ return !value.equals(new String(value.getBytes(ISO_8859_1), ISO_8859_1));
}
/**
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.ByteBuffer;
-import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
import java.util.regex.Pattern;
import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
int n = s.length();
if (n == 0) return s;
- ByteBuffer bb;
- try {
- bb = ByteBuffer.wrap(s.getBytes("UTF-8"));
- } catch (UnsupportedEncodingException e){
- // should not happen
- throw new RuntimeException(e);
- }
+ ByteBuffer bb = ByteBuffer.wrap(s.getBytes(Charset.forName("UTF-8")));
StringBuilder sb = new StringBuilder();
while (bb.hasRemaining()) {
int b = bb.get() & 0xff;
package org.apache.poi.xssf.usermodel;\r
\r
import java.io.IOException;\r
-import java.io.UnsupportedEncodingException;\r
+import java.nio.charset.Charset;\r
import java.util.Date;\r
\r
import junit.framework.TestCase;\r
verifyBug54084Unicode(wbStreamingWritten);\r
}\r
\r
- private void verifyBug54084Unicode(Workbook wb) throws UnsupportedEncodingException {\r
+ private void verifyBug54084Unicode(Workbook wb) {\r
// expected data is stored in UTF-8 in a text-file\r
- String testData = new String(HSSFTestDataSamples.getTestDataFileContent("54084 - Greek - beyond BMP.txt"), "UTF-8").trim();\r
+ byte data[] = HSSFTestDataSamples.getTestDataFileContent("54084 - Greek - beyond BMP.txt");\r
+ String testData = new String(data, Charset.forName("UTF-8")).trim();\r
\r
Sheet sheet = wb.getSheetAt(0);\r
Row row = sheet.getRow(0);\r
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
-import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
import org.apache.poi.util.IOUtils;
import org.apache.poi.util.LZWDecompresser;
compressedSize = LittleEndian.readInt(src);
decompressedSize = LittleEndian.readInt(src);
int compressionType = LittleEndian.readInt(src);
+ @SuppressWarnings("unused")
int dataCRC = LittleEndian.readInt(src);
// TODO - Handle CRC checking on the output side
@Override
protected int populateDictionary(byte[] dict) {
- try {
- // Copy in the RTF constants
- byte[] preload = LZW_RTF_PRELOAD.getBytes("US-ASCII");
- System.arraycopy(preload, 0, dict, 0, preload.length);
-
- // Start adding new codes after the constants
- return preload.length;
- } catch(UnsupportedEncodingException e) {
- throw new RuntimeException("Your JVM is broken as it doesn't support US ASCII");
- }
+ // Copy in the RTF constants
+ byte[] preload = LZW_RTF_PRELOAD.getBytes(Charset.forName("US-ASCII"));
+ System.arraycopy(preload, 0, dict, 0, preload.length);
+
+ // Start adding new codes after the constants
+ return preload.length;
}
}
package org.apache.poi.hmef.attribute;
-import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
import org.apache.poi.hmef.Attachment;
import org.apache.poi.hmef.HMEFMessage;
String tmpData = null;
if(type == Types.ASCII_STRING.getId()) {
- try {
- tmpData = new String(data, CODEPAGE);
- } catch(UnsupportedEncodingException e) {
- throw new RuntimeException("JVM Broken - core encoding " + CODEPAGE + " missing");
- }
+ tmpData = new String(data, Charset.forName(CODEPAGE));
} else if(type == Types.UNICODE_STRING.getId()) {
tmpData = StringUtil.getFromUnicodeLE(data);
} else {
package org.apache.poi.hslf.model;
-import org.apache.poi.ddf.*;
-import org.apache.poi.hslf.record.*;
-import org.apache.poi.hslf.exceptions.HSLFException;
-import org.apache.poi.util.LittleEndian;
-
import java.io.ByteArrayOutputStream;
-import java.io.UnsupportedEncodingException;
import java.util.Iterator;
+import org.apache.poi.ddf.EscherClientDataRecord;
+import org.apache.poi.ddf.EscherComplexProperty;
+import org.apache.poi.ddf.EscherContainerRecord;
+import org.apache.poi.ddf.EscherOptRecord;
+import org.apache.poi.ddf.EscherProperties;
+import org.apache.poi.ddf.EscherRecord;
+import org.apache.poi.ddf.EscherSpRecord;
+import org.apache.poi.hslf.exceptions.HSLFException;
+import org.apache.poi.hslf.record.Document;
+import org.apache.poi.hslf.record.ExControl;
+import org.apache.poi.hslf.record.ExObjList;
+import org.apache.poi.hslf.record.OEShapeAtom;
+import org.apache.poi.hslf.record.Record;
+import org.apache.poi.hslf.record.RecordTypes;
+import org.apache.poi.util.LittleEndian;
+import org.apache.poi.util.StringUtil;
+
/**
* Represents an ActiveX control in a PowerPoint document.
*
ExControl ctrl = getExControl();
ctrl.getExControlAtom().setSlideId(sheet._getSheetNumber());
- try {
- String name = ctrl.getProgId() + "-" + getControlIndex();
- byte[] data = (name + '\u0000').getBytes("UTF-16LE");
- EscherComplexProperty prop = new EscherComplexProperty(EscherProperties.GROUPSHAPE__SHAPENAME, false, data);
- EscherOptRecord opt = getEscherOptRecord();
- opt.addEscherProperty(prop);
- } catch (UnsupportedEncodingException e){
- throw new HSLFException(e);
- }
+ String name = ctrl.getProgId() + "-" + getControlIndex() + '\u0000';
+ byte[] data = StringUtil.getToUnicodeLE(name);
+ EscherComplexProperty prop = new EscherComplexProperty(EscherProperties.GROUPSHAPE__SHAPENAME, false, data);
+ EscherOptRecord opt = getEscherOptRecord();
+ opt.addEscherProperty(prop);
}
}
package org.apache.poi.hslf.model;
-import org.apache.poi.ddf.*;
-import org.apache.poi.hslf.usermodel.PictureData;
-import org.apache.poi.hslf.usermodel.SlideShow;
-import org.apache.poi.hslf.record.Document;
-import org.apache.poi.hslf.blip.Bitmap;
-import org.apache.poi.hslf.exceptions.HSLFException;
-import org.apache.poi.util.POILogger;
-
-import javax.imageio.ImageIO;
-import java.awt.image.BufferedImage;
-import java.awt.*;
+import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
+import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.IOException;
-import java.io.UnsupportedEncodingException;
import java.util.List;
+import javax.imageio.ImageIO;
+
+import org.apache.poi.ddf.EscherBSERecord;
+import org.apache.poi.ddf.EscherComplexProperty;
+import org.apache.poi.ddf.EscherContainerRecord;
+import org.apache.poi.ddf.EscherOptRecord;
+import org.apache.poi.ddf.EscherProperties;
+import org.apache.poi.ddf.EscherRecord;
+import org.apache.poi.ddf.EscherSimpleProperty;
+import org.apache.poi.ddf.EscherSpRecord;
+import org.apache.poi.hslf.blip.Bitmap;
+import org.apache.poi.hslf.record.Document;
+import org.apache.poi.hslf.usermodel.PictureData;
+import org.apache.poi.hslf.usermodel.SlideShow;
+import org.apache.poi.util.POILogger;
+import org.apache.poi.util.StringUtil;
+
/**
* Represents a picture in a PowerPoint document.
logger.log(POILogger.DEBUG, "EscherContainerRecord.BSTORE_CONTAINER was not found ");
return null;
}
- List lst = bstore.getChildRecords();
+ List<EscherRecord> lst = bstore.getChildRecords();
int idx = getPictureIndex();
if (idx == 0){
logger.log(POILogger.DEBUG, "picture index was not found, returning ");
public String getPictureName(){
EscherOptRecord opt = getEscherOptRecord();
EscherComplexProperty prop = (EscherComplexProperty)getEscherProperty(opt, EscherProperties.BLIP__BLIPFILENAME);
- String name = null;
- if(prop != null){
- try {
- name = new String(prop.getComplexData(), "UTF-16LE");
- int idx = name.indexOf('\u0000');
- return idx == -1 ? name : name.substring(0, idx);
- } catch (UnsupportedEncodingException e){
- throw new HSLFException(e);
- }
- }
- return name;
+ if (prop == null) return null;
+ String name = StringUtil.getFromUnicodeLE(prop.getComplexData());
+ return name.trim();
}
/**
*/
public void setPictureName(String name){
EscherOptRecord opt = getEscherOptRecord();
- try {
- byte[] data = (name + '\u0000').getBytes("UTF-16LE");
- EscherComplexProperty prop = new EscherComplexProperty(EscherProperties.BLIP__BLIPFILENAME, false, data);
- opt.addEscherProperty(prop);
- } catch (UnsupportedEncodingException e){
- throw new HSLFException(e);
- }
+ byte[] data = StringUtil.getToUnicodeLE(name + '\u0000');
+ EscherComplexProperty prop = new EscherComplexProperty(EscherProperties.BLIP__BLIPFILENAME, false, data);
+ opt.addEscherProperty(prop);
}
/**
package org.apache.poi.hslf.record;
-import org.apache.poi.util.LittleEndian;
import java.io.IOException;
import java.io.OutputStream;
-import java.io.UnsupportedEncodingException;
+
+import org.apache.poi.util.LittleEndian;
+import org.apache.poi.util.StringUtil;
/**
* This atom corresponds exactly to a Windows Logical Font (LOGFONT) structure.
* @return font name
*/
public String getFontName(){
- String name = null;
- try {
- int i = 0;
- while(i < 64){
- //loop until find null-terminated end of the font name
- if(_recdata[i] == 0 && _recdata[i + 1] == 0) {
- name = new String(_recdata, 0, i, "UTF-16LE");
- break;
- }
- i += 2;
+ int maxLen = Math.min(_recdata.length,64);
+ for(int i = 0; i < maxLen; i+=2){
+ //loop until find null-terminated end of the font name
+ if(_recdata[i] == 0 && _recdata[i + 1] == 0) {
+ return StringUtil.getFromUnicodeLE(_recdata, 0, i/2);
}
- } catch (UnsupportedEncodingException e){
- throw new RuntimeException(e.getMessage(), e);
}
- return name;
+ return null;
}
/**
*/
public void setFontName(String name){
// Add a null termination if required
- if(! name.endsWith("\000")) {
- name = name + "\000";
+ if(! name.endsWith("\u0000")) {
+ name += '\u0000';
}
// Ensure it's not now too long
}
// Everything's happy, so save the name
- try {
- byte[] bytes = name.getBytes("UTF-16LE");
- System.arraycopy(bytes, 0, _recdata, 0, bytes.length);
- } catch (UnsupportedEncodingException e){
- throw new RuntimeException(e.getMessage(), e);
- }
+ byte[] bytes = StringUtil.getToUnicodeLE(name);
+ System.arraycopy(bytes, 0, _recdata, 0, bytes.length);
}
public void setFontIndex(int idx){
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
-import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
import java.util.Calendar;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public void readValue(InputStream value) throws IOException {
// Stored in the file as us-ascii
- try {
- byte[] data = IOUtils.toByteArray(value);
- rawId = new String(data, "ASCII");
- } catch(UnsupportedEncodingException e) {
- throw new RuntimeException("Core encoding not found, JVM broken?", e);
- }
+ byte[] data = IOUtils.toByteArray(value);
+ rawId = new String(data, Charset.forName("ASCII"));
// Now process the date
String[] parts = rawId.split(";");
}
public void writeValue(OutputStream out) throws IOException {
- try {
- byte[] data = rawId.getBytes("ASCII");
- out.write(data);
- } catch(UnsupportedEncodingException e) {
- throw new RuntimeException("Core encoding not found, JVM broken?", e);
- }
+ byte[] data = rawId.getBytes(Charset.forName("ASCII"));
+ out.write(data);
}
/**
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
-import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
import org.apache.poi.hsmf.datatypes.Types.MAPIType;
import org.apache.poi.util.IOUtils;
}
private void storeString() {
if (type == Types.ASCII_STRING) {
- try {
- rawValue = value.getBytes(encoding7Bit);
- } catch (UnsupportedEncodingException e) {
- throw new RuntimeException("Encoding not found - " + encoding7Bit, e);
- }
+ rawValue = value.getBytes(Charset.forName(encoding7Bit));
} else if (type == Types.UNICODE_STRING) {
- rawValue = new byte[value.length()*2];
- StringUtil.putUnicodeLE(value, rawValue, 0);
+ rawValue = StringUtil.getToUnicodeLE(value);
} else {
throw new IllegalArgumentException("Invalid type " + type + " for String Chunk");
}
}
// Decode
- try {
- return new String(data, encoding);
- } catch (UnsupportedEncodingException e) {
- throw new RuntimeException("Encoding not found - " + encoding, e);
- }
+ return new String(data, Charset.forName(encoding));
}
}
==================================================================== */
package org.apache.poi.hwpf.model;
-import java.io.IOException;
-
import org.apache.poi.util.Internal;
+import org.apache.poi.util.StringUtil;
@Internal
public class SinglentonTextPiece extends TextPiece
{
- public SinglentonTextPiece( StringBuilder buffer ) throws IOException
+ public SinglentonTextPiece( StringBuilder buffer )
{
- super( 0, buffer.length(), buffer.toString().getBytes( "UTF-16LE" ),
- new PieceDescriptor( new byte[8], 0 ) );
+ super( 0, buffer.length(), StringUtil.getToUnicodeLE(buffer.toString()), new PieceDescriptor( new byte[8], 0 ) );
}
@Override
package org.apache.poi.hwpf.model;
-import java.io.UnsupportedEncodingException;
import java.util.Arrays;
import org.apache.poi.hwpf.usermodel.CharacterProperties;
import org.apache.poi.util.LittleEndian;
import org.apache.poi.util.POILogFactory;
import org.apache.poi.util.POILogger;
+import org.apache.poi.util.StringUtil;
/**
* Comment me
nameLength = std[nameStart];
}
- try
- {
- _name = new String(std, nameStart, nameLength * multiplier, "UTF-16LE");
- }
- catch (UnsupportedEncodingException ignore)
- {
- // ignore
- }
+ _name = StringUtil.getFromUnicodeLE(std, nameStart, (nameLength*multiplier)/2);
//length then null terminator.
int grupxStart = ((nameLength + 1) * multiplier) + nameStart;
package org.apache.poi.hwpf.model;
-import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
import org.apache.poi.util.Internal;
* Create the StringBuilder from the text and unicode flag
*/
private static StringBuilder buildInitSB(byte[] text, PieceDescriptor pd) {
- String str;
- try {
- if(pd.isUnicode()) {
- str = new String(text, "UTF-16LE");
- } else {
- str = new String(text, "Cp1252");
- }
- } catch(UnsupportedEncodingException e) {
- throw new RuntimeException("Your Java is broken! It doesn't know about basic, required character encodings!");
- }
- return new StringBuilder(str);
+ String str = new String(text, Charset.forName(pd.isUnicode() ? "UTF-16LE" : "Cp1252"));
+
+ return new StringBuilder(str);
}
/**
public byte[] getRawBytes()
{
- try {
- return ((CharSequence)_buf).toString().getBytes(_usesUnicode ?
- "UTF-16LE" : "Cp1252");
- } catch (UnsupportedEncodingException ignore) {
- throw new RuntimeException("Your Java is broken! It doesn't know about basic, required character encodings!");
- }
+ return ((CharSequence)_buf).toString().getBytes(
+ Charset.forName(_usesUnicode ? "UTF-16LE" : "Cp1252")
+ );
}
/**
*/
@Deprecated
public CharacterRun insertBefore(String text, CharacterProperties props)
- // throws UnsupportedEncodingException
{
initAll();
PAPX papx = _paragraphs.get(_parStart);
*/
@Deprecated
public CharacterRun insertAfter(String text, CharacterProperties props)
- // throws UnsupportedEncodingException
{
initAll();
PAPX papx = _paragraphs.get(_parEnd - 1);
*/
@Deprecated
public Paragraph insertBefore(ParagraphProperties props, int styleIndex)
- // throws UnsupportedEncodingException
{
return this.insertBefore(props, styleIndex, "\r");
}
*/
@Deprecated
protected Paragraph insertBefore(ParagraphProperties props, int styleIndex, String text)
- // throws UnsupportedEncodingException
{
initAll();
StyleSheet ss = _doc.getStyleSheet();
*/
@Deprecated
public Paragraph insertAfter(ParagraphProperties props, int styleIndex)
- // throws UnsupportedEncodingException
{
return this.insertAfter(props, styleIndex, "\r");
}
*/
@Deprecated
protected Paragraph insertAfter(ParagraphProperties props, int styleIndex, String text)
- // throws UnsupportedEncodingException
{
initAll();
StyleSheet ss = _doc.getStyleSheet();
import org.apache.poi.ddf.EscherOptRecord;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.HWPFTestDataSamples;
+import org.apache.poi.util.StringUtil;
/**
* Test cases for {@link OfficeDrawing} and {@link OfficeDrawingsImpl} classes.
EscherComplexProperty gtextUNICODE = (EscherComplexProperty) officeArtFOPT
.lookup( 0x00c0 );
- String text = new String( gtextUNICODE.getComplexData(), "UTF-16LE" );
+ String text = StringUtil.getFromUnicodeLE(gtextUNICODE.getComplexData());
assertEquals( "DRAFT CONTRACT\0", text );
}
}
package org.apache.poi.util;
-import java.io.UnsupportedEncodingException;
+import java.nio.charset.Charset;
import java.text.NumberFormat;
-import org.apache.poi.util.StringUtil.StringsIterator;
-
import junit.framework.TestCase;
+import org.apache.poi.util.StringUtil.StringsIterator;
+
/**
* Unit test for StringUtil
*
(byte) 'o', (byte) ' ', (byte) 'W', (byte) 'o',
(byte) 'r', (byte) 'l', (byte) 'd', (byte) 0xAE
};
- String input;
- try {
- input = new String( expected_output, StringUtil.getPreferredEncoding() );
- } catch (UnsupportedEncodingException e) {
- throw new RuntimeException(e);
- }
+ String input = new String( expected_output, Charset.forName(StringUtil.getPreferredEncoding()) );
StringUtil.putCompressedUnicode( input, output, 0 );
for ( int j = 0; j < expected_output.length; j++ )