Tagged: utility Toggle Comment Threads | Keyboard Shortcuts

  • Subinkrishna Gopi 8:17 pm on July 2, 2012 Permalink |
    Tags: , input, io, , , output, utility   

    My most favorite methods – part 2 

    Okay, here’s the second one. And it took me a few months to write this one πŸ˜›

     * Copies from an {@link InputStream} to an {@link OutputStream}
     * @param from
     * @param to
    public static void copyStream(InputStream from, OutputStream to)
    	byte[] contents = null;
    	int count = -1;
    	if ((null != from) && (null != to))
    			contents = new byte[1024];
    			while (-1 != (count = from.read(contents)))
    				to.write(contents, 0, count);
    		catch (Exception e)
    			contents = null;

    This method copies the contents from the input stream to the output stream. Do I need to explain more?

    • Andy Res 9:14 pm on July 2, 2012 Permalink

      It’s funny the way how you write the condition in the if and while loop, like you are checking the “null” and “-1” against your variables, which seems unusual for me. πŸ™‚
      I would write instead if(from!=null), and while((count=from.read(contents))!=-1) ..

    • Subinkrishna G 11:09 am on July 3, 2012 Permalink

      This is one practice that I follow from beginning πŸ˜‰

  • Subinkrishna Gopi 2:48 pm on March 5, 2009 Permalink |
    Tags: , , utility   

    The most commonly used (favorite) methods 

    Whenever we write code we will have certain set of code blocks – or methods written by us – which we will be using in every other method. isValidString(String aString) is my favorite method.

    public static boolean isValidString(String aString)
        return((null != aString) && (aString.trim().length() > 0));

    This one comes second in my list. This method is very useful if we are writing loads of conventional JSPs/Servlets.

    public static String getRequestParameter(HttpServletRequest request,
    		String paramName)
    	return (getRequestParameter(request, paramName, null));
    public static String getRequestParameter(HttpServletRequest request,
    		String paramName, String defaultValue)
    	String paramValue = null;
    	if ((null != request) && isValidString(paramName))
    		paramValue = request.getParameter(paramName);
    		paramValue = !Utility.isValidString(paramValue)
    			? defaultValue : paramValue;
    		paramValue = defaultValue;
    	return (paramValue);

    This method comes third. This method tries to implement in_array() of Php. This method checks whether the specified object is available in the given array.

    public static boolean inArray(Object[] array, Object key)
    boolean isKeyInArray = false;
    if (null != array)
    for (int i = 0; i < array.length; i++) { if (array[i].equals(key)) { isKeyInArray = true; break; } } } return (isKeyInArray); } [/sourcecode] But this was like reinventing the wheel. From Java 1.2, they had an API - java.util.Arrays - which does almost everything. I implementing it without knowing that (I am very bad).Β  But I love my code. So, what's your most commonly used (favorite) method?

Compose new post
Next post/Next comment
Previous post/Previous comment
Show/Hide comments
Go to top
Go to login
Show/Hide help
shift + esc