Feb 23

Decimal format uses BigDecimal.ROUND_HALF_EVEN and prior to 1.6 this cannot be changed.
In 1.6 a setRoundingMethod() was added to allow the rounding strategy to be changed.

DecimalFormat df = NumberFormat.getNumberInstance();
df.setRoundingMode(1, RoundingMode.HALF_UP);
String formatted = df.format(12.345);

If you need an alternate rounding strategy prior to 1.6 then you need to use BigDecimal to do your formatting.

double d = 123.45;
BigDecimal bd = new BigDecimal(d);
bd.setScale(1, BigDecimal.ROUND_HALF_UP);
String formatted = bd.toString();

written by objects \\ tags: , , , ,

Feb 22

Apache installations often announce lots of information about what they are running such as what OS is being used, the version of Apache, and what Apache Modules are loaded.

To disable this, add the following to your httpd.conf
ServerSignature Off

written by objects \\ tags: , ,

Feb 18

To send a HTTP GET request using Java can be done with the URL class. The openStream() method will send the GET request and return an input stream that can be used to read the HTTP response.

Wrapping the stream in a BufferedInputStream can be done to improve the I/O performance.

The ByteArrayOutputStream makes it easy to write the contents of the response to a byte array as shown in the following example.

URL url = new URL("http://www.objects.com.au/services/sherpa.html");
InputStream in = new BufferedInputStream(url.openStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while (-1!=(n=in.read(buf)))
{
   out.write(buf, 0, n);
}
out.close();
in.close();
byte[] response = out.toByteArray();

written by objects \\ tags: , , , ,