1 9 package org.jboss.portal.server.output; 10 11 import java.io.ByteArrayOutputStream ; 12 import java.io.IOException ; 13 import java.io.OutputStream ; 14 import java.io.PrintWriter ; 15 import java.io.StringWriter ; 16 17 import org.jboss.portal.server.WindowContext; 18 19 25 public class FragmentResult extends Result 26 { 27 28 private ByteArrayOutputStream bytes; 29 30 31 private StringWriter chars; 32 33 34 private PrintWriter writer; 35 36 37 private String contentType; 38 39 40 private String title; 41 42 public FragmentResult(WindowContext producer) 43 { 44 super(producer); 45 this.bytes = null; 46 this.chars = null; 47 this.writer = null; 48 this.contentType = null; 49 this.title = null; 50 } 51 52 public ByteArrayOutputStream getBytes() 53 { 54 return bytes; 55 } 56 57 public StringWriter getChars() 58 { 59 return chars; 60 } 61 62 public String getTitle() 63 { 64 return title; 65 } 66 67 public void setTitle(String title) 68 { 69 this.title = title; 70 } 71 72 public String getContentType() 73 { 74 return contentType; 75 } 76 77 public void setContentType(String contentType) 78 { 79 this.contentType = contentType; 80 } 81 82 85 public PrintWriter getWriter()throws IllegalStateException 86 { 87 if (bytes != null) 88 { 89 throw new IllegalStateException ("The window output stream is already used"); 90 } 91 if (contentType == null) 92 { 93 throw new IllegalStateException ("No content type defined"); 94 } 95 if (chars == null) 96 { 97 chars = new StringWriter (); 98 writer = new PrintWriter (chars); 99 } 100 return writer; 101 } 102 103 107 public OutputStream getOutputStream() throws IOException , IllegalStateException 108 { 109 if (chars != null) 110 { 111 throw new IllegalStateException ("The window writer is already used"); 112 } 113 if (contentType == null) 114 { 115 throw new IllegalStateException ("No content type defined"); 116 } 117 if (bytes == null) 118 { 119 bytes = new ByteArrayOutputStream (); 120 } 121 return bytes; 122 } 123 124 public void resetBuffer() 125 { 126 if (bytes != null) 127 { 128 bytes.reset(); 129 } 130 else if (chars != null) 131 { 132 chars.flush(); 133 chars.getBuffer().setLength(0); 134 } 135 } 136 } 137 | Popular Tags |