private Class<?> getClassFromStream(
     final InputStream stream, final String classname, final File container)
     throws IOException, SecurityException {
   final ByteArrayOutputStream baos = new ByteArrayOutputStream();
   int bytesRead = -1;
   final byte[] buffer = new byte[8192];
   while ((bytesRead = stream.read(buffer, 0, 8192)) != -1) {
     baos.write(buffer, 0, bytesRead);
   }
   final byte[] classData = baos.toByteArray();
   return this.defineClassFromData(container, classData, classname);
 }
Ejemplo n.º 2
0
  /** Write certficate bytes into a PEM encoded string */
  public static String writePEM(byte[] bytes, String hdr, String ftr) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    Base64OutputStream b64os = new Base64OutputStream(bos);
    b64os.write(bytes);
    b64os.flush();
    b64os.close();

    ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
    InputStreamReader irr = new InputStreamReader(bis);
    BufferedReader r = new BufferedReader(irr);

    StringBuffer buff = new StringBuffer();
    String line;
    buff.append(hdr);

    while ((line = r.readLine()) != null) {
      buff.append(line + "\n");
    }
    buff.append(ftr);
    return buff.toString();
  }
Ejemplo n.º 3
0
 /**
  * Generates a Payment message based on the information in the PaymentRequest. Provide
  * transactions built by the wallet. If the PaymentRequest did not specify a payment_url, returns
  * null.
  *
  * @param txns list of transactions to be included with the Payment message.
  * @param refundAddr will be used by the merchant to send money back if there was a problem.
  * @param memo is a message to include in the payment message sent to the merchant.
  */
 public @Nullable Protos.Payment getPayment(
     List<Transaction> txns, @Nullable Address refundAddr, @Nullable String memo)
     throws IOException {
   if (!paymentDetails.hasPaymentUrl()) return null;
   Protos.Payment.Builder payment = Protos.Payment.newBuilder();
   if (paymentDetails.hasMerchantData()) payment.setMerchantData(paymentDetails.getMerchantData());
   if (refundAddr != null) {
     Protos.Output.Builder refundOutput = Protos.Output.newBuilder();
     refundOutput.setAmount(totalValue.longValue());
     refundOutput.setScript(
         ByteString.copyFrom(ScriptBuilder.createOutputScript(refundAddr).getProgram()));
     payment.addRefundTo(refundOutput);
   }
   if (memo != null) {
     payment.setMemo(memo);
   }
   for (Transaction txn : txns) {
     txn.verify();
     ByteArrayOutputStream o = new ByteArrayOutputStream();
     txn.bitcoinSerialize(o);
     payment.addTransactions(ByteString.copyFrom(o.toByteArray()));
   }
   return payment.build();
 }
Ejemplo n.º 4
0
  /**
   * Returns an ASN.1 SEQUENCE from a stream, which might be a BER-encoded binary block or a
   * PEM-style BASE64-encoded ASCII data. In the latter case, it's de-BASE64'ed before return.
   *
   * <p>After the reading, the input stream pointer is after the BER block, or after the newline
   * character after the -----END SOMETHING----- line.
   *
   * @param is the InputStream
   * @returns byte block or null if end of stream
   * @throws IOException If any parsing error
   */
  private static byte[] readOneBlock(InputStream is) throws IOException {

    // The first character of a BLOCK.
    int c = is.read();
    if (c == -1) {
      return null;
    }
    if (c == DerValue.tag_Sequence) {
      ByteArrayOutputStream bout = new ByteArrayOutputStream(2048);
      bout.write(c);
      readBERInternal(is, bout, c);
      return bout.toByteArray();
    } else {
      // Read BASE64 encoded data, might skip info at the beginning
      char[] data = new char[2048];
      int pos = 0;

      // Step 1: Read until header is found
      int hyphen = (c == '-') ? 1 : 0; // count of consequent hyphens
      int last = (c == '-') ? -1 : c; // the char before hyphen
      while (true) {
        int next = is.read();
        if (next == -1) {
          // We accept useless data after the last block,
          // say, empty lines.
          return null;
        }
        if (next == '-') {
          hyphen++;
        } else {
          hyphen = 0;
          last = next;
        }
        if (hyphen == 5 && (last == -1 || last == '\r' || last == '\n')) {
          break;
        }
      }

      // Step 2: Read the rest of header, determine the line end
      int end;
      StringBuffer header = new StringBuffer("-----");
      while (true) {
        int next = is.read();
        if (next == -1) {
          throw new IOException("Incomplete data");
        }
        if (next == '\n') {
          end = '\n';
          break;
        }
        if (next == '\r') {
          next = is.read();
          if (next == -1) {
            throw new IOException("Incomplete data");
          }
          if (next == '\n') {
            end = '\n';
          } else {
            end = '\r';
            data[pos++] = (char) next;
          }
          break;
        }
        header.append((char) next);
      }

      // Step 3: Read the data
      while (true) {
        int next = is.read();
        if (next == -1) {
          throw new IOException("Incomplete data");
        }
        if (next != '-') {
          data[pos++] = (char) next;
          if (pos >= data.length) {
            data = Arrays.copyOf(data, data.length + 1024);
          }
        } else {
          break;
        }
      }

      // Step 4: Consume the footer
      StringBuffer footer = new StringBuffer("-");
      while (true) {
        int next = is.read();
        // Add next == '\n' for maximum safety, in case endline
        // is not consistent.
        if (next == -1 || next == end || next == '\n') {
          break;
        }
        if (next != '\r') footer.append((char) next);
      }

      checkHeaderFooter(header.toString(), footer.toString());

      return Base64.getMimeDecoder().decode(new String(data, 0, pos));
    }
  }