How can I create a string stream to write to?
satya - Monday, December 17, 2007 10:05:41 AM
Then there is the printwriter
satya - Monday, December 17, 2007 10:14:56 AM
You can do this
StringWriter s = new StringWriter();
PrintWriter out = new PrintWriter(s);
...write something to out
StringBuffer output = s.getBuffer();
satya - 6/18/2017, 10:46:37 AM
Types of Java Output Streams
Types of Java Output Streams
satya - 6/18/2017, 10:47:01 AM
Known sub classes
ByteArrayOutputStream, 
FileOutputStream, 
FilterOutputStream, 
ObjectOutputStream, 
OutputStream, 
PipedOutputStream
satya - 6/18/2017, 10:49:21 AM
ByteArrayOutputStream
ByteArrayOutputStream
satya - 6/18/2017, 10:51:10 AM
Java outputstream
Show images for: Java outputstream
Amazing what images can tell you!
satya - 6/18/2017, 10:55:05 AM
Hierarchy of streams

satya - 6/18/2017, 10:55:43 AM
From explore java book, the chapter on io
satya - 6/18/2017, 11:03:08 AM
An example of using a ByteArrayOutputStream as a String Output Stream
private static String getObjectAsString(Object o)
   throws JAXBException
   {
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      try   {
         m.marshal(o,bos);
         return bos.toString();
      }
      finally   {
         FileUtils.closeStream(bos);
      }
   }
satya - 6/18/2017, 11:08:49 AM
StringBufferInputStream API
StringBufferInputStream API
satya - 6/18/2017, 11:13:42 AM
Java StringReader to an InputStream
Java StringReader to an InputStream
satya - 6/18/2017, 11:18:21 AM
Here are a set of solutions from SOF
satya - 6/18/2017, 11:27:45 AM
Going back and forth between objects and XML
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import com.ai.common.FileUtils;
/**
 * 
 * This is a test driver to test 
 * to see if the class can be persisted using XML as is
 * 
 * @see LoggedInUserKeysDataset
 * @see PLSUserDetail
 * 
 */
public class PersistentLoginSupportTestDriver 
{
   //Hava static marshallers
   private static JAXBContext jc;
   private static Marshaller m;
   private static Unmarshaller um;
   
   //Initialize them
   static
   {
      try {
         jc = JAXBContext.newInstance(LoggedInUserKeysDataset.class);
         m = jc.createMarshaller();
         um = jc.createUnmarshaller();
         m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
      }
      catch(JAXBException x) {
         throw new RuntimeException(x);
      }
   }
   public static void main(String[] args)
   {
      try   {
         test2();
      }
      catch(Exception x)   {
         throw new RuntimeException(x);
      }
   }
  
   private static void test2() throws Exception
   {
      LoggedInUserKeysDataset data = LoggedInUserKeysDataset.createASample();
       
       //Marshal to system output: java to xml
      printMessage("Direct output as XML");
       m.marshal(data,System.out);
       
       //get it as a string first
       String s = getObjectAsString(data);
      printMessage("String output as XML");
      System.out.println(s);
      
      //Read it back as object
      LoggedInUserKeysDataset newData 
      = (LoggedInUserKeysDataset)getStringAsObject(s);
      
       //get it as a string first
       String s1 = getObjectAsString(newData);
      printMessage("String output as XML");
      System.out.println(s1);
   }
   
   private static void printMessage(String x)
   {
      System.out.println("********************************");
      System.out.println(x);
      System.out.println("********************************");
   }
   private static String getObjectAsString(Object o)
   throws JAXBException
   {
      ByteArrayOutputStream bos = new ByteArrayOutputStream();
      try   {
         m.marshal(o,bos);
         return bos.toString();
      }
      finally   {
         FileUtils.closeStream(bos);
      }
   }
   private static Object getStringAsObject(String s)
   throws JAXBException
   {
      //create a string input stream
      return um.unmarshal(new ByteArrayInputStream(s.getBytes()));
   }
}//eof-class
satya - 6/18/2017, 11:28:53 AM
Supporting class with XML Annotations
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class LoggedInUserKeysDataset {
   
   /*
    * **************************************
    * Private data
    * **************************************
    */
   @XmlElement (name="PLSUserDetail")
   private List<PLSUserDetail> data = new ArrayList<PLSUserDetail>();
   
   public void addUserDetail(PLSUserDetail ud){data.add(ud);}
   public List<PLSUserDetail> getData() { return data;}
   
   /*
    * **************************************
    * Testing support
    * **************************************
    */
   public static LoggedInUserKeysDataset createASample()
   {
      LoggedInUserKeysDataset lds = new LoggedInUserKeysDataset();
      
      PLSUserDetail ud = createASampleUserDetail("1");
      lds.data.add(ud);
      
      ud = createASampleUserDetail("2");
      lds.data.add(ud);
      
      ud = createASampleUserDetail("3");
      lds.data.add(ud);
      return lds;
   }
   private static PLSUserDetail createASampleUserDetail(String variation)
   {
      //Create user1
      PLSUserDetail ud = new PLSUserDetail("user" + variation);
      ud.addRandomKey("key1");
      ud.addRandomKey("key2");
      return ud;
   }
}//eof-class
satya - 6/18/2017, 11:30:39 AM
Do you have to close byte array streams in Java?
Do you have to close byte array streams in Java?
Search for: Do you have to close byte array streams in Java?
satya - 6/18/2017, 2:17:56 PM
FileOutputStream
FileOutputStream(String name, boolean append)
Creates a file output stream to write to the file with the specified name.