Java Code Examples for java.io.InputStream

The following are top voted examples for showing how to use java.io.InputStream. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to product more good examples.

Example 1
Project: arduino-android   File: Template.java View source code 9 votes vote down vote up
private String readTextFile(InputStream inputStream) 
 {
 	ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
 	byte buf[] = new byte[1024];
 	int len;
 	try 
 	{
 		while ((len = inputStream.read(buf)) != -1) 
 		{
 	       outputStream.write(buf, 0, len);
 	    }
 	    outputStream.close();
 	    inputStream.close();
 	} 
 	catch (IOException e) 
 	{
 	}
 	return outputStream.toString();
}
 
Example 2
Project: jentrata-msh   File: PartialInputStream.java View source code 7 votes vote down vote up
/**
 * Constructor for the PartialInputStream object
 *
 * @param in the origninal input stream
 * @param offset the offset the read data from the stream.
 * @param length the length of the data to be read
 * @exception IOException Description of the Exception
 */
PartialInputStream(InputStream in, long offset, long length)
         throws IOException {
    this.in = in;
    this.length = length;
    long skip = in.skip(offset);
    long actualSkip = skip;
    while (actualSkip >= 0 && skip < offset) {
        actualSkip = in.skip(offset - skip);
        if (actualSkip > 0) {
            skip += actualSkip;
        }
    }
    currentRead = 0;
}
 
Example 3
Project: Jorum-DSpace   File: XMLManifest.java View source code 7 votes vote down vote up
/**
 * Create a new manifest object from a serialized manifest XML document.
 * Parse document read from the input stream, optionally validating.
 * NOTE: This method attempts to close the InputStream, is
 * @param is input stream containing serialized XML
 * @param validate if true, enable XML validation using schemas
 *   in document.  Also validates any sub-documents.
 * @throws MetadataValidationException if there is any error parsing
 *          or validating the METS.
 * @return new Document object.
 */
public static Document parseManifest(InputStream is, boolean validate) throws IOException,
		MetadataValidationException {
	SAXBuilder builder = new SAXBuilder(validate);

	// Set the SAX parser to expand entity references so no need to check in value strings
	builder.setExpandEntities(true);

	// Set validation feature
	if (validate) {
		builder.setFeature("http://apache.org/xml/features/validation/schema", true);
	}
      
	log.debug("Mainfest XML validation set to: " + validate);

	// Tell the parser where local copies of schemas are, to speed up
	// validation.  Local XSDs are identified in the configuration file.
	if (localSchemas.length() > 0)
		builder.setProperty("http://apache.org/xml/properties/schema/external-schemaLocation", localSchemas);

	// Parse the manifest file
	Document manifestDocument;
	try {
		manifestDocument = builder.build(is);

		// XXX for temporary debugging

		XMLOutputter outputPretty = new XMLOutputter(Format.getPrettyFormat());
		log.debug("Got IMS DOCUMENT:");
		//            log.debug(outputPretty.outputString(manifestDocument));

	} catch (JDOMException je) {
		throw new MetadataValidationException("Error validating IMS manifest in " + is.toString(), je);
	} finally {
		if (is != null) {
			try {
				is.close();
			} catch (Exception e) {
				e.printStackTrace();
				ExceptionLogger.logException(log, e);
			}
		}
	}

	return manifestDocument;
}
 
Example 4
Project: jentrata-msh   File: IOHandler.java View source code 6 votes vote down vote up
/**
 * Reads an array of bytes from an input stream.
 * 
 * @param ins the input stream to be read.
 * @return an array of bytes read from the specified input stream.
 * @throws IOException if there is IO error occurred during the operation.
 */
public static byte[] readBytes(InputStream ins) throws IOException {
    ByteArrayOutputStream out = new ByteArrayOutputStream(ins.available());
    pipe(ins, out);
    byte[] bytes = out.toByteArray();
    out.close();
    return bytes;
}
 
