📄 testwrite.java
字号:
poiFs.createDocument(ps.toInputStream(), STREAM_NAME); poiFs.writeFilesystem(out); out.close(); /* Read the POIFS: */ final PropertySet[] psa = new PropertySet[1]; final POIFSReader r = new POIFSReader(); r.registerListener(new POIFSReaderListener() { public void processPOIFSReaderEvent (final POIFSReaderEvent event) { try { psa[0] = PropertySetFactory.create(event.getStream()); } catch (Exception ex) { ex.printStackTrace(); throw new RuntimeException(ex.toString()); /* FIXME (2): Replace the previous line by the following * one once we no longer need JDK 1.3 compatibility. */ // throw new RuntimeException(ex); } } }, STREAM_NAME); r.read(new FileInputStream(filename)); Assert.assertNotNull(psa[0]); Section s = (Section) (psa[0].getSections().get(0)); assertEquals(s.getFormatID(), formatID); Object p = s.getProperty(2); Assert.assertEquals(SECTION1, p); s = (Section) (psa[0].getSections().get(1)); p = s.getProperty(2); Assert.assertEquals(SECTION2, p); } static class MyPOIFSReaderListener implements POIFSReaderListener { public void processPOIFSReaderEvent(final POIFSReaderEvent event) { try { PropertySetFactory.create(event.getStream()); } catch (Exception ex) { fail(org.apache.poi.hpsf.Util.toString(ex)); } } } private static final int CODEPAGE_DEFAULT = -1; private static final int CODEPAGE_1252 = 1252; private static final int CODEPAGE_UTF8 = Constants.CP_UTF8; private static final int CODEPAGE_UTF16 = Constants.CP_UTF16; /** * <p>Writes and reads back various variant types and checks whether the * stuff that has been read back equals the stuff that was written.</p> */ public void testVariantTypes() { Throwable t = null; final int codepage = CODEPAGE_DEFAULT; if (!hasProperDefaultCharset()) { System.err.println(IMPROPER_DEFAULT_CHARSET_MESSAGE + " This testcase is skipped."); return; } try { check(Variant.VT_EMPTY, null, codepage); check(Variant.VT_BOOL, new Boolean(true), codepage); check(Variant.VT_BOOL, new Boolean(false), codepage); check(Variant.VT_CF, new byte[]{0}, codepage); check(Variant.VT_CF, new byte[]{0, 1}, codepage); check(Variant.VT_CF, new byte[]{0, 1, 2}, codepage); check(Variant.VT_CF, new byte[]{0, 1, 2, 3}, codepage); check(Variant.VT_CF, new byte[]{0, 1, 2, 3, 4}, codepage); check(Variant.VT_CF, new byte[]{0, 1, 2, 3, 4, 5}, codepage); check(Variant.VT_CF, new byte[]{0, 1, 2, 3, 4, 5, 6}, codepage); check(Variant.VT_CF, new byte[]{0, 1, 2, 3, 4, 5, 6, 7}, codepage); check(Variant.VT_I4, new Integer(27), codepage); check(Variant.VT_I8, new Long(28), codepage); check(Variant.VT_R8, new Double(29.0), codepage); check(Variant.VT_I4, new Integer(-27), codepage); check(Variant.VT_I8, new Long(-28), codepage); check(Variant.VT_R8, new Double(-29.0), codepage); check(Variant.VT_FILETIME, new Date(), codepage); check(Variant.VT_I4, new Integer(Integer.MAX_VALUE), codepage); check(Variant.VT_I4, new Integer(Integer.MIN_VALUE), codepage); check(Variant.VT_I8, new Long(Long.MAX_VALUE), codepage); check(Variant.VT_I8, new Long(Long.MIN_VALUE), codepage); check(Variant.VT_R8, new Double(Double.MAX_VALUE), codepage); check(Variant.VT_R8, new Double(Double.MIN_VALUE), codepage); check(Variant.VT_LPSTR, "", codepage); check(Variant.VT_LPSTR, "\u00e4", codepage); check(Variant.VT_LPSTR, "\u00e4\u00f6", codepage); check(Variant.VT_LPSTR, "\u00e4\u00f6\u00fc", codepage); check(Variant.VT_LPSTR, "\u00e4\u00f6\u00fc\u00df", codepage); check(Variant.VT_LPSTR, "\u00e4\u00f6\u00fc\u00df\u00c4", codepage); check(Variant.VT_LPSTR, "\u00e4\u00f6\u00fc\u00df\u00c4\u00d6", codepage); check(Variant.VT_LPSTR, "\u00e4\u00f6\u00fc\u00df\u00c4\u00d6\u00dc", codepage); check(Variant.VT_LPWSTR, "", codepage); check(Variant.VT_LPWSTR, "\u00e4", codepage); check(Variant.VT_LPWSTR, "\u00e4\u00f6", codepage); check(Variant.VT_LPWSTR, "\u00e4\u00f6\u00fc", codepage); check(Variant.VT_LPWSTR, "\u00e4\u00f6\u00fc\u00df", codepage); check(Variant.VT_LPWSTR, "\u00e4\u00f6\u00fc\u00df\u00c4", codepage); check(Variant.VT_LPWSTR, "\u00e4\u00f6\u00fc\u00df\u00c4\u00d6", codepage); check(Variant.VT_LPWSTR, "\u00e4\u00f6\u00fc\u00df\u00c4\u00d6\u00dc", codepage); } catch (Exception ex) { t = ex; } catch (Error ex) { t = ex; } if (t != null) fail(org.apache.poi.hpsf.Util.toString(t)); } /** * <p>Writes and reads back strings using several different codepages and * checks whether the stuff that has been read back equals the stuff that * was written.</p> */ public void testCodepages() { Throwable thr = null; final int[] validCodepages = new int[] {CODEPAGE_DEFAULT, CODEPAGE_UTF8, CODEPAGE_UTF16, CODEPAGE_1252}; for (int i = 0; i < validCodepages.length; i++) { final int cp = validCodepages[i]; if (cp == -1 && !hasProperDefaultCharset()) { System.err.println(IMPROPER_DEFAULT_CHARSET_MESSAGE + " This testcase is skipped for the default codepage."); continue; } final long t = cp == CODEPAGE_UTF16 ? Variant.VT_LPWSTR : Variant.VT_LPSTR; try { check(t, "", cp); check(t, "\u00e4", cp); check(t, "\u00e4\u00f6", cp); check(t, "\u00e4\u00f6\u00fc", cp); check(t, "\u00e4\u00f6\u00fc\u00c4", cp); check(t, "\u00e4\u00f6\u00fc\u00c4\u00d6", cp); check(t, "\u00e4\u00f6\u00fc\u00c4\u00d6\u00dc", cp); check(t, "\u00e4\u00f6\u00fc\u00c4\u00d6\u00dc\u00df", cp); if (cp == Constants.CP_UTF16 || cp == Constants.CP_UTF8) check(t, "\u79D1\u5B78", cp); } catch (Exception ex) { thr = ex; } catch (Error ex) { thr = ex; } if (thr != null) fail(org.apache.poi.hpsf.Util.toString(thr) + " with codepage " + cp); } final int[] invalidCodepages = new int[] {0, 1, 2, 4711, 815}; for (int i = 0; i < invalidCodepages.length; i++) { int cp = invalidCodepages[i]; final long type = cp == CODEPAGE_UTF16 ? Variant.VT_LPWSTR : Variant.VT_LPSTR; try { check(type, "", cp); check(type, "\u00e4", cp); check(type, "\u00e4\u00f6", cp); check(type, "\u00e4\u00f6\u00fc", cp); check(type, "\u00e4\u00f6\u00fc\u00c4", cp); check(type, "\u00e4\u00f6\u00fc\u00c4\u00d6", cp); check(type, "\u00e4\u00f6\u00fc\u00c4\u00d6\u00dc", cp); check(type, "\u00e4\u00f6\u00fc\u00c4\u00d6\u00dc\u00df", cp); fail("UnsupportedEncodingException for codepage " + cp + " expected."); } catch (UnsupportedEncodingException ex) { /* This is the expected behaviour. */ } catch (Exception ex) { thr = ex; } catch (Error ex) { thr = ex; } if (thr != null) fail(org.apache.poi.hpsf.Util.toString(thr)); } } /** * <p>Tests whether writing 8-bit characters to a Unicode property * succeeds.</p> */ public void testUnicodeWrite8Bit() { final String TITLE = "This is a sample title"; final MutablePropertySet mps = new MutablePropertySet(); final MutableSection ms = (MutableSection) mps.getSections().get(0); ms.setFormatID(SectionIDMap.SUMMARY_INFORMATION_ID); final MutableProperty p = new MutableProperty(); p.setID(PropertyIDMap.PID_TITLE); p.setType(Variant.VT_LPSTR); p.setValue(TITLE); ms.setProperty(p); Throwable t = null; try { ByteArrayOutputStream out = new ByteArrayOutputStream(); mps.write(out); out.close(); byte[] bytes = out.toByteArray(); PropertySet psr = new PropertySet(bytes); assertTrue(psr.isSummaryInformation()); Section sr = (Section) psr.getSections().get(0); String title = (String) sr.getProperty(PropertyIDMap.PID_TITLE); assertEquals(TITLE, title); } catch (WritingNotSupportedException e) { t = e; } catch (IOException e) { t = e; } catch (NoPropertySetStreamException e) { t = e; } if (t != null) fail(t.getMessage()); } /** * <p>Writes a property and reads it back in.</p> * * @param variantType The property's variant type. * @param value The property's value. * @param codepage The codepage to use for writing and reading. * @throws UnsupportedVariantTypeException if the variant is not supported. * @throws IOException if an I/O exception occurs. * @throws ReadingNotSupportedException * @throws UnsupportedEncodingException */ private void check(final long variantType, final Object value, final int codepage) throws UnsupportedVariantTypeException, IOException, ReadingNotSupportedException, UnsupportedEncodingException { final ByteArrayOutputStream out = new ByteArrayOutputStream(); VariantSupport.write(out, variantType, value, codepage); out.close(); final byte[] b = out.toByteArray(); final Object objRead = VariantSupport.read(b, 0, b.length + LittleEndian.INT_SIZE, variantType, codepage); if (objRead instanceof byte[]) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -