Exemplo n.º 1
0
  /**
   * This should get optimized out on an A1 OBU build. This method is used by the tools in order to
   * build test envelopes used in test cases. It may also be used in the build process to combine a
   * .jad envelope .jar envelope within a single larger envelope
   *
   * @param tag The identifier 'tag' used to the envelope being built.
   * @param is The input stream which will become the envelope's payload
   * @param os The output stream where the envelope will be written. The contents of the output
   *     stream will contain payload size, tag, payload, payload CRC32.
   * @return <code>true</code> Envelope was constructed successfully, <code>
   * 		false</code> the envelope construction failed.
   */
  public static boolean buildEnvelope(int tag, InputStream is, OutputStream os) {
    boolean retval = false;

    // sanity check
    if ((null == is) || (null == os) || (tag < 0) || (tag > 250)) return false;

    byte buf[] = new byte[BLOCK_SIZE];
    int n = 0;
    CRC32 crc = new CRC32();

    try {
      int sz = is.available();
      ByteEncoder be = new ByteEncoder();
      be.writeInt(4, sz);
      be.writeInt(1, tag);
      os.write(be.getContent());

      while (n >= 0) {
        n = is.read(buf);
        if (n > 0) {
          crc.update(buf, 0, n);
          os.write(buf, 0, n);
        }
      }

      be = new ByteEncoder();
      be.writeLong(4, crc.getValue());
      os.write(be.getContent());
      retval = true;
      crc = null;
    } catch (Exception e) {
      // ^^FQG=Unable to build envelope
      AqLog.getInstance().error("^^FQG", e);
    }
    return retval;
  }
Exemplo n.º 2
0
  // TODO: ALLEN VALIDATE
  public boolean getData(OutputStream os) {
    int bytesRead = 0;
    int bytesRemain = 0;
    int bytesToRead = 0;
    byte[] buffer = new byte[BLOCK_SIZE];

    bytesRemain = size;
    // ^^FQC=Stage2 bytesRemain:
    AqLog.getInstance().debug("^^FQC " + bytesRemain);

    do {
      if (bytesRemain < BLOCK_SIZE) {
        bytesToRead = bytesRemain;
      } else {
        bytesToRead = BLOCK_SIZE;
      }

      try {
        bytesRead = is.read(buffer, 0, bytesToRead);
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }

      if (bytesRead > 0) {
        bytesRemain -= bytesRead;

        /*
         **  Perform the CRC
         */
        crc.update(buffer, 0, bytesRead);

        /*
         ** Write to the output file.
         */
        try {
          os.write(buffer, 0, bytesRead);
          os.flush();
        } catch (IOException ioe2) {
          //  The file may be corrupt.
          // ^^FQ6=File could not be created from download.  fileName:
          AqLog.getInstance().info("^^FQ6 ");
          return false;
        }
      }

      if ((lastBytesRemain - bytesRemain) >= 2048) {
        // ^^FQA=Stage 2 BytesRemain:
        AqLog.getInstance().debug("^^FQA " + bytesRemain);
        lastBytesRemain = bytesRemain;
      }

    } while ((bytesRemain > 0) && (bytesRead >= 0));

    // read the CRC from the stream
    boolean retval = true;
    if (getExpectedCrcValue(is) != crc.getValue()) {
      retval = false;
    }

    // ^^FQ1=Stage2: BytesRead,bytesRemain,expectedCrc,actualCrc,retval
    AqLog.getInstance()
        .info(
            "^^FQ1 "
                + bytesRead
                + ","
                + bytesRemain
                + ","
                + this.getEnvelopeCrc()
                + ","
                + crc.getValue()
                + ","
                + retval);
    return retval;
  }