Example 5
Project: cassandra-dev   File: HttpStartLineParser.java View source code 6 votes vote down vote up
public boolean onMoreBytes(InputStream in) throws HttpParsingException, IOException
{
    int got;

    if (parseState_ == StartLineParseState.TO_RESET)
    {
        resetParserState();
    }

    while (in.available() > 0)
    {
        in.mark(1);
        got = in.read();
			
        switch (parseState_)
        {
            case EATING_WHITESPACE:
                    switch (got)
                    {
                        case ' ':
                                    break;
                        default:
                                    in.reset();
                                    parseState_ = nextState_;
                                    break;
                    }
                    break;
                
            case READING_METHOD:
                    switch (got)
                    {
                        case ' ':
                                parseState_ = StartLineParseState.EATING_WHITESPACE;
                                nextState_ = StartLineParseState.READING_PATH;
                                break;
                        default:
                                httpMethod_.append((char) got);
                                break;
                    }
                    break;
                    
            case READING_PATH:
                    switch (got)
                    {
                        case '\r':
                                parseState_ = StartLineParseState.CHECKING_EOL;
                                break;
                        case '%':
                                encodeTo_ = httpPath_;
                                nextState_ = parseState_;
                                parseState_ = StartLineParseState.DECODING_FIRST_CHAR;
                                break;
                        case ' ':
                                parseState_ = StartLineParseState.EATING_WHITESPACE;
                                nextState_ = StartLineParseState.READING_VERSION;
                                break;
                        case '?':
                                parseState_ = StartLineParseState.READING_QUERY;
                                break;
                        default:
                                httpPath_.append((char) got);
                                break;
                    }
                    break;
                        
            case READING_QUERY:
                    switch (got)
                    {
                        case '\r':
                                parseState_ = StartLineParseState.CHECKING_EOL;
                                break;
                        case '%':
                                encodeTo_ = httpQuery_;
                                nextState_ = parseState_;
                                parseState_ = StartLineParseState.DECODING_FIRST_CHAR;
                                break;
                        case ' ':
                                parseState_ = StartLineParseState.EATING_WHITESPACE;
                                nextState_ = StartLineParseState.READING_VERSION;
                                break;
                        case '+':
                                httpQuery_.append(' ');
                                break;
                        default:
                                httpQuery_.append((char) got);
                                break;
                    }
                    break;
                        
            case DECODING_FIRST_CHAR:
                    encodedValue_ = decodeHex(got) * 16;
                    parseState_ = StartLineParseState.DECODING_SECOND_CHAR;
                    break;

            case DECODING_SECOND_CHAR:
                    encodeTo_.append((char) (decodeHex(got) + encodedValue_));
                    parseState_ = nextState_;
                    break;
                            
            case READING_VERSION:
                    switch (got)
                    {
                        case '\r':
                                parseState_ = StartLineParseState.CHECKING_EOL;
                                break;
                        default:
                                httpVersion_.append((char) got);
                                break;
                    }
                    break;
                    
            case CHECKING_EOL:
                    switch (got)
                    {
                        case '\n':
                                finishLine_();
                                parseState_ = StartLineParseState.TO_RESET;
                                return true;
                        default:
                                throw new HttpParsingException();
                    }
                    
            default:
                    throw new HttpParsingException();
        }
    }
    
    return false;
}
 
Example 6
Project: hdt-java   File: DictionarySectionFactory.java View source code 6 votes vote down vote up
public static DictionarySectionPrivate loadFrom(InputStream input, ProgressListener listener) throws IOException {
	if(!input.markSupported()) {
		throw new IllegalArgumentException("Need support for mark()/reset(). Please wrap the InputStream with a BufferedInputStream");
	}
	input.mark(64);
	int dictType = input.read();
	input.reset();
	input.mark(64);		// To allow children to reset() and try another instance.
	
	DictionarySectionPrivate section=null;
	
	switch(dictType) {
	case PFCDictionarySection.TYPE_INDEX:
		try{
			// First try load using the standard PFC 
			section = new PFCDictionarySection(new HDTSpecification());
			section.load(input, listener);
		} catch (IllegalArgumentException e) {
			// The PFC Could not load the file because it is too big, use PFCBig
			section = new PFCDictionarySectionBig(new HDTSpecification());
			section.load(input, listener);
		}
		return section;
	}
	throw new IOException("DictionarySection implementation not available for id "+dictType);
}
 
Example 7
Project: openbel-framework   File: PasswordPrompter.java View source code 6 votes vote down vote up
/**
 * Retrieve a password in the [email protected] InputStream} with an optional
 * [email protected] prompt} message if reading from [email protected] System#in}.
 *
 * @param in [email protected] InputStream}, the input stream to retrieve the
 * password from, which cannot be null
 * @param prompt [email protected] String}, the message to prompt, which will
 * only be used if [email protected] InputStream} equals [email protected] System#in}.
 * @return [email protected] char[]}, the password characters
 * @throws IOException - Thrown if an io error occurred reading the
 * password from the [email protected] InputStream}.
 */
public static final char[] getPassword(InputStream in, String prompt)
        throws IOException {
    if (in == null) {
        throw new InvalidArgument("in is null");
    }

    char[] pwd;
    if (in.equals(System.in)) {
        System.out.print(prompt);

        Console console = System.console();

        if (console == null) {
            throw new IOException(
                    "Cannot obtain the console with which to read the password from.");
        }

        pwd = console.readPassword();
    } else {
        BufferedReader pwdReader =
                new BufferedReader(new InputStreamReader(in));
        String buffer = pwdReader.readLine();
        if (buffer == null) return null;
        pwd = buffer.toCharArray();
        pwdReader.close();
    }

    return pwd;
}