Пример #1
0
  // #sijapp cond.if (protocols_MRIM is "true") or (protocols_VK is "true") or (protocols_ICQ is
  // "true") #
  private byte[] read(InputStream in, int length) throws IOException {
    if (0 == length) {
      return null;
    }
    if (0 < length) {
      byte[] bytes = new byte[length];
      int readCount = 0;
      while (readCount < bytes.length) {
        int c = in.read(bytes, readCount, bytes.length - readCount);
        if (-1 == c) break;
        readCount += c;
      }
      return bytes;
    }

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    for (int i = 0; i < 100 * 1024; ++i) {
      int ch = in.read();
      if (-1 == ch) break;
      bytes.write(ch);
    }
    byte[] content = bytes.toByteArray();
    bytes.close();
    return content;
  }
Пример #2
0
  // Saves traffic from file
  private synchronized void save() throws IOException, RecordStoreException {
    // Open record store
    Storage traffic = new Storage("traffic");
    traffic.open(true);
    // Add empty records if necessary
    traffic.initRecords(2);

    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    DataOutputStream dos = new DataOutputStream(baos);

    // Add traffic amount and savedSince to record store
    dos.writeInt(allInTraffic + sessionInTraffic);
    dos.writeInt(allOutTraffic + sessionOutTraffic);
    dos.writeLong(savedSince.getTime());
    dos.writeLong(lastTimeUsed.getTime());
    generateCostSum(0, 0, false);
    dos.writeInt(savedCost);
    traffic.setRecord(2, baos.toByteArray());

    traffic.close();
  }
Пример #3
0
  private String getContent(String url) {
    HttpConnection httemp = null;
    InputStream istemp = null;
    String content = "";

    try {
      httemp = (HttpConnection) Connector.open(url);
      httemp.setRequestProperty("Connection", "cl" + "ose");
      if (HttpConnection.HTTP_OK != httemp.getResponseCode()) {
        throw new IOException();
      }

      istemp = httemp.openInputStream();
      int length = (int) httemp.getLength();
      if (-1 != length) {
        byte[] bytes = new byte[length];
        istemp.read(bytes);
        content = new String(bytes);

      } else {
        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        while (true) {
          int ch = istemp.read();
          if (-1 == ch) break;
          bytes.write(ch);
        }
        content = new String(bytes.toByteArray());
        bytes.close();
      }

    } catch (Exception e) {
      content = "Error: " + e.getMessage();
    }
    try {
      httemp.close();
      istemp.close();
    } catch (Exception e) {
    }
    return StringConvertor.removeCr(content);
  }