IORequest.java 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /*
  2. * @author Ranides Atterwim <ranides@gmail.com>
  3. * @copyright Ranides Atterwim
  4. * @license WTFPL
  5. * @url http://ranides.net/projects/assira
  6. */
  7. package net.ranides.assira.io;
  8. import edu.umd.cs.findbugs.annotations.SuppressWarnings;
  9. import java.io.Serializable;
  10. import java.util.Arrays;
  11. import net.ranides.assira.events.Event;
  12. /**
  13. *
  14. * @author ranides
  15. */
  16. @SuppressWarnings({
  17. "EI_EXPOSE_REP",
  18. "PMD"
  19. })
  20. public abstract class IORequest implements Event, Serializable {
  21. private static final long serialVersionUID = 1L;
  22. private static final class Li { // NOPMD lazy init idiom
  23. static final Close CLOSE = new Close();
  24. static final Flush FLUSH = new Flush();
  25. }
  26. public static Close close() {
  27. return Li.CLOSE;
  28. }
  29. public static Flush flush() {
  30. return Li.FLUSH;
  31. }
  32. public static WriteByte write(int value) {
  33. return new WriteByte(value);
  34. }
  35. public static WriteByteArray write(byte[] array) {
  36. return new WriteByteArray(Arrays.copyOf(array, array.length));
  37. }
  38. public static WriteByteArray write(byte[] array, int offset, int length) {
  39. return new WriteByteArray(Arrays.copyOfRange(array, offset, offset + length));
  40. }
  41. public static class Close extends IORequest {
  42. private static final long serialVersionUID = 1L;
  43. protected Close() {
  44. // do nothing
  45. }
  46. @Override
  47. public String toString() {
  48. return "IORequest.Close";
  49. }
  50. }
  51. public static class Flush extends IORequest {
  52. private static final long serialVersionUID = 1L;
  53. protected Flush() {
  54. // do nothing
  55. }
  56. @Override
  57. public String toString() {
  58. return "IORequest.Flush";
  59. }
  60. }
  61. public static class WriteByte extends IORequest {
  62. private static final long serialVersionUID = 1L;
  63. private final int data;
  64. protected WriteByte(int data) {
  65. this.data = data;
  66. }
  67. public final int data() {
  68. return data;
  69. }
  70. @Override
  71. public String toString() {
  72. return "IORequest.WriteByte: " + data;
  73. }
  74. }
  75. public static class WriteByteArray extends IORequest {
  76. private static final long serialVersionUID = 1L;
  77. private final byte[] data;
  78. protected WriteByteArray(byte[] data) {
  79. this.data = data;
  80. }
  81. public byte[] data() {
  82. return data;
  83. }
  84. @Override
  85. public String toString() {
  86. return "IORequest.WriteByteArray: " + data.length;
  87. }
  88. }
  89. }