Tag Archives: encoding

Malformed \uxxxx encoding error in Java

malformed
You may see the following error while java execution on windows OS:

Malformed \uxxxx encoding.

Possible solution:
Check your *.properties files and fix windows-pathes

from

C:\Some\path

to

C:/Some/path

or

C:\\Some\\path

Java symbol escaping!

Set charset in Spring MVC


To set up encoding for site, you can use CharacterEncodingFilter in web.xml like this:


 encodingFilter
 org.springframework.web.filter.CharacterEncodingFilter
 
  encoding  
  UTF-8  
   
  
 forceEncoding  
 true  
   
  

  
encodingFilter  
/*  

Also http://stackoverflow.com/questions/138948/how-to-get-utf-8-working-in-java-webapps

Read and write text file in Java

Short for text file ex.

// Reading...
StringBuffer sb = new StringBuffer();
FileInputStream fis = null;
Reader reader = null;
try
{
	fis = new FileInputStream("in.txt");
	reader = new BufferedReader(new InputStreamReader(fis,"UTF-8"));
	char[] buffer = new char[1024];
	int read;
	while ((read = reader.read(buffer,0,buffer.length)) > 0)
		sb.append(buffer,0,read);
}
finally
{
	if (reader != null)
		reader.close();
	if (fis != null)
		fis.close();
}

// Processing...
String res = sb.toString() + "...OK!";

// Writing...
FileOutputStream fos = null;
Writer writer = null;
try
{
	fos = new FileOutputStream("out.txt");
	writer = new BufferedWriter(new OutputStreamWriter(fos,"UTF-8"));
	writer.write(res);
}
finally
{
	if (writer != null)
		writer.close();
	if (fos != null)
		fos.close();
}

Greate for me.

Read text file with Scanner in Java


Reading file with encoding.
Snipet:

File file = new File("filePath.txt");
Scanner scanner = new Scanner(new FileInputStream(file), "UTF-8");
try
{
	while (scanner.hasNextLine())
		System.out.println(scanner.nextLine());
}
finally
{
	scanner.close();
}

Read http response correctly!

If you’re using apache http client, method getResponseBodyAsString() set the following:

01 02 2012 17:44:52,484 WARN main org.apache.commons.httpclient.HttpMethodBase - Going to buffer response body of large or unknown size. Using getResponseBodyAsStream instead is recommended.

HttpClient client = new HttpClient();
HttpMethodBase method = new GetMethod("htp://site.com");
int statusCode = client.executeMethod(method);
if (statusCode != HttpStatus.SC_OK)
    throw new HttpException(method.getStatusText() + ": " + method.getResponseBodyAsString());
else
{
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    byte[] byteArray = new byte[1024];
    int count = 0;
    while ((count = method.getResponseBodyAsStream().read(byteArray,0,byteArray.length)) > 0)
        outputStream.write(byteArray,0,count);
    return new String(outputStream.toByteArray(),method.getParams().getContentCharset());
}

It’s very importante to use method.getParams().getContentCharset() for correct results! Be aware!

Write data file in Java


Snippet shows writing files to disk.

File outFile = new File("123.txt");
DataOutputStream dos = new DataOutputStream(new FileOutputStream(outFile));
dos.writeBytes("Some input string to save!");
dos.close();
PHP

Convert windows-1251 to utf8 encoding

function convertCp1251ToUtf8($str)
{
	static $table = array(
		"\xA8" => "\xD0\x81",
		"\xB8" => "\xD1\x91",
		"\xA1" => "\xD0\x8E",
		"\xA2" => "\xD1\x9E",
		"\xAA" => "\xD0\x84",
		"\xAF" => "\xD0\x87",
		"\xB2" => "\xD0\x86",
		"\xB3" => "\xD1\x96",
		"\xBA" => "\xD1\x94",
		"\xBF" => "\xD1\x97",
		"\x8C" => "\xD3\x90",
		"\x8D" => "\xD3\x96",
		"\x8E" => "\xD2\xAA",
		"\x8F" => "\xD3\xB2",
		"\x9C" => "\xD3\x91",
		"\x9D" => "\xD3\x97",
		"\x9E" => "\xD2\xAB",
		"\x9F" => "\xD3\xB3",
	);
	
	return preg_replace('#[\x80-\xFF]#se',
	' "$0" >= "\xF0" ? "\xD1".chr(ord("$0")-0x70) :
					   ("$0" >= "\xC0" ? "\xD0".chr(ord("$0")-0x30) :
						(isset($table["$0"]) ? $table["$0"] : "")
					   )',
	$str
	);
}

Special thanx to http://webkev.com/2011/03/01/konvertiruem-windows-1251-v-utf-8-s-pomoshhyu-php/