Return value from stream

I have a stream class below that reads a file from a URL and then saves the result in a public static variable, so it can be accessed from other classes. Is it better to achieve this?

thank

public class ReadContent implements Runnable{

    private HttpConnection connection;
    private InputStream inputStream;
    private String url;

    public ReadContent(String url){
        this.url = url;
    }

    public void run() {
        readContentURL();
    }

    private void readContentURL() {

        try {   
                connection = (HttpConnection)Connector.open(url);
                connection.setRequestMethod(HttpConnection.GET);
                connection.setRequestProperty("Connection", "close");
                inputStream = connection.openDataInputStream();

            //  inputStream = getClass().getResourceAsStream(url);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                int c ;
                while (true) {
                    c = inputStream.read();
                    if (c == -1)
                        break;
                    baos.write(c);
                }

                SavedJSON.result = new JSONObject(new String(baos.toByteArray()));

            } 
            catch(Exception e){
                e.printStackTrace();
            }   
    }

}

Here is my suggested solution -

public class MyFuture{ 
      private final Object lock = new Object();

      private JSONObject value;
      public void set(JSONObject t){
          value = t;
          synchronized(lock){
              value = t;
              lock.notifyAll();  
          }
      }

      public JSONObject get(){
         synchronized(lock){
              while(value == null)
                try {
                    lock.wait();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

              return value;
         }

      }    
    }

public class SavedJSON {

    public static MyFuture result;
    }

public class ReadContent implements Runnable{

    private HttpConnection connection;
    private InputStream inputStream;
    private String url;

    public ReadContent(String url){
        this.url = url;
    }

    public void run() {
        readContentURL();
    }

    private void readContentURL() {

        try {   
            int len = 0;
                connection = (HttpConnection)Connector.open(url);
                connection.setRequestMethod(HttpConnection.GET);
            //  connection.setRequestProperty("Connection", "close");
                inputStream = connection.openDataInputStream();

            //  inputStream = getClass().getResourceAsStream(url);
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                int c ;
                while (true) {
                    c = inputStream.read();
                    if (c == -1)
                        break;
                    ++len;
                    baos.write(c);
                }

                SavedJSON.result.set(new JSONObject(new String(baos.toByteArray(), 0, len, "utf-8")));

            } 
            catch(Exception e){
                e.printStackTrace();
            }   
    }

}
+3
source share
5 answers

Since you cannot use Callable (and I assume you cannot use the Future either), you can try and create your own Future. Its relatively simple:

public class MyFuture<T>{ // can you not use generics either?
  private final Object lock = new Object();

  private T value;
  public void set(T t){
      synchronized(lock){
          value = t;
          lock.notifyAll();  
      }
  }
  public T get(){
     synchronized(lock){
          while(value == null) lock.wait();

          return value;
     }

  }    
}

Now you can have SavedJSON.resultit MyFuture, and whenever someone wants this value and needs to wait, he can just call SavedJSON.result.get();, and obviously it can beSavedJSON.result.set(new JSONObject(new String(baos.toByteArray())));

Edit:

This will be your comment and change.

-: , . "" . , .

: = t . . , .

+2

Callable ( Runnable, ) Executors .

+1

, :

:

public interface SimpleCallback {
    public void onReceive(JSONObject data);
}

:

...
SimpleCallback callback = new SimpleCallback() { 
    public void onReceive(JSONObject data) {
        // do something
    }
}
new Thread(new ReadContent(url, callback));
...

:

...
    // read input stream
    callback.onReceive(new JSONObject(new String(baos.toByteArray())));
} catch(Exception e){
...

, .

+1

Your result is out of sync. You must improve your SavedJSON class to create the getResult () and setResult () methods and make them synchronized. wait () and notify () should also help you.

0
source

All Articles