/** * parse message. If the stream ends before the message is completely read, then the method adds an EOF field. * * @param b message image * * @throws IOException * @throws JDOMException */ public void unpack (byte[] b) throws IOException, JDOMException { unpack (new ByteArrayInputStream (b)); }
public void unpack (InputStream in) throws IOException, ISOException { synchronized (this) { try { fsd.unpack(in); } catch (JDOMException e) { throw new ISOException (e); } } }
public int unpack(byte[] b) throws ISOException { try { fsd.unpack (b); return b.length; } catch (Exception e) { throw new ISOException (e); } } public void unpack (InputStream in) throws IOException, ISOException {
/** * parse message. If the stream ends before the message is completely read, then the method adds an EOF field. * * @param is input stream * * @throws IOException * @throws JDOMException */ public void unpack (InputStream is) throws IOException, JDOMException { try { if (is.markSupported()) is.mark(READ_BUFFER); unpack (new InputStreamReader(is, charset), getSchema (baseSchema)); if (is.markSupported()) { is.reset(); is.skip (readCount); readCount = 0; } } catch (EOFException e) { if (!fields.isEmpty()) fields.put ("EOF", "true"); // some fields were read, but unexpected EOF found else // nothing new since last msg, fields were read; no more msgs from this stream throw e; // just rethrow the exception } } /**
public void testUnpack() throws Exception { ByteArrayInputStream is = new ByteArrayInputStream("11ABCDEFGHIJKLMNOPQRST".getBytes()); omsg.unpack(is); assertEquals("11", omsg.get("length")); assertEquals("ABCDEFGHIJKLMNOPQRST", omsg.get("rest")); } }
@Test public void testUnpackThrowsNullPointerException1() throws Throwable { FSDMsg fSDMsg = new FSDMsg("testFSDMsgBasePath"); InputStream is = new ByteArrayInputStream(new byte[0]); InputStreamReader r = new InputStreamReader(is); try { fSDMsg.unpack(r, null); fail("Expected NullPointerException to be thrown"); } catch (NullPointerException ex) { assertNull("ex.getMessage()", ex.getMessage()); assertEquals("fSDMsg.fields.size()", 0, fSDMsg.fields.size()); assertEquals("(ByteArrayInputStream) is.available()", 0, is.available()); } }
@Test public void testUnpack() throws Throwable { FSDMsg fSDMsg = new FSDMsg("testFSDMsgBasePath"); InputStream is = new ByteArrayInputStream(new byte[1]); InputStreamReader r = new InputStreamReader(is); Element schema = new Element("testFSDMsgName", "testFSDMsgUri"); fSDMsg.unpack(r, schema); assertEquals("fSDMsg.fields.size()", 0, fSDMsg.fields.size()); assertEquals("(ByteArrayInputStream) is.available()", 1, is.available()); assertEquals("schema.getName()", "testFSDMsgName", schema.getName()); }
@Test public void testUnpackThrowsNullPointerException2() throws Throwable { FSDMsg fSDMsg = new FSDMsg(null, "testFSDMsgBaseSchema"); byte[] bytes = new byte[3]; InputStream is = new ByteArrayInputStream(bytes); try { fSDMsg.unpack(is); fail("Expected NullPointerException to be thrown"); } catch (NullPointerException ex) { assertNull("ex.getMessage()", ex.getMessage()); assertEquals("fSDMsg.fields.size()", 0, fSDMsg.fields.size()); assertEquals("fSDMsg.baseSchema", "testFSDMsgBaseSchema", fSDMsg.baseSchema); assertEquals("(ByteArrayInputStream) is.available()", 3, is.available()); } }
@Test public void testUnpackThrowsMalformedURLException1() throws Throwable { FSDMsg fSDMsg = new FSDMsg("testFSDMsgBasePath", "testFSDMsgBaseSchema"); byte[] bytes = new byte[3]; InputStream is = new ByteArrayInputStream(bytes); try { fSDMsg.unpack(is); fail("Expected MalformedURLException to be thrown"); } catch (MalformedURLException ex) { assertEquals("ex.getClass()", MalformedURLException.class, ex.getClass()); assertEquals("fSDMsg.fields.size()", 0, fSDMsg.fields.size()); assertEquals("fSDMsg.baseSchema", "testFSDMsgBaseSchema", fSDMsg.baseSchema); assertEquals("(ByteArrayInputStream) is.available()", 3, is.available()); } }
public void testSeparator() throws Exception { String value = "1"; String field = "testafs"; imsg.set(field, value); assertEquals("Separator", "1'FS'", imsg.pack().replace(FS, "'FS'")); omsg.unpack(imsg.pack().getBytes()); assertEquals(value, omsg.get(field)); } }
public void testLeadingBlanks() throws Exception { String value = " 123"; String field = "testafs"; imsg.set(field, value); assertEquals("Leading blanks", ISOUtil.hex2byte("2020203132331C"), imsg .pack().getBytes()); omsg.unpack(imsg.pack().getBytes()); assertEquals(value, omsg.get(field)); }
public void testTraillingBlanksDroppedwithFS() throws Exception { String value = "123 "; String field = "testafs"; imsg.set(field, value); assertEquals("3132331C", ISOUtil.hexString(imsg .pack().getBytes())); omsg.unpack(imsg.pack().getBytes()); assertEquals("123", omsg.get(field)); }
public void testMixedBlanksLeadingArePreserved() throws Exception { String value = " 123 "; String field = "testafs"; imsg.set(field, value); assertEquals("Mixed blanks", ISOUtil.hex2byte("20203132331C"), imsg .pack().getBytes()); omsg.unpack(imsg.pack().getBytes()); assertEquals(" 123", omsg.get(field)); }
@Test public void testUnpackThrowsMalformedURLException() throws Throwable { FSDMsg fSDMsg = new FSDMsg("testFSDMsgBasePath"); byte[] b = new byte[1]; try { fSDMsg.unpack(b); fail("Expected MalformedURLException to be thrown"); } catch (MalformedURLException ex) { assertEquals("ex.getClass()", MalformedURLException.class, ex.getClass()); assertEquals("fSDMsg.fields.size()", 0, fSDMsg.fields.size()); assertEquals("fSDMsg.baseSchema", "base", fSDMsg.baseSchema); assertEquals("b.length", 1, b.length); } }
@Test public void testUnpackThrowsNullPointerException() throws Throwable { FSDMsg fSDMsg = new FSDMsg(null, "testFSDMsgBaseSchema"); byte[] b = new byte[1]; try { fSDMsg.unpack(b); fail("Expected NullPointerException to be thrown"); } catch (NullPointerException ex) { assertNull("ex.getMessage()", ex.getMessage()); assertEquals("fSDMsg.fields.size()", 0, fSDMsg.fields.size()); assertEquals("fSDMsg.baseSchema", "testFSDMsgBaseSchema", fSDMsg.baseSchema); assertEquals("b.length", 1, b.length); } }
public void testSeparator() throws Exception { imsg.set("testafs", "x"); imsg.set("b", "y"); assertEquals("properties", "x'FS'y", imsg.pack().replace(FS, "'FS'")); omsg.unpack(imsg.pack().getBytes()); assertEquals("x", omsg.get("testafs")); assertEquals("y", omsg.get("b")); } }
public void testFinalField() throws Exception { String value1 = " 123 "; String field1 = "testafs"; String value2 = "ABC"; String field2 = "finalfield"; imsg.set(field1, value1); imsg.set(field2, value2); assertEquals("20203132331C414243",ISOUtil.hexString(imsg.pack().getBytes())); omsg.unpack(imsg.pack().getBytes()); assertEquals(" 123", omsg.get(field1)); assertEquals(value2, omsg.get(field2)); }
@Test public void testPackToBytesCharset() throws Throwable { Element schema = createSchema(); appendField(schema, "name", "A", "DS", 32); FSDMsg fSDMsg = new FSDMsg(SCHEMA_PREFIX); //bytes represents "Zażółć żółtą gęś" it's a sample in polish byte[] expected = ISOUtil.hex2byte("5A61BFF3B3E620BFF3B374B12067EAB6"); Charset charset = Charset.forName("ISO-8859-2"); fSDMsg.setCharset(charset); fSDMsg.set("name", new String(expected,charset)); byte[] b = fSDMsg.packToBytes(); // System.out.println(new String(b, charset)); assertArrayEquals("FSDMsg.packToBytes() don't properly handle character encodings", expected, b); fSDMsg.unpack(b); // System.out.println(fSDMsg.get("name")); b = fSDMsg.get("name").getBytes(charset); assertArrayEquals("FSDMsg.unpack(b) don't properly handle character encodings", expected, b); } }
public void testFSDMsgDefaultKey () throws Exception { FSDMsg m0 = new FSDMsg(SCHEMA_DIR_URL + "fsd-"); FSDMsg m1 = new FSDMsg(SCHEMA_DIR_URL + "fsd-"); FSDMsg u0 = new FSDMsg(SCHEMA_DIR_URL + "fsd-"); FSDMsg u1 = new FSDMsg(SCHEMA_DIR_URL + "fsd-"); m0.set("message-id","03"); m0.set("x","X"); m0.set("y","WHYWHY03"); assertEquals ("Default defined - not used - pack", "000X0300WHYWHY03", m0.pack() ); m1.set("message-id","99"); m1.set("x","X"); m1.set("z","DEFAULT"); assertEquals ("Default defined - used - pack", "000X99DEFAULT ", m1.pack() ); u0.unpack(m0.packToBytes()); assertEquals ("Default defined - not used - unpack", "00WHYWHY03", u0.get("y") ); u1.unpack(m1.packToBytes()); assertEquals ("Default defined - used - unpack", "DEFAULT ", u1.get("z") ); }