Java/File Input Output/StringReader — различия между версиями
Admin (обсуждение | вклад) м (1 версия) |
|
(нет различий)
|
Текущая версия на 06:02, 1 июня 2010
Содержание
Read InputStream to string
/*
* 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.
*/
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
/** Utility class for working with streams.
*/
public final class Streams {
/**
* This convenience method allows to read a
* {@link org.apache.rumons.fileupload.FileItemStream}"s
* content into a string. The platform"s default character encoding
* is used for converting bytes into characters.
* @param pStream The input stream to read.
* @see #asString(InputStream, String)
* @return The streams contents, as a string.
* @throws IOException An I/O error occurred.
*/
public static String asString(InputStream pStream) throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
copy(pStream, baos, true);
return baos.toString();
}
/**
* This convenience method allows to read a
* {@link org.apache.rumons.fileupload.FileItemStream}"s
* content into a string, using the given character encoding.
* @param pStream The input stream to read.
* @param pEncoding The character encoding, typically "UTF-8".
* @see #asString(InputStream)
* @return The streams contents, as a string.
* @throws IOException An I/O error occurred.
*/
public static String asString(InputStream pStream, String pEncoding)
throws IOException {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
copy(pStream, baos, true);
return baos.toString(pEncoding);
}
/**
* Default buffer size for use in
* {@link #copy(InputStream, OutputStream, boolean)}.
*/
private static final int DEFAULT_BUFFER_SIZE = 8192;
/**
* Copies the contents of the given {@link InputStream}
* to the given {@link OutputStream}. Shortcut for
* <pre>
* copy(pInputStream, pOutputStream, new byte[8192]);
* </pre>
* @param pInputStream The input stream, which is being read.
* It is guaranteed, that {@link InputStream#close()} is called
* on the stream.
* @param pOutputStream The output stream, to which data should
* be written. May be null, in which case the input streams
* contents are simply discarded.
* @param pClose True guarantees, that {@link OutputStream#close()}
* is called on the stream. False indicates, that only
* {@link OutputStream#flush()} should be called finally.
*
* @return Number of bytes, which have been copied.
* @throws IOException An I/O error occurred.
*/
public static long copy(InputStream pInputStream,
OutputStream pOutputStream, boolean pClose)
throws IOException {
return copy(pInputStream, pOutputStream, pClose,
new byte[DEFAULT_BUFFER_SIZE]);
}
/**
* Copies the contents of the given {@link InputStream}
* to the given {@link OutputStream}.
* @param pIn The input stream, which is being read.
* It is guaranteed, that {@link InputStream#close()} is called
* on the stream.
* @param pOut The output stream, to which data should
* be written. May be null, in which case the input streams
* contents are simply discarded.
* @param pClose True guarantees, that {@link OutputStream#close()}
* is called on the stream. False indicates, that only
* {@link OutputStream#flush()} should be called finally.
* @param pBuffer Temporary buffer, which is to be used for
* copying data.
* @return Number of bytes, which have been copied.
* @throws IOException An I/O error occurred.
*/
public static long copy(InputStream pIn,
OutputStream pOut, boolean 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 */
}
}
}
}
}
Reads data off a stream, printing every byte read to System.err
/****************************************************************
* 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. *
****************************************************************/
import java.io.IOException;
import java.io.InputStream;
/**
* Reads data off a stream, printing every byte read to System.err.
*/
public class DebugInputStream extends InputStream {
/**
* The input stream being wrapped
*/
InputStream in = null;
/**
* Constructor that takes an InputStream to be wrapped.
*
* @param in the InputStream to be wrapped
*/
public DebugInputStream(InputStream in) {
this.in = in;
}
/**
* Read a byte off the stream
*
* @return the byte read off the stream
* @throws IOException if an exception is encountered when reading
*/
public int read() throws IOException {
int b = in.read();
System.err.write(b);
return b;
}
/**
* Close the stream
*
* @throws IOException if an exception is encountered when closing
*/
public void close() throws IOException {
in.close();
}
}
Reads file contents
/**
*
* The ObjectStyle Group Software License, version 1.1
* ObjectStyle Group - http://objectstyle.org/
*
* Copyright (c) 2002-2005, Andrei (Andrus) Adamchik and individual authors
* of the software. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* 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
* the documentation and/or other materials provided with the
* distribution.
*
* 3. The end-user documentation included with the redistribution, if any,
* must include the following acknowlegement:
* "This product includes software developed by independent contributors
* and hosted on ObjectStyle Group web site (http://objectstyle.org/)."
* Alternately, this acknowlegement may appear in the software itself,
* if and wherever such third-party acknowlegements normally appear.
*
* 4. The names "ObjectStyle Group" and "Cayenne" must not be used to endorse
* or promote products derived from this software without prior written
* permission. For written permission, email
* "andrus at objectstyle dot org".
*
* 5. Products derived from this software may not be called "ObjectStyle"
* or "Cayenne", nor may "ObjectStyle" or "Cayenne" appear in their
* names without prior written permission.
*
* THIS SOFTWARE IS PROVIDED ``AS IS"" AND ANY EXPRESSED OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE OBJECTSTYLE GROUP OR
* ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*
*
* This software consists of voluntary contributions made by many
* individuals and hosted on ObjectStyle Group web site. For more
* information on the ObjectStyle Group, please see
* <http://objectstyle.org/>.
*/
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.lang.reflect.Member;
import java.lang.reflect.Modifier;
import java.net.URL;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.ruparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
/**
* Contains various unorganized static utility methods used across Cayenne.
*
* @author Andrei Adamchik
*/
public class Util {
/**
* Reads file contents, returning it as a String, using System default line separator.
*/
public static String stringFromFile(File file) throws IOException {
return stringFromFile(file, System.getProperty("line.separator"));
}
/**
* Reads file contents, returning it as a String, joining lines with provided
* separator.
*/
public static String stringFromFile(File file, String joinWith) throws IOException {
StringBuffer buf = new StringBuffer();
BufferedReader in = new BufferedReader(new FileReader(file));
try {
String line = null;
while ((line = in.readLine()) != null) {
buf.append(line).append(joinWith);
}
}
finally {
in.close();
}
return buf.toString();
}
}
Reads from an underlying InputStream up to a defined number of bytes or the end of the underlying stream
/*
* 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.
*/
/* $Id: SubInputStream.java 604883 2007-12-17 14:36:37Z jeremias $ */
import java.io.FilterInputStream;
import java.io.IOException;
import java.io.InputStream;
/**
* This class is a FilterInputStream descendant that reads from an underlying InputStream
* up to a defined number of bytes or the end of the underlying stream. Closing this InputStream
* will not result in the underlying InputStream to be closed, too.
* <p>
* This InputStream can be used to read chunks from a larger file of which the length is
* known in advance.
*/
public class SubInputStream extends FilterInputStream {
/** Indicates the number of bytes remaining to be read from the underlying InputStream. */
private long bytesToRead;
/**
* Indicates whether the underlying stream should be closed when the {@link #close()} method
* is called.
*/
private boolean closeUnderlying = false;
/**
* Creates a new SubInputStream.
* @param in the InputStream to read from
* @param maxLen the maximum number of bytes to read from the underlying InputStream until
* the end-of-file is signalled.
* @param closeUnderlying true if the underlying stream should be closed when the
* {@link #close()} method is called.
*/
public SubInputStream(InputStream in, long maxLen, boolean closeUnderlying) {
super(in);
this.bytesToRead = maxLen;
this.closeUnderlying = closeUnderlying;
}
/**
* Creates a new SubInputStream. The underlying stream is not closed, when close() is called.
* @param in the InputStream to read from
* @param maxLen the maximum number of bytes to read from the underlying InputStream until
* the end-of-file is signalled.
*/
public SubInputStream(InputStream in, long maxLen) {
this(in, maxLen, false);
}
/** {@inheritDoc} */
public int read() throws IOException {
if (bytesToRead > 0) {
int result = super.read();
if (result >= 0) {
bytesToRead--;
return result;
} else {
return -1;
}
} else {
return -1;
}
}
/** {@inheritDoc} */
public int read(byte[] b, int off, int len) throws IOException {
if (bytesToRead == 0) {
return -1;
}
int effRead = (int)Math.min(bytesToRead, len);
//cast to int is safe because len can never be bigger than Integer.MAX_VALUE
int result = super.read(b, off, effRead);
if (result >= 0) {
bytesToRead -= result;
}
return result;
}
/** {@inheritDoc} */
public long skip(long n) throws IOException {
long effRead = Math.min(bytesToRead, n);
long result = super.skip(effRead);
bytesToRead -= result;
return result;
}
/** {@inheritDoc} */
public void close() throws IOException {
this.bytesToRead = 0;
if (this.closeUnderlying) {
super.close();
}
}
}
//////////////////////////////////
/*
* 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.
*/
/* $Id: SubInputStreamTestCase.java 604883 2007-12-17 14:36:37Z jeremias $ */
package org.apache.xmlgraphics.util.io;
import java.io.ByteArrayInputStream;
import java.util.Arrays;
import junit.framework.TestCase;
/**
* Test case for SubInputStream.
*/
public class SubInputStreamTestCase extends TestCase {
/**
* Main constructor.
* @param name the test case"s name
* @see junit.framework.TestCase#TestCase(String)
*/
public SubInputStreamTestCase(String name) {
super(name);
}
/**
* Tests SubInputStream.
* @throws Exception if an error occurs
*/
public void testMain() throws Exception {
//Initialize test data
byte[] data = new byte[256];
for (int i = 0; i < data.length; i++) {
data[i] = (byte)(i & 0xff);
}
int v, c;
byte[] buf;
String s;
SubInputStream subin = new SubInputStream(new ByteArrayInputStream(data), 10);
v = subin.read();
assertEquals(0, v);
v = subin.read();
assertEquals(1, v);
buf = new byte[4];
c = subin.read(buf);
assertEquals(4, c);
s = new String(buf, "US-ASCII");
assertEquals("\u0002\u0003\u0004\u0005", s);
Arrays.fill(buf, (byte)0);
c = subin.read(buf, 2, 2);
assertEquals(2, c);
s = new String(buf, "US-ASCII");
assertEquals("\u0000\u0000\u0006\u0007", s);
Arrays.fill(buf, (byte)0);
c = subin.read(buf);
assertEquals(2, c);
s = new String(buf, "US-ASCII");
assertEquals("\u0008\u0009\u0000\u0000", s);
}
}
Read string from InputStream and Reader
/*
* $RCSfile: StringIO.java,v $
*
* Copyright (c) 2007 Sun Microsystems, Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistribution of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Redistribution in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
*
* Neither the name of Sun Microsystems, Inc. or the names of
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* This software is provided "AS IS," without a warranty of any
* kind. ALL EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND
* WARRANTIES, INCLUDING ANY IMPLIED WARRANTY OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT, ARE HEREBY
* EXCLUDED. SUN MICROSYSTEMS, INC. ("SUN") AND ITS LICENSORS SHALL
* NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE AS A RESULT OF
* USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS
* DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR
* ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL,
* CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND
* REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR
* INABILITY TO USE THIS SOFTWARE, EVEN IF SUN HAS BEEN ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGES.
*
* You acknowledge that this software is not designed, licensed or
* intended for use in the design, construction, operation or
* maintenance of any nuclear facility.
*
* $Revision: 1.4 $
* $Date: 2007/02/09 17:20:42 $
* $State: Exp $
*/
import java.io.IOException;
import java.io.File;
import java.io.FileReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URL;
/**
* Utility class with static methods to read the entire contents of a
* file, URL, InputStream, or Reader into a single String that is
* returned to the user.
*
* @since Java 3D 1.4
*/
public class StringIO {
/**
* Read the entire contents of the specified file and return a
* single String object containing the contents of the file.
*
* @param fileName the name of the file from which to read
*
* @return a String containing the contents of the input file
*
* @throws IOException if the specified file cannot be opened, or
* if an I/O error occurs while reading the file
*/
public static String readFully(String fileName) throws IOException {
return readFully(new File(fileName));
}
/**
* Read the entire contents of the specified file and return a
* single String object containing the contents of the file.
* This method does not return until the end of the input file
* is reached.
*
* @param file a File from which to read
*
* @return a String containing the contents of the input file
*
* @throws IOException if the specified file cannot be opened, or
* if an I/O error occurs while reading the file
*/
public static String readFully(File file) throws IOException {
return readFully(new FileReader(file));
}
/**
* Read the entire contents of the specified URL and return a
* single String object containing the contents of the URL.
* This method does not return until an end of stream is reached
* for the URL.
*
* @param url a URL from which to read
*
* @return a String containing the contents of the input URL
*
* @throws IOException if the specified URL cannot be opened, or
* if an I/O error occurs while reading the URL
*/
public static String readFully(URL url) throws IOException {
return readFully(url.openStream());
}
/**
* Read the entire contents of the specified InputStream and return a
* single String object containing the contents of the InputStream.
* This method does not return until the end of the input
* stream is reached.
*
* @param stream an InputStream from which to read
*
* @return a String containing the contents of the input stream
*
* @throws IOException if an I/O error occurs while reading the input stream
*/
public static String readFully(InputStream stream) throws IOException {
return readFully(new InputStreamReader(stream));
}
/**
* Read the entire contents of the specified Reader and return a
* single String object containing the contents of the InputStream.
* This method does not return until the end of the input file or
* stream is reached.
*
* @param reader a Reader from which to read
*
* @return a String containing the contents of the stream
*
* @throws IOException if an I/O error occurs while reading the input stream
*/
public static String readFully(Reader reader) throws IOException {
char[] arr = new char[8*1024]; // 8K at a time
StringBuffer buf = new StringBuffer();
int numChars;
while ((numChars = reader.read(arr, 0, arr.length)) > 0) {
buf.append(arr, 0, numChars);
}
return buf.toString();
}
/**
* Do not construct an instance of this class.
*/
private StringIO() {
}
}
Using the StringReader class
import java.io.StreamTokenizer;
import java.io.StringReader;
public class Main {
public static void main(String[] args) throws Exception{
StringReader reader = new StringReader("this is a test");
int wordCount = 0;
StreamTokenizer streamTokenizer = new StreamTokenizer(reader);
while (streamTokenizer.nextToken() != StreamTokenizer.TT_EOF) {
if (streamTokenizer.ttype == StreamTokenizer.TT_WORD)
wordCount++;
}
System.out.println("Number of words in file: " + wordCount);
}
}
//Number of words in file: 4