Android Studio: Custom Writer Class extends BufferedWriter, uses super and this.write (PemWriter)












2















The following is an example of the Class that I have to use. I'm trying to figure out how to use it. So, I can't change this part, only what's in my activity:



    package com.example.android.datafrominternet.utilities;

import android.util.Log;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.Writer;

public class MyWriter extends BufferedWriter {

public MyWriter(Writer out)
{
super(out);
}

public void writeTest(String repeatthis) throws IOException {
this.write(repeatthis);
this.newLine();
this.write("along with this other stuff.");

Log.d("LOOK HERE", "writeTest was accessed successfully"+repeatthis);
}
}


This is what's in my activity:



    String iwantThisString = "";
StringWriter writer = new StringWriter();
MyWriter myObjectWrites = new MyWriter(writer);
String myNewString = "I want to see this repeated back to me.";

try {
myObjectWrites.writeTest(myNewString);
iwantThisString = writer.toString(); //Does not work.
} catch (IOException e) {
e.printStackTrace();
}
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}


How do I get iwantThisString, to pick up what writeTest is laying down?



If you want to see the actual class that I'm trying to use, it's the pemWriter from Spongy Castle.










share|improve this question

























  • I'm afraid you've neglected to ask a specific question.

    – James K Polk
    Nov 18 '18 at 22:13











  • I've added my specific questions. Thanks

    – Adam Winter
    Nov 18 '18 at 22:49
















2















The following is an example of the Class that I have to use. I'm trying to figure out how to use it. So, I can't change this part, only what's in my activity:



    package com.example.android.datafrominternet.utilities;

import android.util.Log;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.Writer;

public class MyWriter extends BufferedWriter {

public MyWriter(Writer out)
{
super(out);
}

public void writeTest(String repeatthis) throws IOException {
this.write(repeatthis);
this.newLine();
this.write("along with this other stuff.");

Log.d("LOOK HERE", "writeTest was accessed successfully"+repeatthis);
}
}


This is what's in my activity:



    String iwantThisString = "";
StringWriter writer = new StringWriter();
MyWriter myObjectWrites = new MyWriter(writer);
String myNewString = "I want to see this repeated back to me.";

try {
myObjectWrites.writeTest(myNewString);
iwantThisString = writer.toString(); //Does not work.
} catch (IOException e) {
e.printStackTrace();
}
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}


How do I get iwantThisString, to pick up what writeTest is laying down?



If you want to see the actual class that I'm trying to use, it's the pemWriter from Spongy Castle.










share|improve this question

























  • I'm afraid you've neglected to ask a specific question.

    – James K Polk
    Nov 18 '18 at 22:13











  • I've added my specific questions. Thanks

    – Adam Winter
    Nov 18 '18 at 22:49














2












2








2








The following is an example of the Class that I have to use. I'm trying to figure out how to use it. So, I can't change this part, only what's in my activity:



    package com.example.android.datafrominternet.utilities;

import android.util.Log;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.Writer;

public class MyWriter extends BufferedWriter {

public MyWriter(Writer out)
{
super(out);
}

public void writeTest(String repeatthis) throws IOException {
this.write(repeatthis);
this.newLine();
this.write("along with this other stuff.");

Log.d("LOOK HERE", "writeTest was accessed successfully"+repeatthis);
}
}


This is what's in my activity:



    String iwantThisString = "";
StringWriter writer = new StringWriter();
MyWriter myObjectWrites = new MyWriter(writer);
String myNewString = "I want to see this repeated back to me.";

try {
myObjectWrites.writeTest(myNewString);
iwantThisString = writer.toString(); //Does not work.
} catch (IOException e) {
e.printStackTrace();
}
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}


How do I get iwantThisString, to pick up what writeTest is laying down?



If you want to see the actual class that I'm trying to use, it's the pemWriter from Spongy Castle.










share|improve this question
















The following is an example of the Class that I have to use. I'm trying to figure out how to use it. So, I can't change this part, only what's in my activity:



    package com.example.android.datafrominternet.utilities;

import android.util.Log;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.Writer;

public class MyWriter extends BufferedWriter {

public MyWriter(Writer out)
{
super(out);
}

public void writeTest(String repeatthis) throws IOException {
this.write(repeatthis);
this.newLine();
this.write("along with this other stuff.");

Log.d("LOOK HERE", "writeTest was accessed successfully"+repeatthis);
}
}


