java.io.BufferedInputStream#reset ( )源码实例Demo

下面列出了java.io.BufferedInputStream#reset ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。

源代码1 项目: nomulus   文件: RdeUtil.java
/**
 * Look at some bytes from {@code xmlInput} to ensure it appears to be a FULL XML deposit and
 * then use a regular expression to extract the watermark timestamp which is returned.
 */
public static DateTime peekWatermark(BufferedInputStream xmlInput)
    throws IOException, XmlException {
  xmlInput.mark(PEEK_SIZE);
  byte[] peek = new byte[PEEK_SIZE];
  if (xmlInput.read(peek) != PEEK_SIZE) {
    throw new IOException(String.format("Failed to peek %,d bytes on input file", PEEK_SIZE));
  }
  xmlInput.reset();
  String peekStr = new String(peek, UTF_8);
  if (!peekStr.contains("urn:ietf:params:xml:ns:rde-1.0")) {
    throw new XmlException(String.format(
        "Does not appear to be an XML RDE deposit\n%s", dumpHex(peek)));
  }
  if (!peekStr.contains("type=\"FULL\"")) {
    throw new XmlException("Only FULL XML RDE deposits suppported at this time");
  }
  Matcher watermarkMatcher = WATERMARK_PATTERN.matcher(peekStr);
  if (!watermarkMatcher.find()) {
    throw new XmlException("Could not find RDE watermark in XML");
  }
  return DATETIME_FORMATTER.parseDateTime(watermarkMatcher.group(1));
}
 
private void testPositive(final int readLimit) throws IOException {
    byte[] bytes = new byte[100];
    for (int i=0; i < bytes.length; i++)
        bytes[i] = (byte)i;
    // buffer of 10 bytes
    BufferedInputStream bis =
        new BufferedInputStream(new ByteArrayInputStream(bytes), 10);
    // skip 10 bytes
    bis.skip(10);
    bis.mark(readLimit);   // buffer size would increase up to readLimit
    // read 30 bytes in total, with internal buffer increased to 20
    bis.read(new byte[20]);
    bis.reset();
    assert(bis.read() == 10);
}
 
/** @param input the given input stream must be reset later on
 * @param path
 * @return either the path of the root node of the given docview xml or {@code null} if no docview xml given
 * @throws IOException */
private static Path getDocumentViewXmlRootPath(BufferedInputStream input, Path path) throws IOException {
    Path name = path.getFileName();
    Path rootPath = null;

    int nameCount = path.getNameCount();
    if (name.equals(Paths.get(Constants.DOT_CONTENT_XML))) {
        if (nameCount > 1) {
            rootPath = path.subpath(0, nameCount - 1);
        } else {
            rootPath = Paths.get("");
        }
        // correct suffix matching
    } else if (name.toString().endsWith(".xml")) {

        // we need to rely on a buffered input stream to be able to reset it later
        input.mark(1024);
        // analyze content
        // this closes the input source internally, therefore protect against closing
        // make sure to initialize the SLF4J logger appropriately (for the XmlAnalyzer)
        try {
            SerializationType type = XmlAnalyzer.analyze(new InputSource(new CloseShieldInputStream(input)));
            if (type == SerializationType.XML_DOCVIEW) {
                //  remove .xml extension
                String fileName = path.getFileName().toString();
                fileName = fileName.substring(0, fileName.length() - ".xml".length());
                if (nameCount > 1) {
                    rootPath = path.subpath(0, nameCount - 1).resolve(fileName);
                } else {
                    rootPath = Paths.get(fileName);
                }
            }
        } finally {
            input.reset();
        }
    }
    return rootPath;
}
 
源代码4 项目: hop   文件: BOMDetector.java
public BOMDetector( BufferedInputStream in ) throws IOException {
  this.in = in;
  in.mark( 16 );
  readBOM();
  in.reset();
  in.skip( bomSize );
}
 
源代码5 项目: spliceengine   文件: Request.java
private static boolean peekStream( BufferedInputStream in ) 
    throws IOException {
    
    in.mark( 1 );
    boolean notYet =  in.read() > -1;
    in.reset();
    return notYet;
    
}
 
