PageRenderTime 37ms CodeModel.GetById 15ms app.highlight 16ms RepoModel.GetById 2ms app.codeStats 0ms

/razweb/src/com/razie/pubstage/comms/StrCommStream.java

http://razpub.googlecode.com/
Java | 107 lines | 71 code | 13 blank | 23 comment | 14 complexity | cf1f4271cf6bd39493e5bbebb2fb7554 MD5 | raw file
  1/**
  2 * Razvan's public code. 
  3 * Copyright 2008 based on Apache license (share alike) see LICENSE.txt for details.
  4 */
  5package com.razie.pubstage.comms;
  6
  7import java.io.BufferedReader;
  8import java.io.IOException;
  9import java.io.InputStream;
 10import java.io.InputStreamReader;
 11import java.net.URL;
 12import java.util.ArrayList;
 13import java.util.List;
 14
 15import com.razie.pub.comms.CommChannel;
 16
 17/**
 18 * a two way STRING communication channel. Adds filtering functionality
 19 * 
 20 * NOTE that you either read it line by line or all at once, can't combine them right now
 21 * 
 22 * @author razvanc99
 23 * 
 24 */
 25public class StrCommStream extends CommStream {
 26    List<IStrFilter> filters;
 27    BufferedReader   buffer;
 28
 29    /** empty channel - is is null */
 30    protected StrCommStream() {
 31        super((CommChannel)null);
 32    }
 33
 34    /** creates a comm channel with the remote URL */
 35    public StrCommStream(InputStream is, IStrFilter... f) {
 36        super(is);
 37        setFilters(f);
 38    }
 39
 40    /** creates a comm channel with the remote URL */
 41    public StrCommStream(URL url, IStrFilter... f) {
 42        super(url);
 43        setFilters(f);
 44    }
 45
 46    /** creates a comm channel with the remote URL */
 47    public StrCommStream(String url, IStrFilter... f) {
 48        super(url);
 49        setFilters(f);
 50    }
 51
 52    private void setup() {
 53        if (buffer == null)
 54            buffer = new BufferedReader(new InputStreamReader(is));
 55    }
 56
 57    /** will discard the rest of the stream */
 58    public void discard() {
 59        setup();
 60        String rest;
 61        try {
 62            rest = buffer.readLine();
 63            while (rest != null && rest.length() > 0)
 64                rest = buffer.readLine();
 65        } catch (IOException e) {
 66            throw new RuntimeException(e);
 67        }
 68    }
 69
 70    /** will read all incoming until the channel is empty */
 71    public String readAll() {
 72        String s = readStreamImpl();
 73        if (filters != null)
 74            for (IStrFilter f : filters)
 75                s = f.filter(s);
 76        return s;
 77    }
 78
 79    /**
 80     * will read incoming until the end of line OR the channel is empty
 81     * 
 82     * @return the next line in hte stream or NULL if empty
 83     */
 84    public String readLine() {
 85        String s = readStreamLineImpl();
 86        if (filters != null)
 87            for (IStrFilter f : filters)
 88                s = f.filter(s);
 89        return s;
 90    }
 91
 92    protected String readStreamLineImpl() {
 93        setup();
 94        try {
 95            return buffer.readLine();
 96        } catch (IOException e) {
 97            throw new RuntimeException(e);
 98        }
 99    }
100
101    public void setFilters(IStrFilter... f) {
102        if (filters == null)
103            filters = new ArrayList<IStrFilter>();
104        for (IStrFilter fi : f)
105            filters.add(fi);
106    }
107}