Android Open Source - makler Bossa Provider






From Project

Back to project page makler.

License

The source code is released under:

GNU General Public License

If you think the Android project makler listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package pl.net.newton.Makler.history;
//from w  w w.  j a  v a 2s.c  om
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import android.content.Context;
import android.util.Log;
import pl.net.newton.Makler.db.symbol.Symbol;
import pl.net.newton.Makler.httpClient.Connector;

public class BossaProvider implements HistoryProvider {
  private static final String TAG = "Makler";

  private static final String HOST = "bossa.pl";

  private static final String INTRADAY_PATH = "/notowania/wykresy/data/n_int5.txt";

  private static final String HISTORY_PATH = "/notowania/wykresy/data/n.txt";

  private static final String QUERY = "id=%s";

  private static final String HISTORY_FILE_PREFIX = "history_cache_v2_";

  private static final String INTRADAY_FILE_PREFIX = "intraday_cache_v2_";

  private final Connector conn;

  private final Cache intradayCache;

  private final Cache historyCache;

  public BossaProvider(Context ctx) {
    conn = new Connector(HOST, 80);
    intradayCache = new Cache(INTRADAY_FILE_PREFIX, 5, 15 * 60, ctx);
    historyCache = new Cache(HISTORY_FILE_PREFIX, 5, 24 * 60 * 60, ctx);
  }

  public EntryListWithIndexes getIntraday(Symbol symbol, boolean force) {
    final String symbolName = symbol.getSymbol();
    if (intradayCache.hasKey(symbolName) && !force) {
      return new EntryListWithIndexes(intradayCache.getEntry(symbolName));
    }

    final String query = String.format(QUERY, symbolName);
    final EntryList entries = getEntries(INTRADAY_PATH, query);
    intradayCache.addEntry(symbolName, entries);
    return new EntryListWithIndexes(entries);
  }

  public EntryListWithIndexes getHistory(Symbol symbol, boolean force) {
    final String symbolName = symbol.getSymbol();
    if (historyCache.hasKey(symbolName) && !force) {
      return new EntryListWithIndexes(historyCache.getEntry(symbolName));
    }

    final String query = String.format(QUERY, symbolName);
    final EntryList entries = getEntries(HISTORY_PATH, query);
    historyCache.addEntry(symbolName, entries);
    return new EntryListWithIndexes(entries);
  }

  private EntryList getEntries(String path, String query) {
    boolean withTime = false;
    byte[] byteArray;
    EntryList list = new EntryList(0, false);
    int i = 0;
    try {
      Log.d(TAG, path);
      InputStream is = conn.get(path, query);
      ZipInputStream zis = new ZipInputStream(is);
      ByteArrayOutputStream bos = new ByteArrayOutputStream(150000);
      Log.d(TAG, "history loaded");
      ZipEntry entry = zis.getNextEntry();
      Log.d(TAG, "entry name: " + entry.getName());
      ChannelTools.copy(zis, bos);
      Log.d(TAG, "copied to ByteArray");

      byteArray = bos.toByteArray();
      ByteArrayInputStream bis = new ByteArrayInputStream(byteArray);
      int lines = count(bis);
      Log.d(TAG, "lines counted: " + lines);

      i = ByteArrayUtils.nextLine(byteArray, 0);
      withTime = ByteArrayUtils.search("<TIME>".getBytes("UTF-8"), byteArray, 0, i) != -1;
      list = new EntryList(lines, withTime);
    } catch (Exception e) {
      Log.e(TAG, "can't get entries", e);
      return list;
    }

    Log.d(TAG, "parsing lines");
    while (i < byteArray.length) {
      try {
        i = list.addEntry(byteArray, i);
      } catch (IndexOutOfBoundsException e) {
        Log.e(TAG, "index out of bounds", e);
        break;
      }
    }
    Log.d(TAG, "lines parse");
    return list;
  }

  public int count(InputStream is) throws IOException {
    byte[] c = new byte[1024];
    int count = 0;
    int readChars = 0;
    while ((readChars = is.read(c)) != -1) {
      for (int i = 0; i < readChars; ++i) {
        if (c[i] == '\n') {
          ++count;
        }
      }
    }
    return count;
  }