源代码6 项目: dragonwell8_jdk   文件: HttpAwareServerSocket.java
/**
 * Accept a connection. This method will block until the connection
 * is made and four bytes can be read from the input stream.
 * If the first four bytes are "POST", then an HttpReceiveSocket is
 * returned, which will handle the HTTP protocol wrapping.
 * Otherwise, a WrappedSocket is returned.  The input stream will be
 * reset to the beginning of the transmission.
 * In either case, a BufferedInputStream will already be on top of
 * the underlying socket's input stream.
 * @exception IOException IO error when waiting for the connection.
 */
public Socket accept() throws IOException
{
    Socket socket = super.accept();
    BufferedInputStream in =
        new BufferedInputStream(socket.getInputStream());

    RMIMasterSocketFactory.proxyLog.log(Log.BRIEF,
        "socket accepted (checking for POST)");

    in.mark(4);
    boolean isHttp = (in.read() == 'P') &&
                     (in.read() == 'O') &&
                     (in.read() == 'S') &&
                     (in.read() == 'T');
    in.reset();

    if (RMIMasterSocketFactory.proxyLog.isLoggable(Log.BRIEF)) {
        RMIMasterSocketFactory.proxyLog.log(Log.BRIEF,
            (isHttp ? "POST found, HTTP socket returned" :
                      "POST not found, direct socket returned"));
    }

    if (isHttp)
        return new HttpReceiveSocket(socket, in, null);
    else
        return new WrappedSocket(socket, in, null);
}
 
源代码7 项目: android-discourse   文件: Connection.java
/**
 * Returns true if we are confident that we can read data from this
 * connection. This is more expensive and more accurate than {@link
 * #isAlive()}; callers should check {@link #isAlive()} first.
 */
public boolean isReadable() {
    if (!(in instanceof BufferedInputStream)) {
        return true; // Optimistic.
    }
    if (isSpdy()) {
        return true; // Optimistic. We can't test SPDY because its streams are in use.
    }
    BufferedInputStream bufferedInputStream = (BufferedInputStream) in;
    try {
        int readTimeout = socket.getSoTimeout();
        try {
            socket.setSoTimeout(1);
            bufferedInputStream.mark(1);
            if (bufferedInputStream.read() == -1) {
                return false; // Stream is exhausted; socket is closed.
            }
            bufferedInputStream.reset();
            return true;
        } finally {
            socket.setSoTimeout(readTimeout);
        }
    } catch (SocketTimeoutException ignored) {
        return true; // Read timed out; socket is good.
    } catch (IOException e) {
        return false; // Couldn't read; socket is closed.
    }
}
 
源代码8 项目: ShareSDK   文件: BitmapAsyncTask.java
private Bitmap getSampleBitmap(InputStream is, int width, int height) {

        BufferedInputStream stream = new BufferedInputStream(is);
        stream.mark(4 * 1024);
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(stream, null, options);
        calculateInSampleSize(width, height, options, true);
        try {
            stream.reset();
        } catch (IOException e) {
        }
        return BitmapFactory.decodeStream(stream, null, options);
    }
 
源代码9 项目: TencentKona-8   文件: HttpAwareServerSocket.java
/**
 * Accept a connection. This method will block until the connection
 * is made and four bytes can be read from the input stream.
 * If the first four bytes are "POST", then an HttpReceiveSocket is
 * returned, which will handle the HTTP protocol wrapping.
 * Otherwise, a WrappedSocket is returned.  The input stream will be
 * reset to the beginning of the transmission.
 * In either case, a BufferedInputStream will already be on top of
 * the underlying socket's input stream.
 * @exception IOException IO error when waiting for the connection.
 */
