Search |
||
My most-used utility methodsPosted by enicholas on October 17, 2006 at 10:48 AM PDT
I've been writing Java code for more than a decade now, and there are a handful of methods I've ended up copying & pasting (or, sadly, reimplementing) in virtually every program I've ever written. I'm not sure why these methods in particular seem to keep cropping up again and again, but nevertheless I end up using them everywhere. send(InputStream in, OutputStream out)This method takes data coming from an public static void send(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[2048];
int c;
while ((c = in.read(buffer)) > 0)
out.write(buffer, 0, c);
}
Over the years I've implemented several variations of this basic idea. I've had readFully(InputStream in)An extension of the public static byte[] readFully(InputStream in) throws IOException {
ByteArrayOutputStream buffer = new ByteArrayOutputStream();
send(in, buffer);
return buffer.toByteArray();
}
An obvious variant of this (assuming a similarly modified Character-based variantsThe String replacementOnce upon a time I had a simple method Now that the capability is in the JRE itself, my ten-year-old What about you?I've written lots of utility methods, of course, but these are the small handful that I seem to always need no matter what I'm doing. Given my background in client/server code, it's perhaps not surprising that they have to do with data transfer. What about you? Any methods that you just can't live without, that you've been copying from program to program for years? »
Related Topics >>
Java Desktop Comments
Comments are listed in date ascending order (oldest first)
|
||
|
|