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