Example #1
0
  private void ReadStream(ReqMsg m) {
    InputStream in = m.m_Stream;
    BufferedReader reader = null;
    try {

      if (m.m_Type.equals("download")) {
        ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();

        // this is storage overwritten on each iteration with bytes
        int bufferSize = 1024;
        byte[] buffer = new byte[bufferSize];

        // we need to know how may bytes were read to write them to the byteBuffer
        int len = 0;

        Toast.makeText(m_Context, "Starting download for " + m.m_CallbackName, Toast.LENGTH_LONG)
            .show();
        while ((len = in.read(buffer)) != -1) {
          byteBuffer.write(buffer, 0, len);
        }
        Toast.makeText(m_Context, "Finished downloading " + m.m_CallbackName, Toast.LENGTH_LONG)
            .show();

        m_Builder.SaveData(m.m_CallbackName, byteBuffer.toByteArray());
      } else {
        // results in evaluating data read from via http - fix if used from net
        reader = new BufferedReader(new InputStreamReader(in));
        String line = "";
        String all = "";
        while ((line = reader.readLine()) != null) {
          all += line + "\n";
        }
        // Log.i("starwisp","got data for "+m.m_CallbackName+"["+all+"]");

        synchronized (mLock) {
          m_Builder.DialogCallback(m_Context, m_Context.m_Name, m.m_CallbackName, all);
        }
      }
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      if (reader != null) {
        try {
          reader.close();
        } catch (IOException e) {
          e.printStackTrace();
        }
      }
    }
  }