Last updated 2004-06-28 by Roedy
Green ©1996-2004 Canadian Mind Products
Java definitions: 0-9 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
You are here : home : Java Glossary : A words : armouring.
There are many data transport systems that either ignore, act on or otherwise meddle with control characters embedded in the data. They may trim trailing blanks, change line end characters, convert tabs to spaces etc. etc. Any of these actions would totally corrupt binary data. To pass binary data through such a meddlesome channel, e.g. the email system, it must first be armoured, converted to use only safe printable characters that will not be meddled with, e.g. a-z A-Z 0-9 and the vanilla punctuation.
MIME email and email attachments have a configurable encoding scheme, controlled via the Transfer-Content-Encoding mime header, often base64 or Quoted-Printable.
Unfortunately this bulks the message up by 30 to 300% depending on the technique you use. The other end has to recognise the armouring technique and do the reverse to get he binary back.
When 8-bit data are encoded in printable characters, the more printable characters used in the representation, generally the more efficient the protocol. However, the more characters used, the greater the odds one of the characters used will be interfered with by your communication channel.
// Base64 armouring // source available at http://mindprod.com/products.html#BASE64 import com.mindprod.base64.Base64; ... // sample byte array to encode byte[] toSend = { (byte)0xfc, (byte)0x0f, (byte)0xc0 }; // create encoder object Base64 base64 = new Base64(); base64.setLineLength( 72 ); // default // encoding a byte[] String send = base64.encoder( toSend ); // decoding a byte[] byte[] reconstituted = base64.decoder( sent );
Base64 uses an small cast of characters to convert 8-bit data into printable characters: a to z, A to Z, 0 to 9, + / and =. You might do this to convert any binary data to printable. This makes base64 suitable for encoding binary data as SQL strings, that will work no matter what the encoding. Unfortunately + / and = all have special meaning in URLs. See Base64 for free Java source code. Every three characters in the original fluff up to four characters in the encoded form. This 33% increase in size occurs independent of what characters appear in your data. At the receiving end you convert the printable characters back to the 8-bit data.
// URL-Encoder armouring import java.net.URLEncoder; import java.net.URLDecoder; ... String toSend = "The \"quick\" brown\nfox."; // encoding a String String send = URLEncoder( toSend, "UTF-8" ); // decoding the String String reconstituted = URLDecoder( sent, "UTF-8" );
You see this used to encode URLs in messages sent by a browser to a server. java.net.URLEncoder uses the following set of characters to convert 8-bit data into printable characters :a to z, A to Z, 0 to 9, -, ., *, and _. It works like this:
// Base64u armouring // source available at http://mindprod.com/products.html#BASE64 import com.mindprod.base64.Base64u; ... // sample byte array to encode byte[] toSend = { (byte)0xfc, (byte)0x0f, (byte)0xc0 }; // create encoder object Base64u base64u = new Base64u(); base64u.setLineLength( 72 ); // default // encoding a byte[] String send = base64u.encoder( toSend ); // decoding a byte[] byte[] reconstituted = base64u.decoder( sent );
// Wrapper serialiazation, compression and base64u armouring. // source code available at http://mindprod.com/products.html#WRAPPER import com.mindprod.wrapper.Wrap; import com.mindprod.wrapper.UnWrap; ... Wrap w = new Wrap(); Anything original message = new Anything(); // you can send any Serializable object, including Strings and raw byte arrays. String transport = w.wrap( originalMessage ); // transport String is sent to another machine... and then Unwrap u = new Unwrap(); Anything reconstitutedMessage = (Anything)u.unwrap( transport );
Use it when you want to include arbitrary Java Objects in your CGI GETS and POSTS.
home |
Canadian Mind Products | |||
| mindprod.com IP:[24.87.56.253] | ||||
| Your IP:[80.134.30.163] | ||||
| You are visitor number 567. | ||||
| Please send errors, omissions and suggestions | ||||
| to improve this page to Roedy Green. | ||||
| You can get a fresh copy of this page from: | or possibly from your local J: drive mirror: | |||
| http://mindprod.com/jgloss/armouring.html | J:\mindprod\jgloss\armouring.html | |||