/** * left pad with '0' * @param s - original string * @param len - desired len * @return zero padded string * @throws ISOException if string's length greater than len */ public static String zeropad(String s, int len) throws ISOException { return padleft(s, len, '0'); }
@Override public String makeCompliant(String noncompliant) { try { return (ISOUtil.padleft("", noncompliant.length(), '*')); } catch (ISOException e) { return "BAD DATA"; } }
/** * zeropads a long without throwing an ISOException (performs modulus operation) * * @param l the long * @param len the length * @return zeropadded value */ public static String zeropad(long l, int len) { try { return padleft (Long.toString ((long) (l % Math.pow (10, len))), len, '0'); } catch (ISOException ignored) { } return null; // should never happen }
/** * prepare long value used as amount for display * (implicit 2 decimals) * @param l value * @param len display len * @return formated field * @exception ISOException */ public static String formatAmount(long l, int len) throws ISOException { String buf = Long.toString(l); if (l < 100) buf = zeropad(buf, 3); StringBuilder s = new StringBuilder(padleft (buf, len-1, ' ') ); s.insert(len-3, '.'); return s.toString(); }
/** * format double value * @param d the amount * @param len the field len * @return a String of fieldLen characters (right justified) */ public static String formatDouble(double d, int len) { String prefix = Long.toString((long) d); String suffix = Integer.toString ( (int) (Math.round(d * 100f) % 100) ); try { if (len > 3) prefix = ISOUtil.padleft(prefix,len-3,' '); suffix = ISOUtil.zeropad(suffix, 2); } catch (ISOException e) { // should not happen } return prefix + "." + suffix; } /**
@Test(expected = NullPointerException.class) public void testPadleftThrowsNullPointerException() throws Throwable { ISOUtil.padleft(null, 0, '\u0002'); }
@Override public byte[] pack(Map<String, String> fields) throws ISOException { if (value== null) {// if the hardcoded value is in the constructor , use it. value = fields.get(getName()); } else { fields.put(getName(), value); } if (value==null){ throw new ISOException(String.format("Field [%s]: Unable to pack as field is not set",getName())); } if (value.length() <= size) { if (padLeft){ ISOUtil.padleft(getValue(), size,(char) padCharacter.byteValue() ); } else { ISOUtil.padright(getValue(), size,(char) padCharacter.byteValue() ); } } else { throw new ISOException(String.format("Field [%s]:Cannot pack as data has size %d and size needs to be %d",getName(), value.length(),size)); } byte[] packedbyte = new byte[interpretter.getPackedLength(size)]; interpretter.interpret(getValue(), packedbyte, 0); return packedbyte; }
@Test public void testPadleft() throws Throwable { String result = ISOUtil.padleft("testString", 11, '2'); assertEquals("result", "2testString", result); }
@Test public void testPadleft1() throws Throwable { String result = ISOUtil.padleft("2C", 2, ' '); assertEquals("result", "2C", result); }
try paddedKsn = ISOUtil.padleft( sn.getBaseKeyID() + sn.getDeviceID() + sn.getTransactionCounter(), 20, 'F'
} else { int tagNumber = (Integer) c.getKey(); String tag = ISOUtil.padleft(String.valueOf(tagNumber), this.tag.length(), '0'); ISOTaggedField isoTaggedField = new ISOTaggedField(tag, c); if (fld.length > tagNumber) {
/** * If a variable stream comes in with data that reaches end of stream and * does not contain a delimitter its valid. There is no point to have a * delimiter at the end of a field if its the last field. * * @throws ISOException */ @Test public void unpackTest04() throws ISOException { VariableFieldPackager p = new VariableFieldPackager("F1", 20, new Byte((byte) 0x1c), AsciiInterpreter.INSTANCE); FSDMsgX msg = new FSDMsgX("Test");; msg.add("F1", p); String inStream = ISOUtil.padleft("", 10, '1'); msg.unpack(inStream.getBytes()); assertEquals(inStream, msg.get("F1")); }
/** * Field size can be upto 20 followed by delimiter. The data available is 25 * wide and no delimiter. An exception should be thrown indicating a * delimiter wasnt found after the max of 20 allowed. * * @throws ISOException */ @Test public void unpackTest03() throws ISOException { thrown.expect(ISOException.class); thrown.expectMessage("Field [F1]: Delimiter 1c not present after max size 20"); VariableFieldPackager p = new VariableFieldPackager("F1", 20, new Byte((byte) 0x1c), AsciiInterpreter.INSTANCE); FSDMsgX msg = new FSDMsgX("Test");; msg.add("F1", p); String inStream = ISOUtil.padleft("", 25, '1') + (char) 0x1c; msg.unpack(inStream.getBytes()); }
@Test public void testPadleftThrowsISOException() throws Throwable { try { ISOUtil.padleft("testString", 0, '\u0002'); fail("Expected ISOException to be thrown"); } catch (ISOException ex) { assertEquals("ex.getMessage()", "invalid len 10/0", ex.getMessage()); assertNull("ex.nested", ex.nested); } }