Esempio n. 1
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;
  }
 /**
  * @see
  *     com.newland.message.packager.FieldPackager.iso.ISOFieldPackager#unpack(org.jpos.iso.ISOComponent,
  *     byte[], int)
  */
 public int unpack(IField<?> c, byte[] b, int offset) throws MessageException {
   try {
     int len = prefixer.decodeLength(b, offset);
     if (len == -1) {
       // The prefixer doesn't know how long the field is, so use
       // maxLength instead
       len = getLength();
     }
     int lenLen = prefixer.getPackedLength();
     byte[] unpacked = interpreter.uninterpret(b, offset + lenLen, len);
     // logger.debug("当前解析域值:["+CodecUtils.hexString(unpacked)+"]");
     ((IField<byte[]>) c).setValue(unpacked);
     return lenLen + interpreter.getPackedLength(len);
   } catch (Exception e) {
     throw new MessageException(makeExceptionMessage(c, "unpacking"), e);
   }
 }
  @Override
  public int unpack(ISOComponent c, byte[] b, int offset) throws ISOException {
    try {
      int len = prefixer.decodeLength(b, offset);
      if (len == -1) {
        // The prefixer doesn't know how long the field is, so use
        // maxLength instead
        len = getLength(b, offset);
      }

      int lenLen = prefixer.getPackedLength();
      String unpacked = interpreter.uninterpret(b, offset + lenLen, len);

      c.setValue(unpacked);
      return lenLen + interpreter.getPackedLength(len);
    } catch (Exception e) {
      throw new ISOException(makeExceptionMessage(c, "unpacking"), e);
    }
  }
  @Override
  public byte[] pack(ISOComponent c) throws ISOException {
    try {
      String data = (String) c.getValue();
      //		      System.out.println(data);

      String paddedData = padder.pad(data, getLength());
      byte[] rawData =
          new byte
              [prefixer.getPackedLength()
                  + interpreter.getPackedLength(paddedData.getBytes().length)];
      prefixer.encodeLength(paddedData.getBytes().length, rawData);
      interpreter.interpret(paddedData, rawData, prefixer.getPackedLength());
      return rawData;
    } catch (Exception e) {
      e.printStackTrace();
      throw new ISOException(makeExceptionMessage(c, "packing"), e);
    }
  }
 /** Convert the component into a byte[]. */
 public byte[] pack(IField<?> c) throws MessageException {
   try {
     byte[] data = (byte[]) c.getValue();
     int packedLength = prefixer.getPackedLength();
     if (packedLength == 0) {
       if (data.length != getLength()) {
         throw new MessageException(
             "Binary data length not the same as the packager length ("
                 + data.length
                 + "/"
                 + getLength()
                 + ")");
       }
     }
     byte[] ret = new byte[interpreter.getPackedLength(data.length) + packedLength];
     prefixer.encodeLength(data.length, ret);
     interpreter.interpret(data, ret, packedLength);
     return ret;
   } catch (Exception e) {
     throw new MessageException(makeExceptionMessage(c, "packing"), e);
   }
 }