當前位置: 首頁>>代碼示例>>Java>>正文


Java InputStreamReader.read方法代碼示例

本文整理匯總了Java中java.io.InputStreamReader.read方法的典型用法代碼示例。如果您正苦於以下問題:Java InputStreamReader.read方法的具體用法?Java InputStreamReader.read怎麽用?Java InputStreamReader.read使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在java.io.InputStreamReader的用法示例。


在下文中一共展示了InputStreamReader.read方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: getStringFromInputStream

import java.io.InputStreamReader; //導入方法依賴的package包/類
/**
 * Convert an InputStream in a String
 *
 * @param inputStream InputStream to convert
 *
 * @return String with the content of InputStream
 *
 * @throws UnsupportedEncodingException
 * @throws IOException
 */
public static String getStringFromInputStream(InputStream inputStream) throws UnsupportedEncodingException, IOException {
  
 InputStreamReader inputTxtStream = new InputStreamReader(inputStream, "ISO-8859-1");

  String StrTemp = "";

  if (inputTxtStream != null) {
    final int DATA_BLOCK_SIZE = 2048;

    // Create the byte array to hold the data
    char[] bytes = new char[DATA_BLOCK_SIZE];

    // Read in the bytes
    int numRead = 0;

    while ((numRead = inputTxtStream.read(bytes, 0, bytes.length)) >= 0) {
      StrTemp += new String(bytes, 0, numRead);
    }
  }

  return StrTemp;
}
 
開發者ID:costea7,項目名稱:ChronoBike,代碼行數:33,代碼來源:StreamUtil.java

示例2: getStringFromFile

import java.io.InputStreamReader; //導入方法依賴的package包/類
/**
   * Convert a file to a String using specified encoding ("UTF-8", "UTF-16", "latin1", ...)
   * 
   * @param String filePath, String charset
   * 
   * @return String
   * 
   * @throws UnsupportedEncodingException, IOException
   */
public static String getStringFromFile(String filePath, String charset) throws Exception {

	File file = new File(filePath);
	String str = null;
	int numRead = 0;
	final int DATA_BLOCK_SIZE = 2048;
	
	InputStreamReader isr = new InputStreamReader(new FileInputStream(file), charset);

    if (isr != null) {

      // Create the byte array to hold the data
      char[] charArray = new char[DATA_BLOCK_SIZE];

      while ((numRead = isr.read(charArray, 0, DATA_BLOCK_SIZE)) >= 0) {
        str += new String(charArray, 0, numRead);
      }
      isr.close();
    }

    return str;
}
 
開發者ID:costea7,項目名稱:ChronoBike,代碼行數:32,代碼來源:StreamUtil.java

示例3: BacktrackInputReader

import java.io.InputStreamReader; //導入方法依賴的package包/類
/**
 * Create an object to read the file name passed with the given charset.
 *
 * @param file	The filename to open
 */

public BacktrackInputReader(File file, String charset)
{
	try
	{
		InputStreamReader isr = readerFactory(file, charset);
		data = new char[readerLength(file, isr)];
		max = isr.read(data);
		pos = 0;
		isr.close();
	}
	catch (IOException e)
	{
		throw new InternalException(0, e.getMessage());
	}
}
 
開發者ID:nickbattle,項目名稱:FJ-VDMJ,代碼行數:22,代碼來源:BacktrackInputReader.java

示例4: checkRoot

import java.io.InputStreamReader; //導入方法依賴的package包/類
private static void checkRoot() {
    sRoot = false;
    char[] buff = new char[1024];
    try {
        Process process = Runtime.getRuntime().exec("su");
        OutputStreamWriter output = new OutputStreamWriter(process.getOutputStream());
        InputStreamReader input = new InputStreamReader(process.getInputStream());
        String testStr = "ROOT_TEST";
        output.write("echo " + testStr + "\n");
        output.flush();
        output.write("exit\n");
        output.flush();
        process.waitFor();
        int count = input.read(buff);
        if (count > 0) {
            if (new String(buff, 0, count).startsWith(testStr))
                sRoot = true;
        }
    }catch (Exception e){
        e.printStackTrace();
    }
}
 
開發者ID:XndroidDev,項目名稱:Xndroid,代碼行數:23,代碼來源:ShellUtils.java

示例5: create

import java.io.InputStreamReader; //導入方法依賴的package包/類
public static Template create(InputStream in) {
	InputStreamReader reader = new InputStreamReader(in);
	char[] buf = new char[4096];
	StringBuilder dest = new StringBuilder();
	while (true) {
		try {
			int nbytes = reader.read(buf);
			if (nbytes < 0)
				break;
			dest.append(buf, 0, nbytes);
		} catch (IOException e) {
			break;
		}
	}
	return new Template(dest.toString());
}
 
