title: Custom Cipher Encrypt/Decrypt Stream
body: <p>This is my first question here, so if you have anything I can improve on (posts as well as coding style) I'm happy to know.</p>
  
  <p>I know that my coding is the very efficient, so I'll happily take your advises and improvements.</p>
  
  <p>Currently, I'm writing an <code>AtbashCipher</code> program, which has to encrypt and decrypt the input <code>String</code>. As with every tasks at University, there are some guidelines, which we need to follow, but I'm going to explain my current code first:</p>
  
  <p>import java.util.Scanner;</p>
  
  <pre><code>public class AtbashCipher {
  
      StringBuilder code = new StringBuilder("");
      String inputString;
      String coded;
  
      public String getInputString() {
          return this.inputString;
      }
  
      public void setInputString(String inputString) {
          this.inputString = inputString;
      }
  
      public String getCoded() {
          return this.coded;
      }
  
      public void setCoded(String coded) {
          this.coded = coded;
      }
  
      public char encode(char c) {
          String alphabet = "abcdefghijklmnopqrstuvwxyz";
          char help = Character.toLowerCase(c);
          if (alphabet.indexOf(help) &gt;= 0) {
              for (int i = 0; i &lt; alphabet.length(); i++) {
                  if (help == alphabet.toLowerCase().charAt(i)) {
                      c = alphabet.charAt(26 - i - 1);
                      code.append(c);
                  }
              }
          } else {
              code.append(c);
          }
  
          if (inputString.length() == code.length()) {
              System.out.println("Encode: " + code);
              coded = code.toString();
              inputString = coded;
              code.setLength(0);
          }
          return c;
      }
  
      public char decode(char c) {
          String alphabet = "abcdefghijklmnopqrstuvwxyz";
          char help = Character.toLowerCase(c);
          if (alphabet.indexOf(help) &gt;= 0) {
              for (int i = 0; i &lt; alphabet.length(); i++) {
                  if (help == alphabet.toLowerCase().charAt(i)) {
                      c = alphabet.charAt(26 - i - 1);
                      code.append(c);
                  }
              }
          } else {
              code.append(c);
          }
  
          if (inputString.length() == code.length()) {
              coded = code.toString();
              inputString = coded;
              System.out.println("Decode: " + code);
              code.setLength(0);
          }
          return c;
      }
  
      public static void main(String[] args) {
          Scanner sc = new Scanner(System.in);
          AtbashCipher atb = new AtbashCipher();
          System.out.print("To be translated: ");
          atb.setInputString(sc.nextLine());
          sc.close();
          for (int j = 0; j &lt; atb.getInputString().length(); j++) {
              char help = atb.getInputString().charAt(j);
              atb.encode(help);
          }
          for (int k = 0; k &lt; atb.getInputString().length(); k++) {
              char help = atb.getInputString().charAt(k);
              atb.decode(help);
          }
      }
  }
  </code></pre>
  
  <p>This code encrypts and decrypts the inputString, which is still need to replace with a Scanner-Input, but that's not a problem.
  We need to bring the methods decode and encode, which are basically the same in my code, but we need to split it anyway. Am I doing something wrong?
  (see edit, yes, I did something wrong.)</p>
  
  <p>The problem starts with the task: 
   We need to write a class <code>AtbashWriter</code> that implements the <code>java.io.Writer</code> and following that, the constructor needs to get these arguments:</p>
  
  <pre><code>public AtbashWriter(Writer base, AtbashCipher cipher)
  </code></pre>
  
  <p>Also, we need to override the <code>write</code>, <code>flush</code> and <code>close</code> methods, given by Java.</p>
  
  <pre><code>public void write(char[] cbuf, int off, int len)
  public void flush()
  public void close()
  </code></pre>
  
  <p>We need to do the same for the <code>Reader</code> as well.</p>
  
  <p>I'm really struggling at getting behind this concept of creating my own <code>Stream</code> and implementing it into my code.</p>
  
  <p>Can you please explain to me how that works and how my code could be more efficient? Errors etc. ?</p>
  
  <p>Please don't just link the java library, because I have some difficulties at understanding the concept from libraries, I always look up some examples.</p>
  
  <p>This task is from the University, but I don't get any grades on this, I just want to do it and practice for the exam.</p>
  
  <p>Every help is appreciated.
  Thank you :) </p>
  
  <p>Edit: I can't edit the first line of my post, so... hey guys.
  Edit2: I just saw that my methods are NOT fully functioning the way i want them, I'll try to correct them.
  Edit3: I edited the program, trying to post the Writer maybe today.</p>
  
  <p>Edit4: Late, but still not forgotten: I finished it and added it in here, problem is solved :) </p>
  
  <pre><code>private Reader base;
  private AtbashCipher cipher;
  
  public AtbashReader(Reader base, AtbashCipher cipher) {
      this.base = base;
      this.cipher = cipher;
  }
  
  public int read (char[] cbuf, int off, int len) throws IOException{
      int result= base.read(cbuf, off, len);
  
      for(int i = off; i &lt; (off+len); i++){
          cbuf[i] = cipher.decode(cbuf[i]);
      }
      return result;
  }
  
  public void close()throws IOException {
      base.close();
  }
  </code></pre>