Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * Licensed to the Apache Software Foundation (ASF) under one or more
   * contributor license agreements.  See the NOTICE file distributed with
   * this work for additional information regarding copyright ownership.
   * The ASF licenses this file to You under the Apache License, Version 2.0
   * (the "License"); you may not use this file except in compliance with
   * the License.  You may obtain a copy of the License at
   *
   *      http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.apache.tomcat.util.http.fileupload.util;
 
 import static org.jboss.web.FileUploadMessages.MESSAGES;
 
 
Utility class for working with streams.
 
 public final class Streams {
    
Private constructor, to prevent instantiation. This class has only static methods.
 
     private Streams() {
         // Does nothing
     }

    
 
     private static final int DEFAULT_BUFFER_SIZE = 8192;

    
Copies the contents of the given java.io.InputStream to the given java.io.OutputStream. Shortcut for
   copy(pInputStream, pOutputStream, new byte[8192]);
 

Parameters:
pInputStream The input stream, which is being read. It is guaranteed, that java.io.InputStream.close() is called on the stream.
pOutputStream The output stream, to which data should be written. May be null, in which case the input streams contents are simply discarded.
pClose True guarantees, that java.io.OutputStream.close() is called on the stream. False indicates, that only java.io.OutputStream.flush() should be called finally.
Returns:
Number of bytes, which have been copied.
Throws:
java.io.IOException An I/O error occurred.
 
     public static long copy(InputStream pInputStream,
             OutputStream pOutputStreamboolean pClose)
             throws IOException {
         return copy(pInputStreampOutputStreampClose,
                 new byte[]);
     }

    
Copies the contents of the given java.io.InputStream to the given java.io.OutputStream.

Parameters:
pIn The input stream, which is being read. It is guaranteed, that java.io.InputStream.close() is called on the stream.
pOut The output stream, to which data should be written. May be null, in which case the input streams contents are simply discarded.
pClose True guarantees, that java.io.OutputStream.close() is called on the stream. False indicates, that only java.io.OutputStream.flush() should be called finally.
pBuffer Temporary buffer, which is to be used for copying data.
Returns:
Number of bytes, which have been copied.
Throws:
java.io.IOException An I/O error occurred.
 
     public static long copy(InputStream pIn,
             OutputStream pOutboolean pClose,
             byte[] pBuffer)
     throws IOException {
         OutputStream out = pOut;
         InputStream in = pIn;
         try {
             long total = 0;
             for (;;) {
                 int res = in.read(pBuffer);
                 if (res == -1) {
                    break;
                }
                if (res > 0) {
                    total += res;
                    if (out != null) {
                        out.write(pBuffer, 0, res);
                    }
                }
            }
            if (out != null) {
                if (pClose) {
                    out.close();
                } else {
                    out.flush();
                }
                out = null;
            }
            in.close();
            in = null;
            return total;
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (Throwable t) {
                    /* Ignore me */
                }
            }
            if (pClose  &&  out != null) {
                try {
                    out.close();
                } catch (Throwable t) {
                    /* Ignore me */
                }
            }
        }
    }

    
This convenience method allows to read a org.apache.tomcat.util.http.fileupload.FileItemStream's content into a string. The platform's default character encoding is used for converting bytes into characters.

Parameters:
pStream The input stream to read.
Returns:
The streams contents, as a string.
Throws:
java.io.IOException An I/O error occurred.
See also:
asString(java.io.InputStream,java.lang.String)
    public static String asString(InputStream pStreamthrows IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        copy(pStreambaostrue);
        return baos.toString();
    }

    
This convenience method allows to read a org.apache.tomcat.util.http.fileupload.FileItemStream's content into a string, using the given character encoding.

Parameters:
pStream The input stream to read.
pEncoding The character encoding, typically "UTF-8".
Returns:
The streams contents, as a string.
Throws:
java.io.IOException An I/O error occurred.
See also:
asString(java.io.InputStream)
    public static String asString(InputStream pStreamString pEncoding)
            throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        copy(pStreambaostrue);
        return baos.toString(pEncoding);
    }

    
Checks, whether the given file name is valid in the sense, that it doesn't contain any NUL characters. If the file name is valid, it will be returned without any modifications. Otherwise, an org.apache.tomcat.util.http.fileupload.InvalidFileNameException is raised.

Parameters:
pFileName The file name to check
Returns:
Unmodified file name, if valid.
Throws:
org.apache.tomcat.util.http.fileupload.InvalidFileNameException The file name was found to be invalid.
    public static String checkFileName(String pFileName) {
        if (pFileName != null  &&  pFileName.indexOf('\u0000') != -1) {
            // pFileName.replace("\u0000", "\\0")
            final StringBuffer sb = new StringBuffer();
            for (int i = 0;  i < pFileName.length();  i++) {
                char c = pFileName.charAt(i);
                switch (c) {
                    case 0:
                        sb.append("\\0");
                        break;
                    default:
                        sb.append(c);
                        break;
                }
            }
            throw new InvalidFileNameException(pFileName,
                    .invalidFileName(sb.toString()));
        }
        return pFileName;
    }
New to GrepCode? Check out our FAQ X