開發者ID:LogisimIt,項目名稱:Logisim,代碼行數:17,代碼來源:Template.java

示例6: readFully

import java.io.InputStreamReader; //導入方法依賴的package包/類
public static String readFully(InputStreamReader in) {
	if (in == null) {
		return null;
	}
	try {
		StringWriter result = new StringWriter();
		char[] cbuf = new char[4096];
		int cnt;
		while ((cnt=in.read(cbuf)) > 0) {
			result.write(cbuf, 0, cnt);
		}
		return result.toString();
	} catch (IOException e) {
		throw new RuntimeException(e);
	}
	finally {
		try {
			in.close();
		} catch (IOException ignore) {}
	}
}
 
開發者ID:ferenc-hechler,項目名稱:RollenspielAlexaSkill,代碼行數:22,代碼來源:TextUtil.java

示例7: run

import java.io.InputStreamReader; //導入方法依賴的package包/類
public void run() {
       /* interrompe il controllato se l'utente preme INVIO */
       logger.info("KeyboardKiller: Press ENTER to cancel task(s).");
	/* aspetta che l'utente prima INVIO */ 
	InputStreamReader inputStreamReader = new InputStreamReader(System.in);
	char answer = '\0'; 
	while (answer=='\0') {
           try {
               answer = (char) inputStreamReader.read();
           } catch (IOException e) {
               logger.info(this.toString() + ": I/O exception: " + e.toString());
           }
	}
	/* ok, ora interrompe i controllati */
	this.cancel(); 
}
 
開發者ID:aswroma3,項目名稱:asw,代碼行數:17,代碼來源:KeyboardKiller.java

示例8: inputStreamToString

import java.io.InputStreamReader; //導入方法依賴的package包/類
/**
 * Using a Reader and a Writer, returns a String from an InputStream.
 */
public static String inputStreamToString(InputStream x,
        int length) throws IOException {

    InputStreamReader in        = new InputStreamReader(x);
    StringWriter      writer    = new StringWriter();
    int               blocksize = 8 * 1024;
    char[]            buffer    = new char[blocksize];

    for (int left = length; left > 0; ) {
        int read = in.read(buffer, 0, left > blocksize ? blocksize
                                                       : left);

        if (read == -1) {
            break;
        }

        writer.write(buffer, 0, read);

        left -= read;
    }

    writer.close();

    return writer.toString();
}
 
開發者ID:parabuild-ci,項目名稱:parabuild-ci,代碼行數:29,代碼來源:StringConverter.java

示例9: main

import java.io.InputStreamReader; //導入方法依賴的package包/類
public static void main(String[] args) throws IOException {
    InputStreamReader is = new InputStreamReader( System.in );
    Button btn = new Button();
    while (true) {
        System.out.print("Press 'Enter'");
        is.read();
        btn.push();
    }
}
 
開發者ID:bmariesan,項目名稱:iStudent,代碼行數:10,代碼來源:StateDemo.java

示例10: readStream

import java.io.InputStreamReader; //導入方法依賴的package包/類
/**
 * High performance read from an {@link InputStream} into a String
 * 
 * @param inputStream
 *            the input stream from which to read
 * @return the string read from the reader
 * @throws IOException
 *             if an IOException occurs
 */
private static String readStream(InputStream inputStream) throws IOException {
	InputStreamReader reader = new InputStreamReader(inputStream);
	StringWriter sw = new StringWriter();
	char[] buffer = new char[BUFFER_SIZE];
	int pos = 0;
	while ((pos = reader.read(buffer)) != -1) {
		sw.write(buffer, 0, pos);
	}
	return sw.toString();
}
 
開發者ID:kmecpp,項目名稱:JSpark,代碼行數:20,代碼來源:FileUtil.java

示例11: CRCCheck

import java.io.InputStreamReader; //導入方法依賴的package包/類
/**
 * Check if the CRC of the snapshot file matches the digest.
 * @param f The snapshot file object
 * @return The table list as a string
 * @throws IOException If CRC does not match
 */