  public boolean rangeExist(final Symbol symbol, int range) {
    String symbolName = symbol.getSymbol();

    if (range >= 0 && range <= 2 && !intradayCache.hasKey(symbolName)) {
      return false;
    }

    if (range >= 3 && range <= 6 && !historyCache.hasKey(symbolName)) {
      return false;
    }

    return true;
  }
}




Java Source Code List

pl.net.newton.Makler.common.Configuration.java
pl.net.newton.Makler.common.DateFormatUtils.java
pl.net.newton.Makler.common.GpwUtils.java
pl.net.newton.Makler.common.LocaleUtils.java
pl.net.newton.Makler.common.NumberFormatUtils.java
pl.net.newton.Makler.db.Constants.java
pl.net.newton.Makler.db.DbHelper.java
pl.net.newton.Makler.db.SqlConnection.java
pl.net.newton.Makler.db.alert.AlertBuilder.java
pl.net.newton.Makler.db.alert.AlertChecker.java
pl.net.newton.Makler.db.alert.AlertValue.java
pl.net.newton.Makler.db.alert.Alert.java
pl.net.newton.Makler.db.alert.AlertsDao.java
pl.net.newton.Makler.db.alert.Event.java
pl.net.newton.Makler.db.alert.Subject.java
pl.net.newton.Makler.db.quote.QuoteField.java
pl.net.newton.Makler.db.quote.Quote.java
pl.net.newton.Makler.db.quote.QuotesDao.java
pl.net.newton.Makler.db.service.SqlProvider.java
pl.net.newton.Makler.db.symbol.SymbolBuilder.java
pl.net.newton.Makler.db.symbol.Symbol.java
pl.net.newton.Makler.db.symbol.SymbolsDb.java
pl.net.newton.Makler.db.wallet.WalletDb.java
pl.net.newton.Makler.db.wallet.WalletItemBuilder.java
pl.net.newton.Makler.db.wallet.WalletItem.java
pl.net.newton.Makler.gpw.DefaultQuotesReceiver.java
pl.net.newton.Makler.gpw.QuotesReceiver.java
pl.net.newton.Makler.gpw.ex.GpwException.java
pl.net.newton.Makler.gpw.service.QuotesListener.java
pl.net.newton.Makler.gpw.service.QuotesService.java
pl.net.newton.Makler.gpw.service.UpdatingThread.java
pl.net.newton.Makler.history.BossaProvider.java
pl.net.newton.Makler.history.ByteArrayUtils.java
pl.net.newton.Makler.history.Cache.java
pl.net.newton.Makler.history.ChannelTools.java
pl.net.newton.Makler.history.EntryListWithIndexes.java
pl.net.newton.Makler.history.EntryList.java
pl.net.newton.Makler.history.HistoryFilter.java
pl.net.newton.Makler.history.HistoryProvider.java
pl.net.newton.Makler.history.service.HistoryListener.java
pl.net.newton.Makler.history.service.HistoryService.java
pl.net.newton.Makler.httpClient.Connector.java
pl.net.newton.Makler.receivers.QuotesAlarmReceiver.java
pl.net.newton.Makler.receivers.StartupReceiver.java
pl.net.newton.Makler.service.ServiceManager.java
pl.net.newton.Makler.ui.About.java
pl.net.newton.Makler.ui.AbstractActivity.java
pl.net.newton.Makler.ui.Alerts.java
pl.net.newton.Makler.ui.FullScreenGraph.java
pl.net.newton.Makler.ui.Preferences.java
pl.net.newton.Makler.ui.QuoteDetails.java
pl.net.newton.Makler.ui.Quotes.java
pl.net.newton.Makler.ui.Symbols.java
pl.net.newton.Makler.ui.WalletForm.java
pl.net.newton.Makler.ui.WalletItemCalculator.java
pl.net.newton.Makler.ui.Wallet.java
pl.net.newton.Makler.ui.adapter.AlertsAdapter.java
pl.net.newton.Makler.ui.adapter.QuotesAdapter.java
pl.net.newton.Makler.ui.adapter.SymbolsAdapter.java
pl.net.newton.Makler.ui.adapter.WalletAdapter.java
pl.net.newton.Makler.ui.graph.GraphView.java
pl.net.newton.Makler.ui.graph.MaklerGraphicalView.java