Example usage for com.lowagie.text.pdf.codec Base64 encodeBytes

List of usage examples for com.lowagie.text.pdf.codec Base64 encodeBytes

Introduction

In this page you can find the example usage for com.lowagie.text.pdf.codec Base64 encodeBytes.

Prototype

public static String encodeBytes(byte[] source) 

Source Link

Document

Encodes a byte array into Base64 notation.

Usage

From source file:com.jk.framework.util.FakeRunnable.java

License:Apache License

/**
 * Encode in to base 64./*from www  .  j  ava2s . c o m*/
 *
 * @param string
 *            the string
 * @return the string
 */
public static String encodeInToBase64(final String string) {
    return Base64.encodeBytes(string.getBytes());
}

From source file:com.main.Reporte.java

public static void sendRequestPdf(byte[] bytes) {
    try {//  w  w  w.  jav  a 2 s  .  com
        // com.lowagie.text.pdf.codec.Base64;
        String encodedString = Base64.encodeBytes(bytes);
        //System.out.println(encodedString);

        URL url = new URL("http://localhost:8000/api/rest/data/save/pdf");
        Map<String, Object> params = new LinkedHashMap<>();
        params.put("cod", "348");
        params.put("nom", "test nom");
        params.put("pdf", encodedString);

        StringBuilder postData = new StringBuilder();
        for (Map.Entry<String, Object> param : params.entrySet()) {
            if (postData.length() != 0)
                postData.append('&');
            postData.append(URLEncoder.encode(param.getKey(), "UTF-8"));
            postData.append('=');
            postData.append(URLEncoder.encode(String.valueOf(param.getValue()), "UTF-8"));
        }
        byte[] postDataBytes = postData.toString().getBytes("UTF-8");

        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setRequestMethod("POST");
        conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
        conn.setRequestProperty("Authorization", "Token 2ccb6e398170f60d172cfc671735c11f3a3c2e0f");
        conn.setRequestProperty("Content-Length", String.valueOf(postDataBytes.length));
        conn.setDoOutput(true);
        conn.getOutputStream().write(postDataBytes);

        /*
        Reader in = new BufferedReader(new InputStreamReader(conn.getInputStream(), "UTF-8"));
        for ( int c = in.read(); c != -1; c = in.read() ){
        System.out.print((char)c);
        }
        */

        BufferedReader in2 = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String line = null;
        while ((line = in2.readLine()) != null) {
            System.out.println(line);
        }

    } catch (MalformedURLException | UnsupportedEncodingException ex) {
        System.out.println(ex.getMessage());
    } catch (IOException ex) {
        System.out.println(ex.getMessage());
    }
}

From source file:com.pronoiahealth.olhie.server.services.BookCoverImageService.java

License:Open Source License

/**
 * Create the front cover and return as base 64 encoded string
 * /*w w  w.j  a va 2s  .  com*/
 * @param coverId
 * @param logoBytes
 * @param authorStr
 * @param titleStr
 * @param textColor
 *            - ex. #FFFFFF
 * @param width
 * @param height
 * @param type
 *            - ex. BufferedImage.TYPE_INT_ARGB
 * @param imgFormat
 *            - ex. ImageFormat.IMAGE_FORMAT_PNG
 * @return
 * @throws Exception
 */
public String createFrontCoverEncoded(String coverId, byte[] logoBytes, String authorStr, String titleStr,
        String spineColor, String authorTextColor, String titleTextColor, int width, int height, int type,
        ImageFormat imgFormat, int maxColors) throws Exception {

    return Base64.encodeBytes(createFrontCover(coverId, logoBytes, authorStr, titleStr, spineColor,
            authorTextColor, titleTextColor, width, height, type, imgFormat, maxColors));
}

From source file:com.pronoiahealth.olhie.server.services.BookCoverImageService.java

License:Open Source License

/**
 * Create a back cover//  w w w .  jav  a2  s. c  o  m
 * 
 * @param coverId
 * @param titleStr
 * @param textColor
 * @param width
 * @param height
 * @param type
 * @param imgFormat
 * @return
 * @throws Exception
 */
public String createBackCoverEncoded(String coverId, String titleStr, String spineColor, String textColor,
        int width, int height, int type, ImageFormat imgFormat, int maxColors) throws Exception {

    return Base64.encodeBytes(createBackCover(coverId, titleStr, spineColor, textColor, width, height, type,
            imgFormat, maxColors));
}

From source file:org.egomez.irpgeditor.env.AS400Systems.java

License:Open Source License

/**
 * saves the system settings./*from  ww  w .j  av a  2 s .c o m*/
 */