This is what's in my activity:



    String iwantThisString = "";
StringWriter writer = new StringWriter();
MyWriter myObjectWrites = new MyWriter(writer);
String myNewString = "I want to see this repeated back to me.";

try {
myObjectWrites.writeTest(myNewString);
iwantThisString = writer.toString(); //Does not work.
} catch (IOException e) {
e.printStackTrace();
}
try {
writer.close();
} catch (IOException e) {
e.printStackTrace();
}


How do I get iwantThisString, to pick up what writeTest is laying down?



If you want to see the actual class that I'm trying to use, it's the pemWriter from Spongy Castle.







java android bouncycastle spongycastle






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 21 '18 at 18:08







Adam Winter

















asked Nov 18 '18 at 22:07









Adam WinterAdam Winter

112




112













  • I'm afraid you've neglected to ask a specific question.

    – James K Polk
    Nov 18 '18 at 22:13











  • I've added my specific questions. Thanks

    – Adam Winter
    Nov 18 '18 at 22:49



















  • I'm afraid you've neglected to ask a specific question.

    – James K Polk
    Nov 18 '18 at 22:13











  • I've added my specific questions. Thanks

    – Adam Winter
    Nov 18 '18 at 22:49

















I'm afraid you've neglected to ask a specific question.

– James K Polk
Nov 18 '18 at 22:13





I'm afraid you've neglected to ask a specific question.

– James K Polk
Nov 18 '18 at 22:13













I've added my specific questions. Thanks

– Adam Winter
Nov 18 '18 at 22:49





I've added my specific questions. Thanks

– Adam Winter
Nov 18 '18 at 22:49












2 Answers
2






active

oldest

votes


















0














Here's the full explanation for how this works, to the best of my knowledge (now that I figured it out):



public class MyWriter extends BufferedWriter {


In creating the class MyWriter that extends BufferedWriter, MyWriter becomes an identical class to BufferedWriter, until you change something.



    public MyWriter(Writer out)
{
super(out);
}


This is the constructor. It looks like a method/function, but it's only called when you create a 'new MyWriter' and it is used to define that new object according these instructions. In this case, creating a new instance of MyWriter requires an input parameter '(Writer out)', which is the StringWriter named 'writer' in the activity. That is, it requires another Writer as an input and calls it 'out'. 'Super' sends 'out' up to the BufferedWriter. In doing this, you've "wrapped" the StringWriter in a BufferedWriter. In other words, you've put a StringWriter inside a BufferedWriter. The result combines the functionality of both kinds of Writer; a "buffered-string-writer" if you will.



