Java Code Examples for org.apache.commons.codec.binary.Base64#encodeAsString()

The following examples show how to use org.apache.commons.codec.binary.Base64#encodeAsString() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TeaUtil.java    From yunpian-java-sdk with MIT License 5 votes vote down vote up
public static String encryptForYunpianV2(String info, String secret) throws UnsupportedEncodingException {
	if (StringUtil.isNullOrEmpty(secret))
		return info;
	int[] key = getKeyByApikey(getApiSecret(secret));
	byte[] bytes = encryptByTea(info, key);
	Base64 base64 = new Base64();
	return base64.encodeAsString(bytes);
}
 
Example 2
Source File: TDigestSerializer.java    From DataVec with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(TDigest td, JsonGenerator j, SerializerProvider sp) throws IOException, JsonProcessingException {
    try(ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos)){
        oos.writeObject(td);
        oos.close();
        byte[] bytes = baos.toByteArray();
        Base64 b = new Base64();
        String str = b.encodeAsString(bytes);
        j.writeStartObject();
        j.writeStringField("digest", str);
        j.writeEndObject();
    }
}
 
Example 3
Source File: TDigestSerializer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
@Override
public void serialize(TDigest td, JsonGenerator j, SerializerProvider sp) throws IOException, JsonProcessingException {
    try(ByteArrayOutputStream baos = new ByteArrayOutputStream(); ObjectOutputStream oos = new ObjectOutputStream(baos)){
        oos.writeObject(td);
        oos.close();
        byte[] bytes = baos.toByteArray();
        Base64 b = new Base64();
        String str = b.encodeAsString(bytes);
        j.writeStartObject();
        j.writeStringField("digest", str);
        j.writeEndObject();
    }
}
 
Example 4
Source File: VectorsConfiguration.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
public String toEncodedJson() {
    Base64 base64 = new Base64(Integer.MAX_VALUE);
    try {
        return base64.encodeAsString(this.toJson().getBytes("UTF-8"));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 5
Source File: WordVectorSerializer.java    From deeplearning4j with Apache License 2.0 5 votes vote down vote up
/**
 * This utility method serializes ElementPair into JSON + packs it into Base64-encoded string
 *
 * @return
 */
protected String toEncodedJson() {
    ObjectMapper mapper = SequenceElement.mapper();
    Base64 base64 = new Base64(Integer.MAX_VALUE);
    try {
        String json = mapper.writeValueAsString(this);
        String output = base64.encodeAsString(json.getBytes("UTF-8"));
        return output;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
 
Example 6
Source File: RDFStoreDAO.java    From aliada-tool with GNU General Public License v3.0 4 votes vote down vote up
/**
 * It loads triples in a graph in the RDF store using HTTP PUT.
 *
 * @param triplesFilename	the path of the file containing the triples to load.  
 * @param rdfSinkFolder		the URI of the RDF SINK folder in the RDF Store.  
 * @param user				the login required for authentication in the RDF store.
 * @param password			the password required for authentication in the RDF store.
 * @return true if the triples have been loaded. False otherwise.
 * @since 1.0
 */
public boolean loadDataIntoGraphByHTTP(final String triplesFilename, String rdfSinkFolder, final String user, final String password) {
	boolean done = false;
	//Get the triples file name
	final File triplesFile = new File(triplesFilename);
	final String triplesFilenameNoPath = triplesFile.getName();
	//Append the file name to the rdfSinkFolder 
	if (rdfSinkFolder != null && !rdfSinkFolder.endsWith("/")) {
		rdfSinkFolder = rdfSinkFolder + "/";
	}
	rdfSinkFolder = rdfSinkFolder + triplesFilenameNoPath;
	//HTTP Authentication
	final Base64 base64 = new Base64();
	final byte[] authenticationArray = new String(user + ":" + password).getBytes();
	final String encoding = base64.encodeAsString(authenticationArray);
	
	try {
		final URL putUrl = new URL(rdfSinkFolder);
		final HttpURLConnection connection = (HttpURLConnection) putUrl.openConnection();
		connection.setReadTimeout(2000);
		connection.setConnectTimeout(5000);
		connection.setDoOutput(true);
		connection.setInstanceFollowRedirects(false);
		connection.setRequestMethod("PUT");
		//Write Authentication
		connection.setRequestProperty  ("Authorization", "Basic " + encoding);			
		//Write File Data
		final OutputStream outStream = connection.getOutputStream();		
		final InputStream inStream = new FileInputStream(triplesFilename);
		final byte buf[] = new byte[1024];
		int len;
		while ((len = inStream.read(buf)) > 0) {
			outStream.write(buf, 0, len);
		}
		inStream.close();
		final int responseCode=((HttpURLConnection)connection).getResponseCode();
		if (responseCode>= 200 && responseCode<=202) {
			done = true;
		} else {
			done = false;
		}
		((HttpURLConnection)connection).disconnect();
	} catch (Exception exception) {
		done = false;
		LOGGER.error(MessageCatalog._00034_HTTP_PUT_FAILED, exception, rdfSinkFolder);
    }
	return done;
}
 
Example 7
Source File: CodeVerifier.java    From oxAuth with MIT License 4 votes vote down vote up
public static String base64UrlEncode(byte[] input) {
    Base64 base64 = new Base64(BaseNCodec.MIME_CHUNK_SIZE, EMPTY_BYTE_ARRAY, true);
    return base64.encodeAsString(input);
}
 
Example 8
Source File: EncodeStringBase64.java    From levelup-java-examples with Apache License 2.0 3 votes vote down vote up
@Test
public void string_base64_encoding_apache () {
	
	String randomPhrase = "Learn. Eat. Code.";
	
	Base64 base64 = new Base64();
	String encodedPhrase = base64.encodeAsString(randomPhrase.getBytes());
	
	assertEquals("TGVhcm4uIEVhdC4gQ29kZS4=", encodedPhrase);
}