本文整理汇总了Java中java.io.PushbackInputStream.unread方法的典型用法代码示例。如果您正苦于以下问题:Java PushbackInputStream.unread方法的具体用法?Java PushbackInputStream.unread怎么用?Java PushbackInputStream.unread使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.io.PushbackInputStream
的用法示例。
在下文中一共展示了PushbackInputStream.unread方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: readAhead
import java.io.PushbackInputStream; //导入方法依赖的package包/类
/**
* Fills {@code buf} with data from the given input stream and
* returns an input stream from which you can still read all data,
* including the data in buf.
*
* @param in The stream to read from.
* @param buf The buffer to fill entirely with data.
* @return A stream which holds all the data {@code in} did.
* @throws EOFException on unexpected end-of-file.
* @throws IOException on any I/O error.
*/
private static InputStream readAhead(
final @WillNotClose InputStream in,
final byte[] buf)
throws EOFException, IOException {
if (in.markSupported()) {
in.mark(buf.length);
new DataInputStream(in).readFully(buf);
in.reset();
return in;
} else {
final PushbackInputStream
pin = new PushbackInputStream(in, buf.length);
new DataInputStream(pin).readFully(buf);
pin.unread(buf);
return pin;
}
}
示例2: startSession
import java.io.PushbackInputStream; //导入方法依赖的package包/类
/**
* Grants access to everyone.Removes authentication related bytes from the
* stream, when a SOCKS5 connection is being made, selects an authentication
* NONE.
*/
public ServerAuthenticator startSession(Socket s) throws IOException {
final PushbackInputStream in = new PushbackInputStream(s
.getInputStream());
final OutputStream out = s.getOutputStream();
final int version = in.read();
if (version == 5) {
if (!selectSocks5Authentication(in, out, 0)) {
return null;
}
} else if (version == 4) {
// Else it is the request message already, version 4
in.unread(version);
} else {
return null;
}
return new ServerAuthenticatorNone(in, out);
}
示例3: decodeLineSuffix
import java.io.PushbackInputStream; //导入方法依赖的package包/类
/**
* Find the end of the line for the next operation.
* The following sequences are recognized as end-of-line
* CR, CR LF, or LF
*/
protected void decodeLineSuffix(PushbackInputStream inStream, OutputStream outStream) throws IOException {
int c;
while (true) {
c = inStream.read();
if (c == -1) {
throw new CEStreamExhausted();
}
if (c == '\n') {
break;
}
if (c == '\r') {
c = inStream.read();
if ((c != '\n') && (c != -1)) {
inStream.unread (c);
}
break;
}
}
}
示例4: decodeLinePrefix
import java.io.PushbackInputStream; //导入方法依赖的package包/类
/**
* In uuencoded buffers, encoded lines start with a character that
* represents the number of bytes encoded in this line. The last
* line of input is always a line that starts with a single space
* character, which would be a zero length line.
*/
protected int decodeLinePrefix(PushbackInputStream inStream, OutputStream outStream) throws IOException {
int c;
c = inStream.read();
if (c == ' ') {
c = inStream.read(); /* discard the (first)trailing CR or LF */
c = inStream.read(); /* check for a second one */
if ((c != '\n') && (c != -1))
inStream.unread (c);
throw new CEStreamExhausted();
} else if (c == -1) {
throw new CEFormatException("UUDecoder: Short Buffer.");
}
c = (c - ' ') & 0x3f;
if (c > bytesPerLine()) {
throw new CEFormatException("UUDecoder: Bad Line Length.");
}
return (c);
}
示例5: isInputStreamGZIPCompressed
import java.io.PushbackInputStream; //导入方法依赖的package包/类
public static boolean isInputStreamGZIPCompressed(PushbackInputStream inputStream) throws
IOException {
boolean z = true;
if (inputStream == null) {
return false;
}
byte[] signature = new byte[2];
int readStatus = inputStream.read(signature);
inputStream.unread(signature);
int streamHeader = (signature[0] & 255) | ((signature[1] << 8) & MotionEventCompat
.ACTION_POINTER_INDEX_MASK);
if (!(readStatus == 2 && 35615 == streamHeader)) {
z = false;
}
return z;
}
示例6: isInputStreamGZIPCompressed
import java.io.PushbackInputStream; //导入方法依赖的package包/类
/**
* Checks the InputStream if it contains GZIP compressed data
*
* @param inputStream InputStream to be checked
* @return true or false if the stream contains GZIP compressed data
* @throws IOException
*/
public static boolean isInputStreamGZIPCompressed(final PushbackInputStream inputStream) throws IOException {
if (inputStream == null)
return false;
byte[] signature = new byte[2];
int readStatus = inputStream.read(signature);
inputStream.unread(signature);
int streamHeader = ((int) signature[0] & 0xff) | ((signature[1] << 8) & 0xff00);
return readStatus == 2 && GZIPInputStream.GZIP_MAGIC == streamHeader;
}
示例7: unGzipBytesToString
import java.io.PushbackInputStream; //导入方法依赖的package包/类
public static String unGzipBytesToString(InputStream in) {
try {
PushbackInputStream pis = new PushbackInputStream(in, 2);
byte[] signature = new byte[2];
pis.read(signature);
pis.unread(signature);
int head = ((signature[0] & 0x00FF) | ((signature[1] << 8) & 0xFF00));
if (head != GZIPInputStream.GZIP_MAGIC) {
return new String(toByteArray(pis), "UTF-8").trim();
}
GZIPInputStream gzip = new GZIPInputStream(pis);
byte[] readBuf = new byte[8 * 1024];
ByteArrayOutputStream outputByte = new ByteArrayOutputStream();
int readCount = 0;
do {
readCount = gzip.read(readBuf);
if (readCount > 0) {
outputByte.write(readBuf, 0, readCount);
}
} while (readCount > 0);
closeQuietly(gzip);
closeQuietly(pis);
closeQuietly(in);
if (outputByte.size() > 0) {
return new String(outputByte.toByteArray());
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
示例8: detectEncoding
import java.io.PushbackInputStream; //导入方法依赖的package包/类
static private String detectEncoding(PushbackInputStream in) throws IOException {
String encoding = UTF8;
int b1 = in.read();
if (b1 != -1) {
int b2 = in.read();
if (b2 != -1) {
in.unread(b2);
if ((b1 == 0xFF && b2 == 0xFE) || (b1 == 0xFE && b2 == 0xFF))
encoding = UTF16;
}
in.unread(b1);
}
return encoding;
}
示例9: isInputStreamGZIPCompressed
import java.io.PushbackInputStream; //导入方法依赖的package包/类
/**
* Checks the InputStream if it contains GZIP compressed data
*
* @param inputStream InputStream to be checked
* @return true or false if the stream contains GZIP compressed data
* @throws java.io.IOException
*/
public static boolean isInputStreamGZIPCompressed(final PushbackInputStream inputStream) throws IOException {
if (inputStream == null)
return false;
byte[] signature = new byte[2];
int readStatus = inputStream.read(signature);
inputStream.unread(signature);
int streamHeader = ((int) signature[0] & 0xff) | ((signature[1] << 8) & 0xff00);
return readStatus == 2 && GZIPInputStream.GZIP_MAGIC == streamHeader;
}
示例10: parse
import java.io.PushbackInputStream; //导入方法依赖的package包/类
public void parse(InputStream stream,
ContentHandler handler, Metadata metadata,
ParseContext parseContext) throws IOException, SAXException,
TikaException
{
byte[] initial4 = new byte[4];
InputStream wrapped;
// Preserve TikaInputStreams as TikaInputStreams as they require less memory to process
if (stream.markSupported())
{
stream.mark(initial4.length);
IOUtils.readFully(stream, initial4);
stream.reset();
wrapped = stream;
}
else
{
PushbackInputStream inp = new PushbackInputStream(stream, 4);
IOUtils.readFully(inp, initial4);
inp.unread(initial4);
wrapped = inp;
}
// Which is it?
if(initial4[0] == POIFSConstants.OOXML_FILE_HEADER[0] &&
initial4[1] == POIFSConstants.OOXML_FILE_HEADER[1] &&
initial4[2] == POIFSConstants.OOXML_FILE_HEADER[2] &&
initial4[3] == POIFSConstants.OOXML_FILE_HEADER[3])
{
ooxmlParser.parse(wrapped, handler, metadata, parseContext);
}
else
{
ole2Parser.parse(wrapped, handler, metadata, parseContext);
}
}
示例11: readLine
import java.io.PushbackInputStream; //导入方法依赖的package包/类
public static String readLine(PushbackInputStream in) throws IOException {
char buf[] = new char[128];
int room = buf.length;
int offset = 0;
int c;
loop: while (true) {
switch (c = in.read()) {
case -1:
case '\n':
break loop;
case '\r':
int c2 = in.read();
if ((c2 != '\n') && (c2 != -1)) in.unread(c2);
break loop;
default:
if (--room < 0) {
char[] lineBuffer = buf;
buf = new char[offset + 128];
room = buf.length - offset - 1;
System.arraycopy((Object)lineBuffer, 0, (Object)buf, 0, offset);
}
buf[offset++] = (char) c;
break;
}
}
if ((c == -1) && (offset == 0)) return null;
return String.copyValueOf(buf, 0, offset);
}
示例12: UnicodeInputStreamReader
import java.io.PushbackInputStream; //导入方法依赖的package包/类
public UnicodeInputStreamReader(InputStream source, String encoding) throws IOException
{
defaultEnc = encoding;
String enc = encoding;
byte[] data = new byte[4];
PushbackInputStream pbStream = new PushbackInputStream(source, data.length);
int read = pbStream.read(data, 0, data.length);
int size = 0;
int bom16 = (data[0] & 0xFF) << 8 | (data[1] & 0xFF);
int bom24 = bom16 << 8 | (data[2] & 0xFF);
int bom32 = bom24 << 8 | (data[3] & 0xFF);
if (bom24 == 0xEFBBBF)
{
enc = "UTF-8";
size = 3;
}
else if (bom16 == 0xFEFF)
{
enc = "UTF-16BE";
size = 2;
}
else if (bom16 == 0xFFFE)
{
enc = "UTF-16LE";
size = 2;
}
else if (bom32 == 0x0000FEFF)
{
enc = "UTF-32BE";
size = 4;
}
else if (bom32 == 0xFFFE0000) //This will never happen as it'll be caught by UTF-16LE,
{ //but if anyone ever runs across a 32LE file, i'd like to dissect it.
enc = "UTF-32LE";
size = 4;
}
if (size < read)
{
pbStream.unread(data, size, read - size);
}
this.input = new InputStreamReader(pbStream, enc);
}
示例13: engineGenerateCertificate
import java.io.PushbackInputStream; //导入方法依赖的package包/类
/**
* Generates a certificate object and initializes it with the data
* read from the input stream inStream.
*/
public java.security.cert.Certificate engineGenerateCertificate(
InputStream in)
throws CertificateException
{
if (currentStream == null)
{
currentStream = in;
sData = null;
sDataObjectCount = 0;
}
else if (currentStream != in) // reset if input stream has changed
{
currentStream = in;
sData = null;
sDataObjectCount = 0;
}
try
{
if (sData != null)
{
if (sDataObjectCount != sData.size())
{
return getCertificate();
}
else
{
sData = null;
sDataObjectCount = 0;
return null;
}
}
PushbackInputStream pis = new PushbackInputStream(in);
int tag = pis.read();
if (tag == -1)
{
return null;
}
pis.unread(tag);
if (tag != 0x30) // assume ascii PEM encoded.
{
return readPEMCertificate(pis);
}
else
{
return readDERCertificate(new ASN1InputStream(pis));
}
}
catch (Exception e)
{
throw new ExCertificateException(e);
}
}
示例14: decodeBufferPrefix
import java.io.PushbackInputStream; //导入方法依赖的package包/类
/**
* For uuencoded buffers, the data begins with a line of the form:
* begin MODE FILENAME
* This line always starts in column 1.
*/
protected void decodeBufferPrefix(PushbackInputStream inStream, OutputStream outStream) throws IOException {
int c;
StringBuffer q = new StringBuffer(32);
String r;
boolean sawNewLine;
/*
* This works by ripping through the buffer until it finds a 'begin'
* line or the end of the buffer.
*/
sawNewLine = true;
while (true) {
c = inStream.read();
if (c == -1) {
throw new CEFormatException("UUDecoder: No begin line.");
}
if ((c == 'b') && sawNewLine){
c = inStream.read();
if (c == 'e') {
break;
}
}
sawNewLine = (c == '\n') || (c == '\r');
}
/*
* Now we think its begin, (we've seen ^be) so verify it here.
*/
while ((c != '\n') && (c != '\r')) {
c = inStream.read();
if (c == -1) {
throw new CEFormatException("UUDecoder: No begin line.");
}
if ((c != '\n') && (c != '\r')) {
q.append((char)c);
}
}
r = q.toString();
if (r.indexOf(' ') != 3) {
throw new CEFormatException("UUDecoder: Malformed begin line.");
}
mode = Integer.parseInt(r.substring(4,7));
bufferName = r.substring(r.indexOf(' ',6)+1);
/*
* Check for \n after \r
*/
if (c == '\r') {
c = inStream.read ();
if ((c != '\n') && (c != -1))
inStream.unread (c);
}
}
示例15: stream2reader
import java.io.PushbackInputStream; //导入方法依赖的package包/类
/**
* Converts a stream to a reader while detecting the encoding.
*
* @param stream the input for the XML data.
* @param charsRead buffer where to put characters that have been read
*
* @throws java.io.IOException
* if an I/O error occurred
*/
protected Reader stream2reader(InputStream stream,
StringBuffer charsRead)
throws IOException
{
PushbackInputStream pbstream = new PushbackInputStream(stream);
int b = pbstream.read();
switch (b) {
case 0x00:
case 0xFE:
case 0xFF:
pbstream.unread(b);
return new InputStreamReader(pbstream, "UTF-16");
case 0xEF:
for (int i = 0; i < 2; i++) {
pbstream.read();
}
return new InputStreamReader(pbstream, "UTF-8");
case 0x3C:
b = pbstream.read();
charsRead.append('<');
while ((b > 0) && (b != 0x3E)) {
charsRead.append((char) b);
b = pbstream.read();
}
if (b > 0) {
charsRead.append((char) b);
}
String encoding = this.getEncoding(charsRead.toString());
if (encoding == null) {
return new InputStreamReader(pbstream, "UTF-8");
}
charsRead.setLength(0);
try {
return new InputStreamReader(pbstream, encoding);
} catch (UnsupportedEncodingException e) {
return new InputStreamReader(pbstream, "UTF-8");
}
default:
charsRead.append((char) b);
return new InputStreamReader(pbstream, "UTF-8");
}
}