AmigoOutputStream

  • 10
  • Locked
1 Change the AmigoOutputStream class so that it wraps the FileOutputStream class. Use inheritance. 2 When the close() method is called, perform the following sequence of actions: 2.1 Call the flush() method. 2.2 Append the following text "CodeGym © All rights reserved." Use the getBytes() method. 2.
You can't complete this task, because you're not signed in.
Comments (8)
  • Popular
  • New
  • Old
You must be signed in to leave a comment
yehuda b
Level 23 , Beersheba, Israel
18 August 2020, 21:56
I don't understand wrapper classes so well yet, but something tells me that this task is twisted and unnecessarily confusing. Why not inherit OutputStream class and override methods to be delegated to the FileOutputStream field (AmigoOutputStream instance)?? Now that you have to inherit FileOutputStream you have to call super constructor, with fileName as a parameter for no apparent reason while the main objective of the constructor is to assign the FileOutputStream parameter to the FileOutputStream field. Am I right??????? Edit: I see, the point of the task is to wrap FileOutputStream class. phew, wrappers are so confusing, almost like unwrapping a melted chewing candy.........
13 August 2020, 21:05
Class AmigoOutputStream required to call super, because there was no default constructor. Which made me to call super with String fileName. It doesn't feel right that I had to initialize using fileName and wasn't able to reuse reference to FileOutputStream object passed to AmigoOutputStream constructor. I am wondering if I did correctly or there is better way to do that. Any ideas? This is what I did
public AmigoOutputStream(FileOutputStream fileOutputStream) throws FileNotFoundException {
    super(fileName);
    this.fileOutputStream = fileOutputStream;
}
David Lavigne
Level 19 , Lynnwood, United States
10 April 2019, 02:20
This task seems off to me. The decorator (wrapper) pattern is often used as an alternative to inheritance, yet this task has us inherit from the decorated object class. This task calls for us to both inherit from the FileOutputStream and have an internal FileOutputStream reference. Since we inherit from FileOutputStream ,there should be no need to maintain the internal reference and have methods that only delegate to the internal reference. It seems to me, that if you inherit from the decorated object class you lose much of the benefits of the decorator pattern. Suggest this task be redesigned to not extend from FileOutputStream.
Clau Dia
Level 24 , Mendoza, Argentina
13 February 2019, 02:54
The appended text in the close() method was never actually written to the file. I coudn't make it work as I wanted, so I just tried to validate and get some suggestions from the mentors. I thought that the last condition would fail but, surprisingly, the task passed testing. Is this a bug?
Roman
Level 41
13 February 2019, 07:17
Give us your code please. We will check it.
Clau Dia
Level 24 , Mendoza, Argentina
23 February 2019, 02:24
Hi Roman! Sorry I couldn't reply earlier. Here is my code:
...
Roman
Level 41
25 February 2019, 12:34
Thank you. It is not a bug. "The appended text in the close() method was never actually written to the file." It was if you call close method. Try run your app with this main method:
public static void main(String[] args) throws IOException {
        new AmigoOutputStream(new FileOutputStream(fileName)).close();
    }
Clau Dia
Level 24 , Mendoza, Argentina
23 March 2019, 21:50
That worked. Thank you!