public Socket accept() throws IOException
{
    Socket socket = super.accept();
    BufferedInputStream in =
        new BufferedInputStream(socket.getInputStream());

    RMIMasterSocketFactory.proxyLog.log(Log.BRIEF,
        "socket accepted (checking for POST)");

    in.mark(4);
    boolean isHttp = (in.read() == 'P') &&
                     (in.read() == 'O') &&
                     (in.read() == 'S') &&
                     (in.read() == 'T');
    in.reset();

    if (RMIMasterSocketFactory.proxyLog.isLoggable(Log.BRIEF)) {
        RMIMasterSocketFactory.proxyLog.log(Log.BRIEF,
            (isHttp ? "POST found, HTTP socket returned" :
                      "POST not found, direct socket returned"));
    }

    if (isHttp)
        return new HttpReceiveSocket(socket, in, null);
    else
        return new WrappedSocket(socket, in, null);
}
 
源代码10 项目: CordovaYoutubeVideoPlayer   文件: Connection.java
/**
 * Returns true if we are confident that we can read data from this
 * connection. This is more expensive and more accurate than {@link
 * #isAlive()}; callers should check {@link #isAlive()} first.
 */
public boolean isReadable() {
  if (!(in instanceof BufferedInputStream)) {
    return true; // Optimistic.
  }
  if (isSpdy()) {
    return true; // Optimistic. We can't test SPDY because its streams are in use.
  }
  BufferedInputStream bufferedInputStream = (BufferedInputStream) in;
  try {
    int readTimeout = socket.getSoTimeout();
    try {
      socket.setSoTimeout(1);
      bufferedInputStream.mark(1);
      if (bufferedInputStream.read() == -1) {
        return false; // Stream is exhausted; socket is closed.
      }
      bufferedInputStream.reset();
      return true;
    } finally {
      socket.setSoTimeout(readTimeout);
    }
  } catch (SocketTimeoutException ignored) {
    return true; // Read timed out; socket is good.
  } catch (IOException e) {
    return false; // Couldn't read; socket is closed.
  }
}
 
源代码11 项目: bluemix-parking-meter   文件: Connection.java
/**
 * Returns true if we are confident that we can read data from this
 * connection. This is more expensive and more accurate than {@link
 * #isAlive()}; callers should check {@link #isAlive()} first.
 */
public boolean isReadable() {
  if (!(in instanceof BufferedInputStream)) {
    return true; // Optimistic.
  }
  if (isSpdy()) {
    return true; // Optimistic. We can't test SPDY because its streams are in use.
  }
  BufferedInputStream bufferedInputStream = (BufferedInputStream) in;
  try {
    int readTimeout = socket.getSoTimeout();
    try {
      socket.setSoTimeout(1);
      bufferedInputStream.mark(1);
      if (bufferedInputStream.read() == -1) {
        return false; // Stream is exhausted; socket is closed.
      }
      bufferedInputStream.reset();
      return true;
    } finally {
      socket.setSoTimeout(readTimeout);
    }
  } catch (SocketTimeoutException ignored) {
    return true; // Read timed out; socket is good.
  } catch (IOException e) {
    return false; // Couldn't read; socket is closed.
  }
}
 
源代码12 项目: http-builder-ng   文件: IoUtils.java
/**
 * Safely copies the contents of the {@link BufferedInputStream} to a {@link String} and resets the stream.
 * This method is generally only useful for testing and logging purposes.
 *
 * @param inputStream the BufferedInputStream
 * @return a String copy of the stream contents
 * @throws IOException           if something goes wrong with the stream
 * @throws IllegalStateException if the stream cannot be reset
 */
public static String copyAsString(final BufferedInputStream inputStream) throws IOException, IllegalStateException {
    if (inputStream == null) return null;

    try {
        inputStream.mark(Integer.MAX_VALUE);
        return new String(streamToBytes(inputStream, false));
    } finally {
        try {
            inputStream.reset();
        } catch (IOException ioe) {
            throw new IllegalStateException("Unable to reset stream - original stream content may be corrupted");
        }
    }
}
 
源代码13 项目: lams   文件: NPOIFSDocument.java
/**
 * Stores the given data for this Document
 */
