Esempio n. 1
0
  @Override
  public Object doTransform(Object src, String outputEncoding) throws TransformerException {
    try {
      if (src instanceof String) {
        return src.toString().getBytes(outputEncoding);
      } else if (src instanceof InputStream) {
        InputStream is = (InputStream) src;
        ByteArrayOutputStream byteOut = new ByteArrayOutputStream();
        try {
          IOUtils.copyLarge(is, byteOut);
        } finally {
          is.close();
        }
        return byteOut.toByteArray();
      } else if (src instanceof OutputHandler) {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();

        try {
          ((OutputHandler) src).write(RequestContext.getEvent(), bytes);

          return bytes.toByteArray();
        } catch (IOException e) {
          throw new TransformerException(this, e);
        }
      }
    } catch (Exception e) {
      throw new TransformerException(this, e);
    }

    return super.doTransform(src, outputEncoding);
  }
  @Override
  public Object doTransform(Object src, String encoding) throws TransformerException {
    try {
      if (src instanceof String) {
        return new ByteArrayInputStream(((String) src).getBytes(encoding));
      } else if (src instanceof byte[]) {
        return new ByteArrayInputStream((byte[]) src);
      } else if (src instanceof OutputHandler) {
        OutputHandler oh = (OutputHandler) src;

        ByteArrayOutputStream out = new ByteArrayOutputStream();
        oh.write(RequestContext.getEvent(), out);

        return new ByteArrayInputStream(out.toByteArray());
      } else {
        return new ByteArrayInputStream((byte[]) super.doTransform(src, encoding));
      }
    } catch (Exception e) {
      throw new TransformerException(this, e);
    }
  }