views:

115

answers:

2

I've inherited a bit of J2ME code where a single class has the following two methods in it:

public DataOutputStream getOutputStream(String filePath) throws IOException
{
    return Connector.openDataOutputStream(filePath);
}


public DataOutputStream createOutputStream(String filePath) throws IOException
{
    FileConnection fc = (FileConnection)Connector.open(filePath);

    if(fc.exists())
        return fc.openDataOutputStream();
    else
        fc.create();
    return fc.openDataOutputStream();
}

As far as I can tell, these two methods do exactly the same thing. Bizarrely, the methods are right next to each other in the class, implying that whoever put them there knew what they were doing.

Are these methods essentially the same? Can I get rid of one of them? (Or probably both, thinking about it).

A: 

FileConnector is available in an optional package only, so you cannot rely on its existence.

Zed
I should have said that filePath is always a "file://" url.
izb
+1  A: 

It seems to me that getOutputStream will throw an exception if the file doesn't exist.

You can only get rid of it if no one relies on that behavior.

The usual refactoring in that situation would be to have an added boolean parameter to createOutputStream to let it know whether it should create the file if it doesn't already exist.

QuickRecipesOnSymbianOS
+1 it's good to have agile method
Max Gontar