private int store(InputStream stream) throws IOException {
    final int bigBlockSize = POIFSConstants.BIG_BLOCK_MINIMUM_DOCUMENT_SIZE;
    BufferedInputStream bis = new BufferedInputStream(stream, bigBlockSize+1);
    bis.mark(bigBlockSize);

    // Do we need to store as a mini stream or a full one?
    if(bis.skip(bigBlockSize) < bigBlockSize) {
       _stream = new NPOIFSStream(_filesystem.getMiniStore());
       _block_size = _filesystem.getMiniStore().getBlockStoreBlockSize();
    } else {
       _stream = new NPOIFSStream(_filesystem);
       _block_size = _filesystem.getBlockStoreBlockSize();
    }

    // start from the beginning 
    bis.reset();
    
    // Store it
    OutputStream os = _stream.getOutputStream();
    byte buf[] = new byte[1024];
    int length = 0;
    
    for (int readBytes; (readBytes = bis.read(buf)) != -1; length += readBytes) {
        os.write(buf, 0, readBytes);
    }
    
    // Pad to the end of the block with -1s
    int usedInBlock = length % _block_size;
    if (usedInBlock != 0 && usedInBlock != _block_size) {
        int toBlockEnd = _block_size - usedInBlock;
        byte[] padding = new byte[toBlockEnd];
        Arrays.fill(padding, (byte)0xFF);
        os.write(padding);
    }
    
    // Tidy and return the length
    os.close();
    return length;
}
 
源代码14 项目: jdk8u-jdk   文件: HttpAwareServerSocket.java
/**
 * Accept a connection. This method will block until the connection
 * is made and four bytes can be read from the input stream.
 * If the first four bytes are "POST", then an HttpReceiveSocket is
 * returned, which will handle the HTTP protocol wrapping.
 * Otherwise, a WrappedSocket is returned.  The input stream will be
 * reset to the beginning of the transmission.
 * In either case, a BufferedInputStream will already be on top of
 * the underlying socket's input stream.
 * @exception IOException IO error when waiting for the connection.
 */
public Socket accept() throws IOException
{
    Socket socket = super.accept();
    BufferedInputStream in =
        new BufferedInputStream(socket.getInputStream());

    RMIMasterSocketFactory.proxyLog.log(Log.BRIEF,
        "socket accepted (checking for POST)");

    in.mark(4);
    boolean isHttp = (in.read() == 'P') &&
                     (in.read() == 'O') &&
                     (in.read() == 'S') &&
                     (in.read() == 'T');
    in.reset();

    if (RMIMasterSocketFactory.proxyLog.isLoggable(Log.BRIEF)) {
        RMIMasterSocketFactory.proxyLog.log(Log.BRIEF,
            (isHttp ? "POST found, HTTP socket returned" :
                      "POST not found, direct socket returned"));
    }

    if (isHttp)
        return new HttpReceiveSocket(socket, in, null);
    else
        return new WrappedSocket(socket, in, null);
}
 
源代码15 项目: IoTgo_Android_App   文件: Connection.java
/**
 * Returns true if we are confident that we can read data from this
 * connection. This is more expensive and more accurate than {@link
 * #isAlive()}; callers should check {@link #isAlive()} first.
 */
public boolean isReadable() {
  if (!(in instanceof BufferedInputStream)) {
    return true; // Optimistic.
  }
  if (isSpdy()) {
    return true; // Optimistic. We can't test SPDY because its streams are in use.
  }
  BufferedInputStream bufferedInputStream = (BufferedInputStream) in;
  try {
    int readTimeout = socket.getSoTimeout();
    try {
      socket.setSoTimeout(1);
      bufferedInputStream.mark(1);
      if (bufferedInputStream.read() == -1) {
        return false; // Stream is exhausted; socket is closed.
      }
      bufferedInputStream.reset();
      return true;
    } finally {
      socket.setSoTimeout(readTimeout);
    }
  } catch (SocketTimeoutException ignored) {
    return true; // Read timed out; socket is good.
  } catch (IOException e) {
    return false; // Couldn't read; socket is closed.
  }
}
 
源代码16 项目: L.TileLayer.Cordova   文件: Connection.java
/**
 * Returns true if we are confident that we can read data from this
 * connection. This is more expensive and more accurate than {@link
 * #isAlive()}; callers should check {@link #isAlive()} first.
 */