public static String CRCCheck(File f) throws IOException {
    final FileInputStream fis = new FileInputStream(f);
    try {
        final BufferedInputStream bis = new BufferedInputStream(fis);
        ByteBuffer crcBuffer = ByteBuffer.allocate(4);
        if (4 != bis.read(crcBuffer.array())) {
            throw new EOFException("EOF while attempting to read CRC from snapshot digest");
        }
        final int crc = crcBuffer.getInt();
        final InputStreamReader isr = new InputStreamReader(bis, "UTF-8");
        CharArrayWriter caw = new CharArrayWriter();
        while (true) {
            int nextChar = isr.read();
            if (nextChar == -1) {
                throw new EOFException("EOF while reading snapshot digest");
            }
            if (nextChar == '\n') {
                break;
            }
            caw.write(nextChar);
        }
        String tableList = caw.toString();
        byte tableListBytes[] = tableList.getBytes("UTF-8");
        CRC32 tableListCRC = new CRC32();
        tableListCRC.update(tableListBytes);
        tableListCRC.update("\n".getBytes("UTF-8"));
        final int calculatedValue = (int)tableListCRC.getValue();
        if (crc != calculatedValue) {
            throw new IOException("CRC of snapshot digest did not match digest contents");
        }

        return tableList;
    } finally {
        try {
            if (fis != null)
                fis.close();
        } catch (IOException e) {}
    }
}
 
開發者ID:s-store,項目名稱:sstore-soft,代碼行數:46,代碼來源:SnapshotUtil.java

示例12: parse

import java.io.InputStreamReader; //導入方法依賴的package包/類
protected void parse(String pPath) {
    try {
        char[] tBytes = new char[1024];
        StringBuilder tBuffer = new StringBuilder();
        InputStream tStream = getClass().getResourceAsStream(pPath);
        if (tStream == null) {
            tStream = ClassLoader.getSystemResourceAsStream(pPath);
        }
        if (tStream == null) {
            tStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(pPath);
        }
        InputStreamReader tReader = new InputStreamReader(tStream, "UTF-8");
        int tCount = tReader.read(tBytes);
        while (tCount > 0) {
            tBuffer.append(new String(tBytes, 0, tCount));
            tCount = tReader.read(tBytes);
        }

        String[] tArr = tBuffer.toString().split("\n");

        createLanguages(tArr[0]);
        for (int i = 1; i < tArr.length; i++) {
            if (tArr[i].indexOf(DELIMITER) > 0) {
                createWord(tArr[i].split(DELIMITER));
            }
        }
        tStream.close();
    }
    catch (Exception e) {
        throw new RuntimeException("Failed to parse dictionary: " + pPath, e);
    }
}
 
開發者ID:cinnober,項目名稱:ciguan,代碼行數:33,代碼來源:AsDictionaryHandler.java

示例13: getStringFromInputStream

import java.io.InputStreamReader; //導入方法依賴的package包/類
private static String getStringFromInputStream(InputStream stream) throws IOException {
    int n = 0;
    char[] buffer = new char[1024 * 4];
    InputStreamReader reader = new InputStreamReader(stream, "UTF8");
    StringWriter writer = new StringWriter();
    while (-1 != (n = reader.read(buffer))) writer.write(buffer, 0, n);
    return writer.toString();
}
 
開發者ID:AdyoOrg,項目名稱:adyo-android,代碼行數:9,代碼來源:AdyoRestRequest.java

示例14: linkUnix

import java.io.InputStreamReader; //導入方法依賴的package包/類
private void linkUnix() throws IOException {
    File so = new File(getDataFolder(), "ChocoTest.so");
    if(!so.exists()) {
        FileWriter wr = new FileWriter(so);
        InputStreamReader in = new InputStreamReader(this.getClass().getResourceAsStream("ChocoTest.so"));
        int i;
        while((i = in.read()) != -1) {
            wr.write(i);
        }
    }
    System.load(so.getAbsolutePath());
}
 
開發者ID:Redrield,項目名稱:SpigotNativePlugin,代碼行數:13,代碼來源:ChocoTest.java

示例15: streamToString

import java.io.InputStreamReader; //導入方法依賴的package包/類
protected String streamToString(InputStream is) throws Exception
{
    String encoding = mTextEncoding;
    if (encoding == null) {
        encoding = mHeaderEncoding;
        if (encoding == null) {
            //Use the default encoding. This is not very optimal, but there is not
            //much that can be done about it.
            encoding = DEFAULT_ENCODING;    
        }
    }
    
    //Try to preallocate the string builder. It is ok if this is huge, we will
    //probably get an out of memory exception and fail gracefully.
    int sizeHint = (int)mContentLength;
    if (sizeHint < 0) {
        sizeHint = 1024;
    }
    
    char[] buffer = new char[BUFFER_SIZE];
    InputStreamReader reader = new InputStreamReader(is, encoding);
    StringBuilder result = new StringBuilder(sizeHint);
    
    int count = 0;
    while ((count = reader.read(buffer, 0, buffer.length)) >= 0) {
        result.append(buffer, 0, count);
    }
    return result.toString();
}
 
開發者ID:suomenriistakeskus,項目名稱:oma-riista-android,代碼行數:30,代碼來源:TextTask.java


注:本文中的java.io.InputStreamReader.read方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。