List of usage examples for java.io BufferedReader read
public int read() throws IOException
From source file:okuyama.imdst.util.KeyManagerValueMap.java
/** * ??????<br>/*from ww w .jav a 2 s .c o m*/ * Object?????? */ public void initNoMemoryModeSetting(String lineFile) { try { if (sync == null) sync = new Object(); readObjectFlg = true; this.tmpVacuumeLineFile = lineFile + ".vacuumtmp"; this.tmpVacuumeCopyMapDirs = new String[5]; this.tmpVacuumeCopyMapDirs[0] = lineFile + ".cpmapdir1/"; this.tmpVacuumeCopyMapDirs[1] = lineFile + ".cpmapdir2/"; this.tmpVacuumeCopyMapDirs[2] = lineFile + ".cpmapdir3/"; this.tmpVacuumeCopyMapDirs[3] = lineFile + ".cpmapdir4/"; this.tmpVacuumeCopyMapDirs[4] = lineFile + ".cpmapdir5/"; // ??Value??Map? String[] overSizeDataStoreDirs = new String[1]; for (int dirIdx = 0; dirIdx < 1; dirIdx++) { overSizeDataStoreDirs[dirIdx] = lineFile + "_" + dirIdx + "/"; } if (this.overSizeDataStore == null) this.overSizeDataStore = new FileBaseDataMap(overSizeDataStoreDirs, 100000, 0.01, ImdstDefine.saveDataMaxSize, ImdstDefine.dataFileWriteMaxSize * 5, ImdstDefine.dataFileWriteMaxSize * 15); File valueFile = new File(lineFile); if (!valueFile.exists() || valueFile.length() < 1) { super.clear(); } // ??BufferedWriter this.bw = new BufferedWriter( new OutputStreamWriter(new FileOutputStream(valueFile, true), ImdstDefine.keyWorkFileEncoding), 1024 * 256); this.dataFileBufferUseCount = new AtomicInteger(0); // ?????? if (ImdstDefine.dataFileWriteDelayFlg) { // ?? this.raf = new CustomRandomAccess(new File(lineFile), "rw"); } else { // ??? //this.raf = new RandomAccessFile(new File(lineFile) , "rw"); //this.raf = new SortedSchedulingRandomAccess(new File(lineFile) , "rw"); this.raf = new HighSpeedDiskCacheRandomAccess(new File(lineFile), "rw", this.diskCacheFile); } // ??? this.raf.setDataPointMap(this); // ???? this.deletedDataPointList = new ArrayBlockingQueue(ImdstDefine.numberOfDeletedDataPoint); BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(new File(lineFile)), ImdstDefine.keyWorkFileEncoding)); this.lineFile = lineFile; int counter = 0; // ???? // ???????????()????? String readDataLine = null; while ((readDataLine = br.readLine()) != null) { counter++; boolean zeroDataFlg = false; int writeLen = this.oneDataLength; if (readDataLine.trim().length() == 0) zeroDataFlg = true; if (readDataLine.getBytes().length < this.oneDataLength) { int shiftByteSize = 0; if (readDataLine.length() < "(B)!0".length()) { int shift = "(B)!0".length() - readDataLine.length(); shiftByteSize = shift; } readDataLine = "(B)!0"; StringBuilder updateBuf = new StringBuilder(readDataLine); for (int i = 0; i < (this.oneDataLength - readDataLine.length()); i++) { updateBuf.append("&"); shiftByteSize++; } if (!zeroDataFlg) { updateBuf.append("\n"); writeLen = writeLen + 1; } shiftByteSize++; this.raf.seek(this.convertLineToSeekPoint(counter)); this.raf.write(updateBuf.toString().getBytes(), 0, writeLen); for (int i = 0; i < shiftByteSize; i++) { br.read(); } } } this.lineCount = counter; br.close(); // ??? this.nowKeySize = super.size(); } catch (Exception e) { e.printStackTrace(); // StatusUtil.setStatusAndMessage(1, "KeyManagerValueMap - init - Error [" + e.getMessage() + "]"); } }
From source file:com.fujitsu.dc.test.utils.Http.java
/** * ????.//from w ww . j a v a 2s .co m * @return TResponse */ public TResponse returns() { BufferedReader br = null; try { // ??? InputStreamReader isr = new InputStreamReader(is, CharEncoding.UTF_8); br = new BufferedReader(isr); String firstLine = br.readLine(); firstLine = this.processParams(firstLine); String[] l1 = firstLine.split(" "); this.method = l1[0]; this.path = l1[1]; String protoVersion = l1[2]; // Open this.url = new URL(baseUrl + this.path); try { this.socket = createSocket(this.url); } catch (KeyManagementException e) { throw new RuntimeException(e); } catch (KeyStoreException e) { throw new RuntimeException(e); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (CertificateException e) { throw new RuntimeException(e); } this.sIn = this.socket.getInputStream(); this.sOut = this.socket.getOutputStream(); this.sReader = new BufferedReader(new InputStreamReader(this.sIn, CharEncoding.UTF_8)); this.sWriter = new BufferedOutputStream(this.sOut); // ? StringBuilder sb = new StringBuilder(); sb.append(this.method); sb.append(" "); sb.append(this.url.getPath()); if (this.url.getQuery() != null) { sb.append("?"); sb.append(this.url.getQuery()); } sb.append(" "); sb.append(protoVersion); this.sWriter.write(sb.toString().getBytes(CharEncoding.UTF_8)); this.sWriter.write(CRLF.getBytes(CharEncoding.UTF_8)); log.debug("Req Start -------"); log.debug(sb.toString()); // Header String line = null; String lastLine = null; int contentLengthLineNum = -1; List<String> lines = new ArrayList<String>(); int i = 0; while ((line = br.readLine()) != null) { if (line.length() == 0) { break; } line = this.processParams(line); if (line.toLowerCase().startsWith(HttpHeaders.CONTENT_LENGTH.toLowerCase())) { // Content-Length??????????????????? contentLengthLineNum = i; } else if (line.toLowerCase().startsWith(HttpHeaders.HOST.toLowerCase())) { line = line.replaceAll("\\?", this.url.getAuthority()); } lines.add(line + CRLF); lastLine = line; i++; } // Version? lines.add("X-Dc-Version: " + DcCoreTestConfig.getCoreVersion() + CRLF); String body = null; // ????Break??????Body????? if (line != null) { log.debug("Req Body-------"); i = 1; StringWriter sw = new StringWriter(); int chr; while ((chr = br.read()) != -1) { sw.write((char) chr); } body = sw.toString(); body = this.processParams(body); // Content-Length? if (contentLengthLineNum != -1) { String contentLength = lines.get(contentLengthLineNum).replaceAll("\\?", String.valueOf(body.getBytes().length)); lines.set(contentLengthLineNum, contentLength); } } else { if (this.paraBody != null) { log.debug("Req Body-------"); // ?Body?? String contentLength = lines.get(contentLengthLineNum).replaceAll("\\?", String.valueOf(this.paraBody.length)); lines.set(contentLengthLineNum, contentLength); } else { // ??????. if (lastLine.length() > 0) { log.debug("one more CRLF"); this.sWriter.write(CRLF.getBytes(CharEncoding.UTF_8)); } } } // Header?? for (String l : lines) { this.sWriter.write(l.getBytes(CharEncoding.UTF_8)); if (log.isDebugEnabled()) { l.replaceAll(CRLF, ""); log.debug(l); } } // this.sWriter.write(CRLF.getBytes(CharEncoding.UTF_8)); // Body?? if (body != null) { this.sWriter.write(body.getBytes(CharEncoding.UTF_8)); log.debug(body); } // ?Body?? if (this.paraBody != null) { this.sWriter.write(this.paraBody); this.sWriter.write(CRLF.getBytes(CharEncoding.UTF_8)); } this.sWriter.flush(); // ?? TResponse ret = new TResponse(this.sReader); return ret; } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } catch (Exception e) { throw new RuntimeException(e); } finally { try { if (br != null) { br.close(); } if (this.sWriter != null) { this.sWriter.close(); } if (this.sReader != null) { this.sReader.close(); } } catch (IOException e) { throw new RuntimeException(e); } } }
From source file:io.personium.test.utils.Http.java
/** * ????./*from w ww . j a v a 2s . c o m*/ * @return TResponse */ public TResponse returns() { BufferedReader br = null; try { // ??? InputStreamReader isr = new InputStreamReader(is, CharEncoding.UTF_8); br = new BufferedReader(isr); String firstLine = br.readLine(); firstLine = this.processParams(firstLine); String[] l1 = firstLine.split(" "); this.method = l1[0]; this.path = l1[1]; String protoVersion = l1[2]; // Open this.url = new URL(baseUrl + this.path); try { this.socket = createSocket(this.url); } catch (KeyManagementException e) { throw new RuntimeException(e); } catch (KeyStoreException e) { throw new RuntimeException(e); } catch (NoSuchAlgorithmException e) { throw new RuntimeException(e); } catch (CertificateException e) { throw new RuntimeException(e); } this.sIn = this.socket.getInputStream(); this.sOut = this.socket.getOutputStream(); this.sReader = new BufferedReader(new InputStreamReader(this.sIn, CharEncoding.UTF_8)); this.sWriter = new BufferedOutputStream(this.sOut); // ? StringBuilder sb = new StringBuilder(); sb.append(this.method); sb.append(" "); sb.append(this.url.getPath()); if (this.url.getQuery() != null) { sb.append("?"); sb.append(this.url.getQuery()); } sb.append(" "); sb.append(protoVersion); this.sWriter.write(sb.toString().getBytes(CharEncoding.UTF_8)); this.sWriter.write(CRLF.getBytes(CharEncoding.UTF_8)); log.debug("Req Start -------"); log.debug(sb.toString()); // Header String line = null; String lastLine = null; int contentLengthLineNum = -1; List<String> lines = new ArrayList<String>(); int i = 0; while ((line = br.readLine()) != null) { if (line.length() == 0) { break; } line = this.processParams(line); if (line.toLowerCase().startsWith(HttpHeaders.CONTENT_LENGTH.toLowerCase())) { // Content-Length??????????????????? contentLengthLineNum = i; } else if (line.toLowerCase().startsWith(HttpHeaders.HOST.toLowerCase())) { line = line.replaceAll("\\?", this.url.getAuthority()); } lines.add(line + CRLF); lastLine = line; i++; } // Version? lines.add("X-Personium-Version: " + PersoniumCoreTestConfig.getCoreVersion() + CRLF); String body = null; // ????Break??????Body????? if (line != null) { log.debug("Req Body-------"); i = 1; StringWriter sw = new StringWriter(); int chr; while ((chr = br.read()) != -1) { sw.write((char) chr); } body = sw.toString(); body = this.processParams(body); // Content-Length? if (contentLengthLineNum != -1) { String contentLength = lines.get(contentLengthLineNum).replaceAll("\\?", String.valueOf(body.getBytes().length)); lines.set(contentLengthLineNum, contentLength); } } else { if (this.paraBody != null) { log.debug("Req Body-------"); // ?Body?? String contentLength = lines.get(contentLengthLineNum).replaceAll("\\?", String.valueOf(this.paraBody.length)); lines.set(contentLengthLineNum, contentLength); } else { // ??????. if (lastLine.length() > 0) { log.debug("one more CRLF"); this.sWriter.write(CRLF.getBytes(CharEncoding.UTF_8)); } } } // Header?? for (String l : lines) { this.sWriter.write(l.getBytes(CharEncoding.UTF_8)); if (log.isDebugEnabled()) { l.replaceAll(CRLF, ""); log.debug(l); } } // this.sWriter.write(CRLF.getBytes(CharEncoding.UTF_8)); // Body?? if (body != null) { this.sWriter.write(body.getBytes(CharEncoding.UTF_8)); log.debug(body); } // ?Body?? if (this.paraBody != null) { this.sWriter.write(this.paraBody); this.sWriter.write(CRLF.getBytes(CharEncoding.UTF_8)); } this.sWriter.flush(); // ?? TResponse ret = new TResponse(this.sReader); return ret; } catch (UnsupportedEncodingException e) { throw new RuntimeException(e); } catch (IOException e) { throw new RuntimeException(e); } catch (Exception e) { throw new RuntimeException(e); } finally { try { if (br != null) { br.close(); } if (this.sWriter != null) { this.sWriter.close(); } if (this.sReader != null) { this.sReader.close(); } } catch (IOException e) { throw new RuntimeException(e); } } }
From source file:org.opendaylight.vtn.manager.it.northbound.VtnNorthboundIT.java
/** * Send request and get result// ww w . j a v a 2 s.c o m * * @param restUrl A request URL. * @param method A request method. * @param body A request body send with request. * @param contentType A contentType of request. * @param auth if {@code true} authorization succeed, * else if {@code false} authorization fails. * @return A returned result for request. */ private String getJsonResult(String restUrl, String method, String body, String contentType, boolean auth) { // Initialize response code to indicate error httpResponseCode = HTTP_BAD_REQUEST; httpLocation = null; LOG.debug("HTTP method: {}, uri: {}", method, restUrl); LOG.debug("Body: {}", body); try { URL url = new URL(restUrl); this.userManager.getAuthorizationList(); String authString = null; if (auth) { this.userManager.authenticate("admin", "admin"); authString = "admin:admin"; } else { this.userManager.authenticate("admin", "bad"); authString = "admin:bad"; } byte[] authEncBytes = Base64.encodeBase64(authString.getBytes()); String authStringEnc = new String(authEncBytes); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod(method); connection.setRequestProperty("Authorization", "Basic " + authStringEnc); connection.setRequestProperty("Content-Type", contentType); connection.setRequestProperty("Accept", "application/json"); if (body != null) { connection.setDoOutput(true); OutputStreamWriter wr = new OutputStreamWriter(connection.getOutputStream()); wr.write(body); wr.flush(); } connection.connect(); connection.getContentType(); // Response code for success should be 2xx httpResponseCode = connection.getResponseCode(); LOG.debug("HTTP response code: {}", httpResponseCode); LOG.debug("HTTP response message: {}", connection.getResponseMessage()); if (httpResponseCode == HTTP_CREATED) { // Determine location. for (int i = 0; true; i++) { String field = connection.getHeaderFieldKey(i); if (field == null) { if (i == 0) { continue; } break; } if (field.equalsIgnoreCase("location")) { httpLocation = connection.getHeaderField(i); break; } } } boolean error = (httpResponseCode > 299); InputStream is = (error) ? connection.getErrorStream() : connection.getInputStream(); StringBuilder sb = new StringBuilder(); if (is != null) { InputStreamReader in = new InputStreamReader(is, Charset.forName("UTF-8")); BufferedReader rd = new BufferedReader(in); int cp; while ((cp = rd.read()) != -1) { sb.append((char) cp); } is.close(); } connection.disconnect(); if (httpResponseCode > 299) { String msg = sb.toString(); if (msg.length() != 0) { LOG.debug("HTTP error response body: {}", msg); return msg; } return httpResponseCode.toString(); } if (httpResponseCode == HTTP_NO_CONTENT) { assertEquals(0, sb.length()); } return sb.toString(); } catch (Exception e) { LOG.error("Caught exception.", e); return null; } }