public boolean isReadable() {
  if (!(in instanceof BufferedInputStream)) {
    return true; // Optimistic.
  }
  if (isSpdy()) {
    return true; // Optimistic. We can't test SPDY because its streams are in use.
  }
  BufferedInputStream bufferedInputStream = (BufferedInputStream) in;
  try {
    int readTimeout = socket.getSoTimeout();
    try {
      socket.setSoTimeout(1);
      bufferedInputStream.mark(1);
      if (bufferedInputStream.read() == -1) {
        return false; // Stream is exhausted; socket is closed.
      }
      bufferedInputStream.reset();
      return true;
    } finally {
      socket.setSoTimeout(readTimeout);
    }
  } catch (SocketTimeoutException ignored) {
    return true; // Read timed out; socket is good.
  } catch (IOException e) {
    return false; // Couldn't read; socket is closed.
  }
}
 
源代码17 项目: Logisim   文件: LogisimFile.java
private static String getFirstLine(BufferedInputStream in) throws IOException {
	byte[] first = new byte[512];
	in.mark(first.length - 1);
	in.read(first);
	in.reset();

	int lineBreak = first.length;
	for (int i = 0; i < lineBreak; i++) {
		if (first[i] == '\n') {
			lineBreak = i;
		}
	}
	return new String(first, 0, lineBreak, "UTF-8");
}
 
源代码18 项目: tmxeditor8   文件: AnalysisXmlConvertConfigDialg.java
public static String getEncoding(File file) {
	String charset = "GBK";
	byte[] first3Bytes = new byte[3];
	boolean checked = false;
	try {
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file));
		bis.mark(0);
		int read = bis.read(first3Bytes, 0, 3);
		if (read == -1)
			return charset;
		if (first3Bytes[0] == (byte) 0xFF && first3Bytes[1] == (byte) 0xFE) {
			charset = "UTF-16LE";
			checked = true;
		}
		else if (first3Bytes[0] == (byte) 0xFE
				&& first3Bytes[1] == (byte) 0xFF) {
			charset = "UTF-16BE";
			checked = true;
		}
		else if (first3Bytes[0] == (byte) 0xEF
				&& first3Bytes[1] == (byte) 0xBB
				&& first3Bytes[2] == (byte) 0xBF) {
			charset = "UTF-8";
			checked = true;
		}
		bis.reset();
		if (!checked) {
			int loc = 0;
			while ((read = bis.read()) != -1) {
				loc++;
				if (read >= 0xF0)
					break;
				if (0x80 <= read && read <= 0xBF) // 单独出现BF以下的,也算是GBK
					break;
				if (0xC0 <= read && read <= 0xDF) {
					read = bis.read();
					if (0x80 <= read && read <= 0xBF) // 双字节 (0xC0 - 0xDF)// (0x80 - 0xBF),也可能在GB编码内
						continue;
					else
						break;
				}
				else if (0xE0 <= read && read <= 0xEF) {// 也有可能出错,但是几率较小
					read = bis.read();
					if (0x80 <= read && read <= 0xBF) {
						read = bis.read();
						if (0x80 <= read && read <= 0xBF) {
							charset = "UTF-8";
							break;
						}
						else
							break;
					}
					else
						break;
				}
			}
		}
		bis.close();
	} catch (Exception e) {
		e.printStackTrace();
	}
	return charset;
}
 
源代码19 项目: citeproc-java   文件: BibliographyFileReader.java
/**
 * Reads the first bytes of the given input stream and tries to
 * determine the file format. Resets the input stream to the position
 * it had when the method was called. Reads up to 100 KB and before
 * giving up. Note that you can supply an additional file name to help
 * the method to determine the exact file format. If you don't know the
 * file name you can pass null, but in this case the method's result
 * might be wrong (depending on the input stream's contents).
 * @param bis a buffered input stream that supports the mark and reset
 * methods
 * @param filename the name of the input file (can be null if you don't
 * know the name)
 * @return the file format (or {@link FileFormat#UNKNOWN} if the format
 * could not be determined)
 * @throws IOException if the input stream could not be read
 */
