Example usage for java.io InputStreamReader read

List of usage examples for java.io InputStreamReader read

Introduction

In this page you can find the example usage for java.io InputStreamReader read.

Prototype

public int read(java.nio.CharBuffer target) throws IOException 

Source Link

Document

Attempts to read characters into the specified character buffer.

Usage

From source file:android_network.hetnet.vpn_service.Util.java

public static StringBuilder readString(InputStreamReader reader) {
    StringBuilder sb = new StringBuilder(2048);
    char[] read = new char[128];
    try {// ww w.j ava  2  s .c  om
        for (int i; (i = reader.read(read)) >= 0; sb.append(read, 0, i))
            ;
    } catch (Throwable ex) {
        Log.e(TAG, ex.toString() + "\n" + Log.getStackTraceString(ex));
    }
    return sb;
}

From source file:edu.stanford.epad.plugins.qifpwrapper.QIFPHandler.java

private static EPADSessionResponse getEPADSessionID(String username, String password, String epadHost,
        int port) {
    String epadSessionURL = buildEPADSessionURL(epadHost, port);
    HttpClient client = new HttpClient();
    PostMethod method = new PostMethod(epadSessionURL);
    String authString = buildAuthorizationString(username, password);
    EPADSessionResponse epadSessionResponse;
    int epadStatusCode;

    try {//from  w  ww  .  j  ava  2 s .c  o m
        log.info("Invoking EPAD session service for user " + username + " at " + epadSessionURL);
        method.setRequestHeader("Authorization", "Basic " + authString);
        epadStatusCode = client.executeMethod(method);
        log.info("Successfully invoked EPAD session service for user " + username + "; status code = "
                + epadStatusCode);
    } catch (IOException e) {
        log.warning("Error calling EPAD session service for user " + username, e);
        epadStatusCode = HttpServletResponse.SC_INTERNAL_SERVER_ERROR;
    }

    try {
        if (epadStatusCode == HttpServletResponse.SC_OK || epadStatusCode == HttpServletResponse.SC_FOUND) {
            try {
                StringBuilder sb = new StringBuilder();
                InputStreamReader isr = null;
                try {
                    isr = new InputStreamReader(method.getResponseBodyAsStream());
                    int read = 0;
                    char[] chars = new char[128];
                    while ((read = isr.read(chars)) > 0) {
                        sb.append(chars, 0, read);
                    }
                } finally {
                    IOUtils.closeQuietly(isr);
                }
                String jsessionID = sb.toString();
                epadSessionResponse = new EPADSessionResponse(HttpServletResponse.SC_OK, jsessionID, "");
                log.debug("Session ID " + jsessionID + " generated for user " + username); // TODO temp
            } catch (IOException e) {
                log.warning(LOGIN_EXCEPTION_MESSAGE, e);
                epadSessionResponse = new EPADSessionResponse(HttpServletResponse.SC_INTERNAL_SERVER_ERROR,
                        null, LOGIN_EXCEPTION_MESSAGE + ": " + e.getMessage());
            }
        } else if (epadStatusCode == HttpServletResponse.SC_UNAUTHORIZED) {
            log.warning(EPAD_UNAUTHORIZED_MESSAGE);
            epadSessionResponse = new EPADSessionResponse(epadStatusCode, null, EPAD_UNAUTHORIZED_MESSAGE);
        } else {
            log.warning(EPAD_LOGIN_ERROR_MESSAGE + "; EPAD status code = " + epadStatusCode);
            epadSessionResponse = new EPADSessionResponse(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, null,
                    EPAD_LOGIN_ERROR_MESSAGE + "; EPAD status code = " + epadStatusCode);
        }
    } finally {
        method.releaseConnection();
    }
    return epadSessionResponse;
}

From source file:org.alfresco.encoding.BomCharactersetFinder.java

/**
 * Just searches the Byte Order Marker, i.e. the first three characters for a sign of
 * the encoding./*from w  ww  . j  a va  2s  . c o m*/
 */
protected Charset detectCharsetImpl(byte[] buffer) throws Exception {
    Charset charset = null;
    ByteArrayInputStream bis = null;
    try {
        bis = new ByteArrayInputStream(buffer);
        bis.mark(3);
        char[] byteHeader = new char[3];
        InputStreamReader in = new InputStreamReader(bis);
        int bytesRead = in.read(byteHeader);
        bis.reset();

        if (bytesRead < 2) {
            // ASCII
            charset = Charset.forName("Cp1252");
        } else if (byteHeader[0] == 0xFE && byteHeader[1] == 0xFF) {
            // UCS-2 Big Endian
            charset = Charset.forName("UTF-16BE");
        } else if (byteHeader[0] == 0xFF && byteHeader[1] == 0xFE) {
            // UCS-2 Little Endian
            charset = Charset.forName("UTF-16LE");
        } else if (bytesRead >= 3 && byteHeader[0] == 0xEF && byteHeader[1] == 0xBB && byteHeader[2] == 0xBF) {
            // UTF-8
            charset = Charset.forName("UTF-8");
        } else {
            // No idea
            charset = null;
        }
        // Done
        return charset;
    } finally {
        if (bis != null) {
            try {
                bis.close();
            } catch (Throwable e) {
            }
        }
    }
}

From source file:com.metawiring.load.generators.LineExtractGenerator.java