public void saveSettings() throws IOException {
    // FileOutputStream fos;
    Properties props;
    AS400System system;

    AesCipherService cipher = new AesCipherService();
    Key key = cipher.generateNewKey();
    byte[] secretBytes = null;
    ByteSource encrypted = null;

    props = new Properties();
    props.setProperty("system.count", Integer.toString(listSystems.size()));
    system = getDefault();
    if (system != null) {
        props.setProperty("system.default", system.getName());
    }
    for (int x = 0; x < listSystems.size(); x++) {
        system = listSystems.get(x);
        props.setProperty("system." + x + ".name", system.getName());
        props.setProperty("system." + x + ".address", system.getAddress());
        props.setProperty("system." + x + ".user", system.getUser());
        props.setProperty("system." + x + ".properties", Base64.encodeBytes(key.getEncoded()));
        // Encriptamos la clave
        secretBytes = CodecSupport.toBytes(system.getPassword());
        encrypted = cipher.encrypt(secretBytes, key.getEncoded());
        props.setProperty("system." + x + ".password", Base64.encodeBytes(encrypted.getBytes()));
    }
    // Creamos los directorios
    File file = new File(System.getProperty("user.home") + File.separator + ".iRPGEditor" + File.separator
            + "conf" + File.separator + "systems.properties");
    if (file.exists() == false) {
        file = new File(System.getProperty("user.home") + File.separator + ".iRPGEditor");
        file.mkdir();
        file = new File(
                System.getProperty("user.home") + File.separator + ".iRPGEditor" + File.separator + "conf");
        file.mkdir();
        file = new File(
                System.getProperty("user.home") + File.separator + ".iRPGEditor" + File.separator + "projects");
        file.mkdir();
    }
    file = new File(System.getProperty("user.home") + File.separator + ".iRPGEditor" + File.separator + "conf"
            + File.separator + "systems.properties");

    // fos = new FileOutputStream(System.getProperty("user.home") +
    // File.separator + ".iRPGEditor" + File.separator
    // + "conf" + File.separator + "systems.properties");
    save(props, file);
    // props.store(fos, "");
}

From source file:org.mapfish.print.map.readers.google.GoogleURLSigner.java

License:Open Source License

public String signature(String resource)
        throws NoSuchAlgorithmException, InvalidKeyException, UnsupportedEncodingException, URISyntaxException {

    // Get an HMAC-SHA1 signing key from the raw key bytes
    SecretKeySpec sha1Key = new SecretKeySpec(key, "HmacSHA1");

    // Get an HMAC-SHA1 Mac instance and initialize it with the HMAC-SHA1
    // key/*from  w  w w .  j  a v  a 2s  .co  m*/
    Mac mac = Mac.getInstance("HmacSHA1");
    mac.init(sha1Key);

    // compute the binary signature for the request
    byte[] sigBytes = mac.doFinal(resource.getBytes());

    // base 64 encode the binary signature
    String signature = Base64.encodeBytes(sigBytes);

    // convert the signature to 'web safe' base 64
    signature = signature.replace('+', '-');
    signature = signature.replace('/', '_');

    return signature;
}

From source file:services.DataTransportService.java

License:Apache License

/**
 *Reads bytes from files encodes them to base64 and adds them in a soap message.
 * It also invokes the WS specified in <code>endpoint</code>. In order to prevent
 * soap from crashing the file is read and packed in a soap message in small chunks
 *@param source. The local file or folder.
 *@param endpoint. The destination WS./*  w w  w  .j ava  2s  . c  o  m*/
 *@param dir. The remote directory to transfer files to
 *@return The destinations WS response. Usually the location were the files are saved
 */
private String sendFilesOverSoap(String source, String endpoint, String dir)
        throws ServiceException, MalformedURLException, IOException {
    FileInputStream fileStream = null;
    File f = new File(source);
    File currentFile;
    Service service = null;
    Call call = null;
    String result = null;
    service = new Service();
    Vector files = new Vector();

    call = (Call) service.createCall();

    call.setTargetEndpointAddress(new URL(endpoint + "DataTransportService"));
    call.setOperationName(new QName("getFilesOverSoap"));

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] tmp = new byte[500 * 1024];
    StringBuffer buffer = new StringBuffer();
    int len = 0;
    String data = "";
    if (f.isDirectory()) {
        for (int i = 0; i < f.listFiles().length; i++) {
            if (f.listFiles()[i].isFile()) {
                files.add(f.listFiles()[i].getAbsolutePath());
            }
        }
    } else if (f.isFile()) {
        files.add(f.getAbsolutePath());
    }

    int s = 0;
    dataSize = 0;
    for (int i = 0; i < files.size(); i++) {
        currentFile = new File((String) files.get(i));
        fileStream = new FileInputStream(currentFile);
        dataSize = (dataSize + currentFile.length());

        while ((len = fileStream.read(tmp)) != -1) {
            baos.write(tmp, 0, len);
            data = Base64.encodeBytes(baos.toByteArray());
            s = s + baos.toByteArray().length;
            //make sure soap will not crach
            if (/*Runtime.getRuntime().freeMemory() < (Runtime.getRuntime().totalMemory()/4)*/s >= 15
                    * 1048576) {
                Object[] args = { new String(currentFile.getName()), dir, buffer.toString().getBytes() };
                result = (String) call.invoke(args);
                totalSize = totalSize + getSOAPSize(call.getMessageContext().getRequestMessage());
                buffer.setLength(0);
                s = 0;
            } else {
                buffer.append(data);
            }
            baos.reset();
        }
        if (buffer.toString().length() >= 1) {
            Object[] args = { new String(currentFile.getName()), dir, buffer.toString().getBytes() };
            result = (String) call.invoke(args);
            totalSize = totalSize + getSOAPSize(call.getMessageContext().getRequestMessage());
            buffer.setLength(0);
        }
    }

    return result;
}