/src/lib/io/filter/base64_output_stream.e

http://github.com/tybor/Liberty · Specman e · 112 lines · 72 code · 11 blank · 29 comment · 0 complexity · b38d4b001eee051a7155ae3d4d1033ff MD5 · raw file

  1. -- This file is part of a Liberty Eiffel library.
  2. -- See the full copyright at the end.
  3. --
  4. class BASE64_OUTPUT_STREAM
  5. --
  6. -- A Base64 encoder. The underlying stream gets encoded in Base64 via this filter.
  7. --
  8. inherit
  9. FILTER_OUTPUT_STREAM
  10. redefine disconnect
  11. end
  12. create {ANY}
  13. connect_to
  14. feature {ANY}
  15. disconnect
  16. local
  17. k: INTEGER
  18. do
  19. -- Before closing the stream, add the padding characters:
  20. inspect
  21. state
  22. when 0 then
  23. -- nothing to add
  24. when 1 then
  25. k := previous_character.code & 3 |<< 4
  26. stream.filtered_put_character(t2b(k))
  27. stream.filtered_put_character('=')
  28. stream.filtered_put_character('=')
  29. when 2 then
  30. k := previous_character.code & 15 |<< 2
  31. stream.filtered_put_character(t2b(k))
  32. stream.filtered_put_character('=')
  33. end
  34. -- To be able to use the object again (via another `connect_to'):
  35. state := 0 -- Now close the stream:
  36. Precursor
  37. end
  38. feature {}
  39. local_can_disconnect: BOOLEAN True
  40. feature {FILTER_OUTPUT_STREAM}
  41. filtered_put_character (c: CHARACTER)
  42. local
  43. k: INTEGER
  44. do
  45. inspect
  46. state
  47. when 0 then
  48. k := c.code |>> 2
  49. stream.filtered_put_character(t2b(k))
  50. state := 1
  51. when 1 then
  52. k := previous_character.code & 3 |<< 4 | (c.code |>> 4)
  53. stream.filtered_put_character(t2b(k))
  54. state := 2
  55. when 2 then
  56. k := previous_character.code & 15 |<< 2 | (c.code |>> 6)
  57. stream.filtered_put_character(t2b(k))
  58. k := c.code & 63
  59. stream.filtered_put_character(t2b(k))
  60. state := 0
  61. end
  62. previous_character := c
  63. end
  64. filtered_flush
  65. do
  66. stream.filtered_flush
  67. end
  68. feature {}
  69. state: INTEGER
  70. previous_character: CHARACTER
  71. feature {}
  72. alphabet: STRING "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
  73. t2b (code: INTEGER): CHARACTER
  74. require
  75. code >= 0 and then code < 64
  76. do
  77. Result := alphabet.item(code + 1)
  78. ensure
  79. alphabet.has(Result)
  80. end
  81. end -- class BASE64_OUTPUT_STREAM
  82. --
  83. -- Copyright (C) 2009-2017: by all the people cited in the AUTHORS file.
  84. --
  85. -- Permission is hereby granted, free of charge, to any person obtaining a copy
  86. -- of this software and associated documentation files (the "Software"), to deal
  87. -- in the Software without restriction, including without limitation the rights
  88. -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  89. -- copies of the Software, and to permit persons to whom the Software is
  90. -- furnished to do so, subject to the following conditions:
  91. --
  92. -- The above copyright notice and this permission notice shall be included in
  93. -- all copies or substantial portions of the Software.
  94. --
  95. -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  96. -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  97. -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  98. -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  99. -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  100. -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  101. -- THE SOFTWARE.