private void loadLines(String filename) {

    InputStream stream = LineExtractGenerator.class.getClassLoader().getResourceAsStream(filename);
    if (stream == null) {
        throw new RuntimeException(filename + " was missing.");
    }/*from   www.  ja  v  a 2s  .c  om*/

    CharBuffer linesImage;
    try {
        InputStreamReader isr = new InputStreamReader(stream);
        linesImage = CharBuffer.allocate(1024 * 1024);
        isr.read(linesImage);
        isr.close();
    } catch (IOException e) {
        logger.error(e.getMessage());
        throw new RuntimeException(e);
    }
    linesImage.flip();
    Collections.addAll(lines, linesImage.toString().split("\n"));
}

From source file:com.metawiring.load.generators.LoremExtractGenerator.java

private CharBuffer loadLoremIpsum() {
    InputStream stream = LoremExtractGenerator.class.getClassLoader()
            .getResourceAsStream("data/lorem_ipsum_full.txt");
    if (stream == null) {
        throw new RuntimeException("lorem_ipsum_full.txt was missing.");
    }/*ww w  .j  ava  2  s .c  om*/

    CharBuffer image;
    try {
        InputStreamReader isr = new InputStreamReader(stream);
        image = CharBuffer.allocate(1024 * 1024);
        isr.read(image);
        isr.close();
    } catch (IOException e) {
        logger.error(e.getMessage());
        throw new RuntimeException(e);
    }
    image.flip();

    return image.asReadOnlyBuffer();

}

From source file:com.easarrive.aws.client.cloudsearch.AmazonCloudSearchClient.java

private String inputStreamToString(InputStream in) throws IOException {
    StringWriter output = new StringWriter();
    InputStreamReader input = new InputStreamReader(in);
    char[] buffer = new char[1024 * 4];
    int n = 0;//from   w  w  w .  j  av a2 s. co  m
    while (-1 != (n = input.read(buffer))) {
        output.write(buffer, 0, n);
    }
    return output.toString();
}

From source file:com.metawiring.load.generators.ExtractGenerator.java

private CharBuffer loadFileData() {
    InputStream stream = null;/*from  www  .ja  v a 2  s .c  om*/
    File onFileSystem = new File("data" + File.separator + fileName);

    if (onFileSystem.exists()) {
        try {
            stream = new FileInputStream(onFileSystem);
        } catch (FileNotFoundException e) {
            throw new RuntimeException(
                    "Unable to find file " + onFileSystem.getPath() + " after verifying that it exists.");
        }
        logger.debug("Loaded file data from " + onFileSystem.getPath());
    }

    if (stream == null) {
        stream = Thread.currentThread().getContextClassLoader().getResourceAsStream("data/" + fileName);
        logger.debug("Loaded file data from classpath resource " + fileName);
    }

    if (stream == null) {
        throw new RuntimeException(fileName + " was missing.");
    }

    CharBuffer image;
    try {
        InputStreamReader isr = new InputStreamReader(stream);
        image = CharBuffer.allocate(1024 * 1024);
        isr.read(image);
        isr.close();
    } catch (IOException e) {
        logger.error(e.getMessage());
        throw new RuntimeException(e);
    }
    image.flip();

    return image.asReadOnlyBuffer();

}

From source file:org.apache.struts.webapp.validator.ShowFileAction.java

public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
        HttpServletResponse response) throws Exception {

    // Get the file name
    String fileName = mapping.getParameter();
    StringBuffer fileContents = new StringBuffer();

    if (fileName != null) {

        InputStream input = servlet.getServletContext().getResourceAsStream(fileName);
        if (input == null) {
            log.warn("File Not Found: " + fileName);
        } else {/*from ww  w  . j  av a 2 s .c o  m*/
            InputStreamReader inputReader = new InputStreamReader(input);
            char[] buffer = new char[1000];
            while (true) {
                int lth = inputReader.read(buffer);
                if (lth < 0) {
                    break;
                } else {
                    fileContents.append(buffer, 0, lth);
                }
            }
        }
    } else {
        log.error("No file name specified.");
    }

    // Store the File contents and name in the Request
    request.setAttribute("fileName", fileName);
    request.setAttribute("fileContents", fileContents.toString());

    return mapping.findForward("success");
}

From source file:com.mobilis.android.nfc.activities.MagTekFragment.java

public static String ReadSettings(Context context, String file) throws IOException {
    FileInputStream fis = null;/*from   w  ww  .  j a va 2 s . c om*/
    InputStreamReader isr = null;
    String data = null;
    fis = context.openFileInput(file);
    isr = new InputStreamReader(fis);
    char[] inputBuffer = new char[fis.available()];
    isr.read(inputBuffer);
    data = new String(inputBuffer);
    isr.close();
    fis.close();
    return data;
}

From source file:org.paxle.core.doc.impl.AParserDocumentTest.java

protected void appendData(File source, IParserDocument target) throws IOException {
    final StringBuilder sourceText = new StringBuilder();

    // writing Data
    final CharBuffer buffer = CharBuffer.allocate(50);
    final InputStreamReader sourceReader = new InputStreamReader(new FileInputStream(source),
            Charset.forName("UTF-8"));
    while (sourceReader.read(buffer) != -1) {
        buffer.flip();// w  w w.ja  v a  2 s  .com
        sourceText.append(buffer);
        target.append(buffer.toString());
        buffer.clear();
    }

    sourceReader.close();
    target.flush();
}