PageRenderTime 27ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/src/org/nutz/lang/socket/SocketContext.java

http://nutz.googlecode.com/
Java | 52 lines | 39 code | 13 blank | 0 comment | 2 complexity | cf27e3aae7a4c9e74d43b85977cd45a4 MD5 | raw file
Possible License(s): Apache-2.0
  1. package org.nutz.lang.socket;
  2. import java.io.BufferedReader;
  3. import java.io.IOException;
  4. import java.io.OutputStream;
  5. import org.nutz.lang.Encoding;
  6. import org.nutz.lang.Lang;
  7. import org.nutz.lang.util.SimpleContext;
  8. public class SocketContext extends SimpleContext {
  9. private SocketAtom atom;
  10. public SocketContext(SocketAtom atom) {
  11. this.atom = atom;
  12. }
  13. public BufferedReader getReader() {
  14. return atom.br;
  15. }
  16. public String readLine() throws IOException {
  17. if (atom.socket.isClosed())
  18. return null;
  19. return atom.br.readLine();
  20. }
  21. public String getCurrentLine() {
  22. return atom.line;
  23. }
  24. public OutputStream getOutputStream() {
  25. return atom.ops;
  26. }
  27. public void write(String str) {
  28. if (!atom.socket.isClosed())
  29. try {
  30. atom.ops.write(str.getBytes(Encoding.UTF8));
  31. }
  32. catch (IOException e) {
  33. throw Lang.wrapThrow(e);
  34. }
  35. }
  36. public void writeLine(String str) {
  37. write(str + "\n");
  38. }
  39. }