public FileFormat determineFileFormat(BufferedInputStream bis,
        String filename) throws IOException {
    int len = 1024 * 100;

    String ext = "";
    if (filename != null) {
        int dot = filename.lastIndexOf('.');
        if (dot > 0) {
            ext = filename.substring(dot + 1);
        }
    }

    // check the first couple of bytes
    bis.mark(len);
    try {
        byte[] firstCharacters = new byte[6];
        bis.read(firstCharacters);

        // check if the file starts with a %YAML directive
        if (firstCharacters[0] == '%' &&
                firstCharacters[1] == 'Y' &&
                firstCharacters[2] == 'A' &&
                firstCharacters[3] == 'M' &&
                firstCharacters[4] == 'L' &&
                Character.isWhitespace(firstCharacters[5])) {
            return FileFormat.YAML;
        }

        // check if the file starts with an EndNote tag, but
        // also make sure the extension is not 'bib' or 'yml'/'yaml'
        // because BibTeX comments and YAML directives look like
        // EndNote tags
        if (firstCharacters[0] == '%' &&
                Character.isWhitespace(firstCharacters[2]) &&
                !ext.equalsIgnoreCase("bib") &&
                !ext.equalsIgnoreCase("yaml") &&
                !ext.equalsIgnoreCase("yml")) {
            return FileFormat.ENDNOTE;
        }

        // check if the file starts with a RIS type tag
        if (firstCharacters[0] == 'T' && firstCharacters[1] == 'Y' &&
                Character.isWhitespace(firstCharacters[2]) &&
                Character.isWhitespace(firstCharacters[3]) &&
                firstCharacters[4] == '-') {
            return FileFormat.RIS;
        }
    } finally {
        bis.reset();
    }

    // now check if it's json, bibtex or yaml
    bis.mark(len);
    try {
        while (true) {
            int c = bis.read();
            --len;
            if (c < 0 || len < 2) {
                return FileFormat.UNKNOWN;
            }

            if (!Character.isWhitespace(c)) {
                if (c == '[') {
                    return FileFormat.JSON_ARRAY;
                } else if (c == '{') {
                    return FileFormat.JSON_OBJECT;
                }
                if (ext.equalsIgnoreCase("yaml") ||
                        ext.equalsIgnoreCase("yml")) {
                    return FileFormat.YAML;
                }
                return FileFormat.BIBTEX;
            }
        }
    } finally {
        bis.reset();
    }
}
 
源代码20 项目: freehealth-connector   文件: ConnectorIOUtils.java
public static InputStream decompress(InputStream input) throws TechnicalConnectorException {
   Validate.notNull(input);
   BufferedInputStream is = new BufferedInputStream(input);
   is.mark(1024);

   try {
      try {
         LOG.debug("Using createCompressorInputStream");
         return factory.createCompressorInputStream(is);
      } catch (Exception var6) {
         LOG.debug("[CompressionFactory]   " + var6.getClass().getSimpleName() + ": " + var6.getMessage());
         is.reset();

         try {
            LOG.debug("Using createArchiveInputStream");
            return asFactory.createArchiveInputStream(is);
         } catch (Exception var5) {
            LOG.debug("[ArchiveStreamFactory] " + var5.getClass().getSimpleName() + ": " + var5.getMessage());
            is.reset();

            try {
               LOG.debug("Using deflater noWrap");
               return deflater(is, true);
            } catch (Exception var4) {
               LOG.debug("[Deflater noWrap] " + var4.getClass().getSimpleName() + ": " + var4.getMessage());
               is.reset();

               try {
                  LOG.debug("Using deflater wrap");
                  return deflater(is, false);
               } catch (Exception var3) {
                  LOG.debug("[Deflater wrap] " + var3.getClass().getSimpleName() + ": " + var3.getMessage());
               }
            }
         }
      }
   } catch (IOException var7) {
      throw new TechnicalConnectorException(TechnicalConnectorExceptionValues.ERROR_COMPRESSION, var7, var7.getMessage());
   }

   throw new TechnicalConnectorException(TechnicalConnectorExceptionValues.ERROR_COMPRESSION, "Unsupported compression algorithm.");
}