All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.pubnub.api.PubnubUtil Maven / Gradle / Ivy

Go to download

PubNub is a cross-platform client-to-client (1:1 and 1:many) push service in the cloud, capable of broadcasting real-time messages to millions of web and mobile clients simultaneously, in less than a quarter second!

There is a newer version: 3.7.10
Show newest version
package com.pubnub.api;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * PubnubUtil class provides utility methods like urlEncode etc
 * 
 * @author Pubnub
 * 
 */
public class PubnubUtil extends PubnubUtilCore {

	/**
	 * Returns encoded String
	 * 
	 * @param sUrl
	 *            , input string
	 * @return , encoded string
	 */
	public static String pamEncode(String sUrl) {
		/* !'()*~ */

		String encoded = urlEncode(sUrl);
		if (encoded != null) {
			encoded = encoded.replace("*", "%2A").replace("!", "%21")
					.replace("'", "%27").replace("(", "%28")
					.replace(")", "%29").replace("[", "%5B")
					.replace("]", "%5D").replace("~", "%7E");
		}
		return encoded;

	}

	/**
	 * Returns encoded String
	 * 
	 * @param sUrl
	 *            , input string
	 * @return , encoded string
	 */
	public static String urlEncode(String sUrl) {
		try {
			return URLEncoder.encode(sUrl, "UTF-8").replace("+", "%20");
		} catch (UnsupportedEncodingException e) {
			return null;
		}
	}

	/**
	 * Convert input String to JSONObject, JSONArray, or String
	 * 
	 * @param str
	 *            JSON data in string format
	 * 
	 * @return JSONArray or JSONObject or String
	 */
	static Object stringToJSON(String str) {
		try {
			return new JSONArray(str);
		} catch (JSONException e) {
		}
		try {
			return new JSONObject(str);
		} catch (JSONException ex) {
		}
		try {
			return Integer.parseInt(str);
		} catch (Exception ex) {
		}
		try {
			return Double.parseDouble(str);
		} catch (Exception ex) {
		}
		return str;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy