PageRenderTime 4ms CodeModel.GetById 1ms app.highlight 1ms RepoModel.GetById 1ms app.codeStats 0ms

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