Пример #1
1
  public void unpack(ISOComponent c, InputStream in) throws IOException, ISOException {

    if (!(c instanceof ISOField))
      throw new ISOException(c.getClass().getName() + " is not an ISOField");

    boolean endFound = false;
    if (in.markSupported()) {
      in.mark(getMaxPackedLength());
    }
    ByteBuffer buf = ByteBuffer.allocate(getMaxPackedLength());

    for (int i = 0; i < getMaxPackedLength() && in.available() > 0; i++) {
      byte dataByte = (byte) in.read();
      if (dataByte == terminator) {
        endFound = true;
        break;
      } else {
        buf.put(dataByte);
      }
    }
    if (endFound) {
      byte[] data = byteBufferToBytes(buf);
      c.setValue(data);
    } else {
      if (in.markSupported()) {
        in.reset();
      }
      throw new ISOException("Terminating Backslash does not exist");
    }
  }
Пример #2
0
  @Override
  public int unpack(ISOComponent m, byte[] b) throws ISOException {
    LogEvent evt = new LogEvent(this, "unpack");
    int consumed = 0;
    ISOComponent c;

    // Unpack the fields
    while (consumed < b.length) {
      // Determine current tag
      int i = consumed == 0 && fld[0] != null ? 0 : tagPrefixer.decodeLength(b, consumed);

      if (!(i < fld.length) || fld[i] == null)
        throw new ISOException("Unsupported sub-field " + i + " unpacking field " + m.getKey());

      c = fld[i].createComponent(i);
      consumed += fld[i].unpack(c, b, consumed);
      if (logger != null) {
        evt.addMessage(
            "<unpack fld=\"" + i + "\" packager=\"" + fld[i].getClass().getName() + "\">");
        evt.addMessage("  <value>" + c.getValue().toString() + "</value>");
        evt.addMessage("</unpack>");
      }
      m.set(c);
    }
    Logger.log(evt);
    return consumed;
  }
 @Override
 public int unpack(ISOComponent c, byte[] b, int offset) throws ISOException {
   byte[] tagBytes = new byte[getMaxPackedLength()];
   System.arraycopy(b, offset, tagBytes, 0, tagBytes.length);
   c.setValue(ISOUtil.byte2hex(tagBytes));
   return tagBytes.length;
 }
Пример #4
0
 public int unpack(ISOComponent m, byte[] b) throws ISOException {
   LogEvent evt = new LogEvent(this, "unpack");
   int consumed = 0;
   for (int i = 0; consumed < b.length; i++) {
     ISOComponent c = fld[i].createComponent(i);
     consumed += fld[i].unpack(c, b, consumed);
     if (logger != null) {
       evt.addMessage(
           "<unpack fld=\"" + i + "\" packager=\"" + fld[i].getClass().getName() + "\">");
       evt.addMessage("  <value>" + c.getValue().toString() + "</value>");
       evt.addMessage("</unpack>");
     }
     m.set(c);
   }
   Logger.log(evt);
   return consumed;
 }
Пример #5
0
  /**
   * @param c - a component
   * @return packed component
   * @throws org.jpos.iso.ISOException
   */
  public byte[] pack(ISOComponent c) throws ISOException {
    int len;
    byte[] s = c.getBytes();

    if ((len = s.length) > getLength()) {
      throw new ISOException(
          "Invalid length "
              + len
              + " packing IF_FSTBINARY field "
              + c.getKey()
              + " max length="
              + getLength());
    }
    byte[] b = new byte[s.length + 1];
    System.arraycopy(s, 0, b, 0, s.length);
    b[b.length - 2] = terminator;
    return b;
  }
Пример #6
0
 /**
  * @param c - the Component to unpack
  * @param b - binary image
  * @param offset - starting offset within the binary image
  * @return consumed bytes
  * @throws org.jpos.iso.ISOException
  */
 public int unpack(ISOComponent c, byte[] b, int offset) throws ISOException {
   if (!(c instanceof ISOField))
     throw new ISOException(c.getClass().getName() + " is not an ISOField");
   int length = -1;
   for (int i = 0; i < getMaxPackedLength(); i++) {
     byte dataByte = b[offset + i];
     if (dataByte == terminator) {
       length = i;
       break;
     }
   }
   if (length >= 0) {
     byte[] value = new byte[length];
     System.arraycopy(b, offset, value, 0, length);
     c.setValue(value);
     return length + 1;
   } else {
     throw new ISOException("Terminating Backslash does not exist");
   }
 }
Пример #7
0
 public ISOComponent validate(ISOComponent c) throws org.jpos.iso.ISOException {
   LogEvent evt = new LogEvent(this, "validate");
   try {
     Map tab = c.getChildren();
     for (int i = 0; i < fldVld.length; i++) {
       ISOField f = (ISOField) tab.get(i);
       if (f != null) c.set(fldVld[i].validate(f));
     }
     return c;
   } catch (ISOVException ex) {
     if (!ex.treated()) {
       c.set(ex.getErrComponent());
       ex.setTreated(true);
     }
     evt.addMessage(ex);
     throw ex;
   } finally {
     Logger.log(evt);
   }
 }
 @Override
 public byte[] pack(ISOComponent c) throws ISOException {
   byte[] tagBytes;
   String tag = c.getValue().toString();
   tagBytes = ISOUtil.hex2byte(tag);
   if (tagBytes.length != getMaxPackedLength()) {
     byte[] b = new byte[getMaxPackedLength()];
     System.arraycopy(tagBytes, 0, b, b.length - tagBytes.length, tagBytes.length);
     tagBytes = b;
   }
   return tagBytes;
 }
Пример #9
0
  @Override
  public byte[] pack(ISOComponent c) throws ISOException {
    LogEvent evt = new LogEvent(this, "pack");
    try {
      int len = 0;
      Map tab = c.getChildren();
      List<byte[]> l = new ArrayList();

      for (Map.Entry ent : (Set<Map.Entry>) tab.entrySet()) {
        Integer i = (Integer) ent.getKey();
        if (i < 0) continue;
        if (fld[i] == null)
          throw new ISOException("Unsupported sub-field " + i + " packing field " + c.getKey());
        if (ent.getValue() instanceof ISOComponent)
          try {
            ISOComponent f = (ISOComponent) ent.getValue();
            byte[] b = fld[i].pack(f);
            len += b.length;
            l.add(b);
          } catch (Exception e) {
            evt.addMessage("error packing subfield " + i);
            evt.addMessage(c);
            evt.addMessage(e);
            throw e;
          }
      }
      int k = 0;
      byte[] d = new byte[len];
      for (byte[] b : l) {
        System.arraycopy(b, 0, d, k, b.length);
        k += b.length;
      }
      if (logger != null) // save a few CPU cycle if no logger available
      evt.addMessage(ISOUtil.hexString(d));
      return d;
    } catch (Exception ex) {
      throw new ISOException(ex);
    }
  }
Пример #10
0
 public byte[] pack(ISOComponent c) throws ISOException {
   try {
     Map tab = c.getChildren();
     StringBuilder sb = new StringBuilder();
     for (int i = 0; i < fld.length; i++) {
       ISOField f = (ISOField) tab.get(i);
       if (f != null) {
         sb.append(new String(fld[i].pack(f)));
       }
     }
     return sb.toString().getBytes();
   } catch (Exception ex) {
     throw new ISOException(this.getRealm() + ": " + ex.getMessage(), ex);
   }
 }