    public MyWriter writeTest(String repeatthis) throws IOException {
this.write(repeatthis);
this.newLine();
this.write("along with this other stuff.");


An important thing to note here is that 'this' refers to the BufferedWriter. It's essentially appending each string to an unnamed string that exists inside the BufferedWriter.



The most important thing is that you have to use 'flush()' to dump everything out of the BufferedWriter to get it where it's supposed to go. In this case, 'flush()' finally sends the string to the StringWriter, where it will remain, until you pull it out of there. The following is the proper working code:



    StringWriter writer = new StringWriter();
MyWriter myObjectWrites = new MyWriter(writer);
String myNewString = "I want to see this repeated back to me.";

try {
myObjectWrites.writeTest(myNewString);
} catch (IOException e) {
e.printStackTrace();
}
try {
myObjectWrites.flush();
myObjectWrites.close();
} catch (IOException e) {
e.printStackTrace();
}
return writer.toString();


Note that you close() the BufferedWriter object 'myObjectWrites' NOT the StringWriter object 'writer'. Also, according to documentation, 'close()' also flushes the BufferedWriter, but I'm still using 'flush()' to be safe.






share|improve this answer































    0














    Also, I've found that the following code does work with the spongy castle library, taken from:
    Export RSA public key to PEM String using java



        PublicKey publicKey = keyPair.getPublic();
    StringWriter writer = new StringWriter();
    PemWriter pemWriter = new PemWriter(writer);
    pemWriter.writeObject(new PemObject("PUBLIC KEY", publicKey.getEncoded()));
    pemWriter.flush();
    pemWriter.close();
    return writer.toString();





    share|improve this answer

























      Your Answer






      StackExchange.ifUsing("editor", function () {
      StackExchange.using("externalEditor", function () {
      StackExchange.using("snippets", function () {
      StackExchange.snippets.init();
      });
      });
      }, "code-snippets");

      StackExchange.ready(function() {
      var channelOptions = {
      tags: "".split(" "),
      id: "1"
      };
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function() {
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled) {
      StackExchange.using("snippets", function() {
      createEditor();
      });
      }
      else {
      createEditor();
      }
      });

      function createEditor() {
      StackExchange.prepareEditor({
      heartbeatType: 'answer',
      autoActivateHeartbeat: false,
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader: {
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      },
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      });


      }
      });














      draft saved

      draft discarded


















      StackExchange.ready(
      function () {
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53365942%2fandroid-studio-custom-writer-class-extends-bufferedwriter-uses-super-and-this%23new-answer', 'question_page');
      }
      );

      Post as a guest















      Required, but never shown

























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes









      0














      Here's the full explanation for how this works, to the best of my knowledge (now that I figured it out):



      public class MyWriter extends BufferedWriter {


      In creating the class MyWriter that extends BufferedWriter, MyWriter becomes an identical class to BufferedWriter, until you change something.



          public MyWriter(Writer out)
      {
      super(out);
      }


      This is the constructor. It looks like a method/function, but it's only called when you create a 'new MyWriter' and it is used to define that new object according these instructions. In this case, creating a new instance of MyWriter requires an input parameter '(Writer out)', which is the StringWriter named 'writer' in the activity. That is, it requires another Writer as an input and calls it 'out'. 'Super' sends 'out' up to the BufferedWriter. In doing this, you've "wrapped" the StringWriter in a BufferedWriter. In other words, you've put a StringWriter inside a BufferedWriter. The result combines the functionality of both kinds of Writer; a "buffered-string-writer" if you will.



          public MyWriter writeTest(String repeatthis) throws IOException {
      this.write(repeatthis);
      this.newLine();
      this.write("along with this other stuff.");


      An important thing to note here is that 'this' refers to the BufferedWriter. It's essentially appending each string to an unnamed string that exists inside the BufferedWriter.



      The most important thing is that you have to use 'flush()' to dump everything out of the BufferedWriter to get it where it's supposed to go. In this case, 'flush()' finally sends the string to the StringWriter, where it will remain, until you pull it out of there. The following is the proper working code:



          StringWriter writer = new StringWriter();
      MyWriter myObjectWrites = new MyWriter(writer);
      String myNewString = "I want to see this repeated back to me.";

      try {
      myObjectWrites.writeTest(myNewString);
      } catch (IOException e) {
      e.printStackTrace();
      }
      try {
      myObjectWrites.flush();
      myObjectWrites.close();
      } catch (IOException e) {
      e.printStackTrace();
      }
      return writer.toString();


      Note that you close() the BufferedWriter object 'myObjectWrites' NOT the StringWriter object 'writer'. Also, according to documentation, 'close()' also flushes the BufferedWriter, but I'm still using 'flush()' to be safe.






      share|improve this answer




























        0














        Here's the full explanation for how this works, to the best of my knowledge (now that I figured it out):



        public class MyWriter extends BufferedWriter {


        In creating the class MyWriter that extends BufferedWriter, MyWriter becomes an identical class to BufferedWriter, until you change something.



            public MyWriter(Writer out)
        {
        super(out);
        }


        This is the constructor. It looks like a method/function, but it's only called when you create a 'new MyWriter' and it is used to define that new object according these instructions. In this case, creating a new instance of MyWriter requires an input parameter '(Writer out)', which is the StringWriter named 'writer' in the activity. That is, it requires another Writer as an input and calls it 'out'. 'Super' sends 'out' up to the BufferedWriter. In doing this, you've "wrapped" the StringWriter in a BufferedWriter. In other words, you've put a StringWriter inside a BufferedWriter. The result combines the functionality of both kinds of Writer; a "buffered-string-writer" if you will.



            public MyWriter writeTest(String repeatthis) throws IOException {
        this.write(repeatthis);
        this.newLine();
        this.write("along with this other stuff.");


        An important thing to note here is that 'this' refers to the BufferedWriter. It's essentially appending each string to an unnamed string that exists inside the BufferedWriter.



        The most important thing is that you have to use 'flush()' to dump everything out of the BufferedWriter to get it where it's supposed to go. In this case, 'flush()' finally sends the string to the StringWriter, where it will remain, until you pull it out of there. The following is the proper working code:



            StringWriter writer = new StringWriter();
        MyWriter myObjectWrites = new MyWriter(writer);
        String myNewString = "I want to see this repeated back to me.";

        try {
        myObjectWrites.writeTest(myNewString);
        } catch (IOException e) {
        e.printStackTrace();
        }
        try {
        myObjectWrites.flush();
        myObjectWrites.close();
        } catch (IOException e) {
        e.printStackTrace();
        }
        return writer.toString();


        Note that you close() the BufferedWriter object 'myObjectWrites' NOT the StringWriter object 'writer'. Also, according to documentation, 'close()' also flushes the BufferedWriter, but I'm still using 'flush()' to be safe.






        share|improve this answer


























          0












          0








          0







          Here's the full explanation for how this works, to the best of my knowledge (now that I figured it out):



          public class MyWriter extends BufferedWriter {


          In creating the class MyWriter that extends BufferedWriter, MyWriter becomes an identical class to BufferedWriter, until you change something.



              public MyWriter(Writer out)
          {
          super(out);
          }


          This is the constructor. It looks like a method/function, but it's only called when you create a 'new MyWriter' and it is used to define that new object according these instructions. In this case, creating a new instance of MyWriter requires an input parameter '(Writer out)', which is the StringWriter named 'writer' in the activity. That is, it requires another Writer as an input and calls it 'out'. 'Super' sends 'out' up to the BufferedWriter. In doing this, you've "wrapped" the StringWriter in a BufferedWriter. In other words, you've put a StringWriter inside a BufferedWriter. The result combines the functionality of both kinds of Writer; a "buffered-string-writer" if you will.



              public MyWriter writeTest(String repeatthis) throws IOException {
          this.write(repeatthis);
          this.newLine();
          this.write("along with this other stuff.");


          An important thing to note here is that 'this' refers to the BufferedWriter. It's essentially appending each string to an unnamed string that exists inside the BufferedWriter.



          The most important thing is that you have to use 'flush()' to dump everything out of the BufferedWriter to get it where it's supposed to go. In this case, 'flush()' finally sends the string to the StringWriter, where it will remain, until you pull it out of there. The following is the proper working code:



              StringWriter writer = new StringWriter();
          MyWriter myObjectWrites = new MyWriter(writer);
          String myNewString = "I want to see this repeated back to me.";

          try {
          myObjectWrites.writeTest(myNewString);
          } catch (IOException e) {
          e.printStackTrace();
          }
          try {
          myObjectWrites.flush();
          myObjectWrites.close();
          } catch (IOException e) {
          e.printStackTrace();
          }
          return writer.toString();


          Note that you close() the BufferedWriter object 'myObjectWrites' NOT the StringWriter object 'writer'. Also, according to documentation, 'close()' also flushes the BufferedWriter, but I'm still using 'flush()' to be safe.






          share|improve this answer













          Here's the full explanation for how this works, to the best of my knowledge (now that I figured it out):



          public class MyWriter extends BufferedWriter {


          In creating the class MyWriter that extends BufferedWriter, MyWriter becomes an identical class to BufferedWriter, until you change something.



              public MyWriter(Writer out)
          {
          super(out);
          }


          This is the constructor. It looks like a method/function, but it's only called when you create a 'new MyWriter' and it is used to define that new object according these instructions. In this case, creating a new instance of MyWriter requires an input parameter '(Writer out)', which is the StringWriter named 'writer' in the activity. That is, it requires another Writer as an input and calls it 'out'. 'Super' sends 'out' up to the BufferedWriter. In doing this, you've "wrapped" the StringWriter in a BufferedWriter. In other words, you've put a StringWriter inside a BufferedWriter. The result combines the functionality of both kinds of Writer; a "buffered-string-writer" if you will.



              public MyWriter writeTest(String repeatthis) throws IOException {
          this.write(repeatthis);
          this.newLine();
          this.write("along with this other stuff.");


          An important thing to note here is that 'this' refers to the BufferedWriter. It's essentially appending each string to an unnamed string that exists inside the BufferedWriter.



          The most important thing is that you have to use 'flush()' to dump everything out of the BufferedWriter to get it where it's supposed to go. In this case, 'flush()' finally sends the string to the StringWriter, where it will remain, until you pull it out of there. The following is the proper working code:



              StringWriter writer = new StringWriter();
          MyWriter myObjectWrites = new MyWriter(writer);
          String myNewString = "I want to see this repeated back to me.";

          try {
          myObjectWrites.writeTest(myNewString);
          } catch (IOException e) {
          e.printStackTrace();
          }
          try {
          myObjectWrites.flush();
          myObjectWrites.close();
          } catch (IOException e) {
          e.printStackTrace();
          }
          return writer.toString();


          Note that you close() the BufferedWriter object 'myObjectWrites' NOT the StringWriter object 'writer'. Also, according to documentation, 'close()' also flushes the BufferedWriter, but I'm still using 'flush()' to be safe.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 21 '18 at 18:45









          Adam WinterAdam Winter

          112




          112

























              0














              Also, I've found that the following code does work with the spongy castle library, taken from:
              Export RSA public key to PEM String using java



                  PublicKey publicKey = keyPair.getPublic();
              StringWriter writer = new StringWriter();
              PemWriter pemWriter = new PemWriter(writer);
              pemWriter.writeObject(new PemObject("PUBLIC KEY", publicKey.getEncoded()));
              pemWriter.flush();
              pemWriter.close();
              return writer.toString();





              share|improve this answer






























                0














                Also, I've found that the following code does work with the spongy castle library, taken from:
                Export RSA public key to PEM String using java



                    PublicKey publicKey = keyPair.getPublic();
                StringWriter writer = new StringWriter();
                PemWriter pemWriter = new PemWriter(writer);
                pemWriter.writeObject(new PemObject("PUBLIC KEY", publicKey.getEncoded()));
                pemWriter.flush();
                pemWriter.close();
                return writer.toString();





                share|improve this answer




























                  0












                  0








                  0







                  Also, I've found that the following code does work with the spongy castle library, taken from:
                  Export RSA public key to PEM String using java



                      PublicKey publicKey = keyPair.getPublic();
                  StringWriter writer = new StringWriter();
                  PemWriter pemWriter = new PemWriter(writer);
                  pemWriter.writeObject(new PemObject("PUBLIC KEY", publicKey.getEncoded()));
                  pemWriter.flush();
                  pemWriter.close();
                  return writer.toString();





                  share|improve this answer















                  Also, I've found that the following code does work with the spongy castle library, taken from:
                  Export RSA public key to PEM String using java



                      PublicKey publicKey = keyPair.getPublic();
                  StringWriter writer = new StringWriter();
                  PemWriter pemWriter = new PemWriter(writer);
                  pemWriter.writeObject(new PemObject("PUBLIC KEY", publicKey.getEncoded()));
                  pemWriter.flush();
                  pemWriter.close();
                  return writer.toString();






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Nov 21 '18 at 18:51

























                  answered Nov 20 '18 at 20:56









                  Adam WinterAdam Winter

                  112




                  112






























                      draft saved

                      draft discarded




















































                      Thanks for contributing an answer to Stack Overflow!


                      • Please be sure to answer the question. Provide details and share your research!

                      But avoid



                      • Asking for help, clarification, or responding to other answers.

                      • Making statements based on opinion; back them up with references or personal experience.


                      To learn more, see our tips on writing great answers.




                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function () {
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53365942%2fandroid-studio-custom-writer-class-extends-bufferedwriter-uses-super-and-this%23new-answer', 'question_page');
                      }
                      );

                      Post as a guest















                      Required, but never shown





















































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown

































                      Required, but never shown














                      Required, but never shown












                      Required, but never shown







                      Required, but never shown







                      Popular posts from this blog

                      MongoDB - Not Authorized To Execute Command

                      How to fix TextFormField cause rebuild widget in Flutter

                      Npm cannot find a required file even through it is in the searched directory