You are viewing a plain text version of this content. The canonical link for it is here.
Posted to slide-dev@jakarta.apache.org by re...@apache.org on 2001/01/04 01:58:23 UTC

cvs commit: jakarta-slide/src/share/org/apache/slide/content NodeRevisionContent.java

remm        01/01/03 16:58:23

  Modified:    src/share/org/apache/slide/content NodeRevisionContent.java
  Log:
  - Implement missing functionality in NodeRevisionContent.
    Patch submitted by Juergen Pill.
  
  Revision  Changes    Path
  1.5       +150 -49   jakarta-slide/src/share/org/apache/slide/content/NodeRevisionContent.java
  
  Index: NodeRevisionContent.java
  ===================================================================
  RCS file: /home/cvs/jakarta-slide/src/share/org/apache/slide/content/NodeRevisionContent.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- NodeRevisionContent.java	2000/12/01 07:17:28	1.4
  +++ NodeRevisionContent.java	2001/01/04 00:58:23	1.5
  @@ -1,13 +1,13 @@
   /*
  - * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/content/NodeRevisionContent.java,v 1.4 2000/12/01 07:17:28 remm Exp $
  - * $Revision: 1.4 $
  - * $Date: 2000/12/01 07:17:28 $
  + * $Header: /home/cvs/jakarta-slide/src/share/org/apache/slide/content/NodeRevisionContent.java,v 1.5 2001/01/04 00:58:23 remm Exp $
  + * $Revision: 1.5 $
  + * $Date: 2001/01/04 00:58:23 $
    *
    * ====================================================================
    *
    * The Apache Software License, Version 1.1
    *
  - * Copyright (c) 1999 The Apache Software Foundation.  All rights 
  + * Copyright (c) 1999 The Apache Software Foundation.  All rights
    * reserved.
    *
    * Redistribution and use in source and binary forms, with or without
  @@ -15,7 +15,7 @@
    * are met:
    *
    * 1. Redistributions of source code must retain the above copyright
  - *    notice, this list of conditions and the following disclaimer. 
  + *    notice, this list of conditions and the following disclaimer.
    *
    * 2. Redistributions in binary form must reproduce the above copyright
    *    notice, this list of conditions and the following disclaimer in
  @@ -23,15 +23,15 @@
    *    distribution.
    *
    * 3. The end-user documentation included with the redistribution, if
  - *    any, must include the following acknowlegement:  
  - *       "This product includes software developed by the 
  + *    any, must include the following acknowlegement:
  + *       "This product includes software developed by the
    *        Apache Software Foundation (http://www.apache.org/)."
    *    Alternately, this acknowlegement may appear in the software itself,
    *    if and wherever such third-party acknowlegements normally appear.
    *
    * 4. The names "The Jakarta Project", "Tomcat", and "Apache Software
    *    Foundation" must not be used to endorse or promote products derived
  - *    from this software without prior written permission. For written 
  + *    from this software without prior written permission. For written
    *    permission, please contact apache@apache.org.
    *
    * 5. Products derived from this software may not be called "Apache"
  @@ -59,24 +59,33 @@
    *
    * [Additional notices, if required by prior licensing conditions]
    *
  - */ 
  + */
   
   package org.apache.slide.content;
   
   import java.io.*;
   import java.util.Date;
  +import java.util.List;
  +import java.util.ArrayList;
   import org.apache.slide.common.*;
   import org.apache.slide.util.Messages;
   
   /**
    * Encapsultes the contents of a revision.
  - * 
  + *
    * @author <a href="mailto:remm@apache.org">Remy Maucherat</a>
  - * @version $Revision: 1.4 $
  + * @author Juergen Pill
  + * @version $Revision: 1.5 $
    */
   public final class NodeRevisionContent implements Serializable {
       
       
  +    // -------------------------------------------------------------- Constants
  +    
  +    
  +    private static final int CHUNK = 1024*4;
  +    
  +    
       // ----------------------------------------------------- Instance Variables
       
       
  @@ -87,12 +96,6 @@
       
       
       /**
  -     * Binary content.
  -     */
  -    private byte[] binaryContent = null;
  -    
  -    
  -    /**
        * Reader.
        */
       private transient Reader reader = null;
  @@ -109,105 +112,113 @@
       
       /**
        * Content accessor.
  -     * 
  +     *
        * @return char[] Content
        */
       public char[] getContent() {
  -        // TODO : Use a StringBuffer to do that ...
  -        long contentLength = 1;
           char[] result = null;
           if (content != null) {
               result = content;
               inputStream = null;
  -        } else {
  -            if (reader != null) {
  -                inputStream = null;
  -                try {
  -                    // Limitation : this function can only handle 4Go of data,
  -                    // which should not be a problem.
  -                    // Use readContent to be able to read more.
  -                    result = new char[(int) contentLength];
  -                    int position = 0;
  -                    while (position < contentLength) {
  -                        int nChar = reader
  -                            .read(result, position, 
  -                                  (int) (contentLength - position));
  -                        position = position + nChar;
  -                    }
  -                } catch (IOException e) {
  -                    e.printStackTrace();
  -                    // Silent exception ?
  -                }
  -            }
  +            reader = null;
  +        };
  +        if (inputStream != null) {
  +            content = new String(readFromStream(inputStream)).toCharArray();
  +            result = content;
  +            inputStream = null;
  +            reader = null;
           }
  +        if (reader != null) {
  +            content = readFromReader(reader);
  +            result = content;
  +            inputStream = null;
  +            reader = null;
  +        }
           return result;
       }
       
       
  +    
       /**
        * Content accessor.
  -     * 
  +     *
        * @return Reader
        */
  -    public Reader readContent() 
  +    public Reader readContent()
           throws IOException {
           Reader result = null;
           if (content != null) {
               result = new CharArrayReader(content);
               inputStream = null;
  +            reader = null;
           }
           if (reader != null) {
               result = reader;
               inputStream = null;
           }
  +        if (inputStream != null) {
  +            result = new InputStreamReader(inputStream);
  +            reader = null;
  +        }
           return result;
       }
       
       
       /**
        * Content accessor.
  -     * 
  +     *
        * @return InputStream
        */
       public InputStream streamContent()
           throws IOException {
  +        InputStream result = null;
  +        if (content != null) {
  +            result = new StringBufferInputStream(new String(content));
  +            reader = null;
  +            inputStream = null;
  +        }
           if (inputStream != null) {
  +            result = inputStream;
               content = null;
               reader = null;
           }
  -        return inputStream;
  +        return result;
       }
       
       
       /**
        * Content mutator.
  -     * 
  +     *
        * @param content New content
        */
       public void setContent(char[] content) {
           this.content = content;
           this.reader = null;
  +        this.inputStream = null;
       }
       
       
       /**
        * Content mutator.
  -     * 
  +     *
        * @param reader New reader
        */
       public void setContent(Reader reader) {
           this.reader = reader;
  +        this.inputStream = null;
           this.content = null;
       }
       
       
       /**
        * Content mutator.
  -     * 
  +     *
        * @param inputStream New input stream
        */
       public void setContent(InputStream inputStream) {
           this.inputStream = inputStream;
  +        this.reader = null;
  +        this.content = null;
       }
       
       
  @@ -219,12 +230,102 @@
        */
       public void validate() {
           
  -        if ((content == null) && (binaryContent == null) && (reader == null)
  -            && (inputStream == null))
  +        if ((content == null) && (reader == null) && (inputStream == null))
               throw new ObjectValidationFailedException
                   (Messages.message
                    (NodeRevisionContent.class.getName() + ".noContent"));
           
  +    }
  +    
  +    
  +    // -------------------------------------------------------- Private Methods
  +    
  +    
  +    /**
  +     * Read the data from the stream and return the result in a byte array. 
  +     * Return null in case of an error.
  +     */
  +    private byte[] readFromStream(InputStream inputStream) {
  +        byte[] chunk;
  +        byte[] all;
  +        int chunkLen;
  +        int allLen;
  +        List chunks;
  +        int i;
  +        int max;
  +        int ofs;
  +        
  +        allLen = 0;
  +        try {
  +            chunk = new byte[CHUNK];
  +            chunkLen = inputStream.read(chunk);
  +            chunks = new ArrayList();
  +            while (chunkLen != -1) {
  +                chunks.add(new Integer(chunkLen));
  +                chunks.add(chunk);
  +                allLen += chunkLen;
  +                chunk = new byte[CHUNK];
  +                chunkLen = inputStream.read(chunk);
  +            }
  +        } catch (IOException e) {
  +            return null;
  +        }
  +
  +        all = new byte[allLen];
  +        ofs = 0;
  +        max = chunks.size();
  +        for (i = 0; i < max; i += 2) {
  +            chunkLen = ((Integer) chunks.get(i)).intValue();
  +            chunk = (byte[]) chunks.get(i + 1);
  +            System.arraycopy(chunk, 0, all, ofs, chunkLen);
  +            ofs += chunkLen;
  +        }
  +        return all;
  +    }
  +    
  +    
  +    
  +    
  +    /**
  +     * Read the data from the reader and return the result in a char array. 
  +     * Return null in case of an error.
  +     */
  +    private char[] readFromReader(Reader reader) {
  +        char[] chunk;
  +        char[] all;
  +        int chunkLen;
  +        int allLen;
  +        List chunks;
  +        int i;
  +        int max;
  +        int ofs;
  +        
  +        allLen = 0;
  +        try {
  +            chunk = new char[CHUNK];
  +            chunkLen = reader.read(chunk);
  +            chunks = new ArrayList();
  +            while (chunkLen != -1) {
  +                chunks.add(new Integer(chunkLen));
  +                chunks.add(chunk);
  +                allLen += chunkLen;
  +                chunk = new char[CHUNK];
  +                chunkLen = reader.read(chunk);
  +            }
  +        } catch (IOException e) {
  +            return null;
  +        }
  +        
  +        all = new char[allLen];
  +        ofs = 0;
  +        max = chunks.size();
  +        for (i = 0; i < max; i += 2) {
  +            chunkLen = ((Integer) chunks.get(i)).intValue();
  +            chunk = (char[]) chunks.get(i + 1);
  +            System.arraycopy(chunk, 0, all, ofs, chunkLen);
  +            ofs += chunkLen;
  +        }
  +        return all;
       }