本文整理汇总了Java中java.io.BufferedInputStream.mark方法的典型用法代码示例。如果您正苦于以下问题:Java BufferedInputStream.mark方法的具体用法?Java BufferedInputStream.mark怎么用?Java BufferedInputStream.mark使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类java.io.BufferedInputStream
的用法示例。
在下文中一共展示了BufferedInputStream.mark方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: extractRequestEntity
import java.io.BufferedInputStream; //导入方法依赖的package包/类
static String extractRequestEntity(ContainerRequestContext request) {
if (request.hasEntity()) {
InputStream inputStreamOriginal = request.getEntityStream();
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStreamOriginal, MAX_ENTITY_READ);
bufferedInputStream.mark(MAX_ENTITY_READ);
byte[] bytes = new byte[MAX_ENTITY_READ];
int read;
try {
read = bufferedInputStream.read(bytes, 0, MAX_ENTITY_READ);
bufferedInputStream.reset();
} catch (IOException e) {
throw new RuntimeException(e);
}
request.setEntityStream(bufferedInputStream);
return new String(bytes, Charsets.UTF_8);
}
return null;
}
示例2: decodeBitmap
import java.io.BufferedInputStream; //导入方法依赖的package包/类
private static Bitmap decodeBitmap(InputStream inputStream, Rect rect) {
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.RGB_565;
if (rect != null) {
options.inJustDecodeBounds = true;
bufferedInputStream.mark(MARK_POSITION);
BitmapFactory.decodeStream(bufferedInputStream, null, options);
try {
bufferedInputStream.reset();
} catch (IOException e) {
Debug.e(e);
}
options.inJustDecodeBounds = false;
options.inSampleSize = AbstractImageLoader.getSampleSize(options.outWidth, options.outHeight, rect.width(), rect.height());
}
return BitmapFactory.decodeStream(bufferedInputStream, null, options);
}
示例3: load
import java.io.BufferedInputStream; //导入方法依赖的package包/类
public void load(TGSongLoaderHandle handle) throws TGFileFormatException{
try{
BufferedInputStream stream = new BufferedInputStream(handle.getInputStream());
stream.mark(1);
Iterator<TGInputStreamBase> it = TGFileFormatManager.getInstance(this.context).getInputStreams();
while(it.hasNext() && handle.getSong() == null){
TGInputStreamBase reader = (TGInputStreamBase)it.next();
reader.init(handle.getFactory(),stream);
if( reader.isSupportedVersion() ){
handle.setSong(reader.readSong());
handle.setFormat(reader.getFileFormat());
return;
}
stream.reset();
}
stream.close();
} catch(Throwable throwable) {
throw new TGFileFormatException(throwable);
}
throw new TGFileFormatException("Unsupported file format");
}
示例4: testNegative
import java.io.BufferedInputStream; //导入方法依赖的package包/类
private void testNegative(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); // 1 byte short in buffer size
// read 30 bytes in total, with internal buffer incread to 20
bis.read(new byte[20]);
try {
bis.reset();
fail();
} catch(IOException ex) {
assertEquals("Resetting to invalid mark", ex.getMessage());
}
}
示例5: loadImage
import java.io.BufferedInputStream; //导入方法依赖的package包/类
/**
* @see org.newdawn.slick.opengl.LoadableImageData#loadImage(java.io.InputStream, boolean, boolean, int[])
*/
public ByteBuffer loadImage(InputStream is, boolean flipped, boolean forceAlpha, int[] transparent) throws IOException {
CompositeIOException exception = new CompositeIOException();
ByteBuffer buffer = null;
BufferedInputStream in = new BufferedInputStream(is, is.available());
in.mark(is.available());
// cycle through our source until one of them works
for (int i=0;i<sources.size();i++) {
in.reset();
try {
LoadableImageData data = (LoadableImageData) sources.get(i);
buffer = data.loadImage(in, flipped, forceAlpha, transparent);
picked = data;
break;
} catch (Exception e) {
Log.warn(sources.get(i).getClass()+" failed to read the data", e);
exception.addException(e);
}
}
if (picked == null) {
throw exception;
}
return buffer;
}
示例6: loadXml
import java.io.BufferedInputStream; //导入方法依赖的package包/类
@ApiMethod
public static Document loadXml(InputStream stream)
{
String docText = null;
try
{
int buffSize = 2048;
BufferedInputStream buff = new BufferedInputStream(stream, buffSize);
byte[] debug = new byte[buffSize];
buff.mark(buffSize);
buff.read(debug);
docText = new String(debug);
docText = docText.trim();
buff.reset();
stream = buff;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(stream);
doc.getDocumentElement().normalize();
return doc;
}
catch (Exception ex)
{
String msg = "Unable to parse document: " + ex.getMessage();
if (docText != null)
msg += "\r\n" + docText;
throw new RuntimeException(msg);
}
}
示例7: Gpx2Fit
import java.io.BufferedInputStream; //导入方法依赖的package包/类
public Gpx2Fit(String name, InputStream in, Gpx2FitOptions options) throws Exception {
mGpx2FitOptions = options;
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
factory.setNamespaceAware(true);
XmlPullParser parser = factory.newPullParser();
//parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
courseName = name;
BufferedInputStream inputStream = new BufferedInputStream(in);
inputStream.mark(64000);
//FileInputStream in = new FileInputStream(file);
try {
parser.setInput(inputStream, null);
parser.nextTag();
readGPX(parser);
inputStream.close();
return;
} catch (Exception e) {
ns = HTTP_WWW_TOPOGRAFIX_COM_GPX_1_0;
Log.debug("Ex {}", e);
}
inputStream.reset();
try {
parser.setInput(inputStream, null);
parser.nextTag();
readGPX(parser);
} finally {
inputStream.close();
}
}
示例8: maybeDecompress
import java.io.BufferedInputStream; //导入方法依赖的package包/类
private static InputStream maybeDecompress(InputStream input) throws IOException {
// Due to a bug, Jira sometimes returns double-compressed responses. See JRA-37608
BufferedInputStream buffered = new BufferedInputStream(input, 2);
buffered.mark(2);
int[] buf = new int[2];
buf[0] = buffered.read();
buf[1] = buffered.read();
buffered.reset();
int header = (buf[1] << 8) | buf[0];
if (header == GZIPInputStream.GZIP_MAGIC) {
return new GZIPInputStream(buffered);
} else {
return buffered;
}
}
示例9: accept
import java.io.BufferedInputStream; //导入方法依赖的package包/类
/**
* 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: readMagic
import java.io.BufferedInputStream; //导入方法依赖的package包/类
static byte[] readMagic(BufferedInputStream in) throws IOException {
in.mark(4);
byte[] magic = new byte[4];
for (int i = 0; i < magic.length; i++) {
// read 1 byte at a time, so we always get 4
if (1 != in.read(magic, i, 1))
break;
}
in.reset();
return magic;
}
示例11: JavaSoundAudioClip
import java.io.BufferedInputStream; //导入方法依赖的package包/类
public JavaSoundAudioClip(InputStream in) throws IOException {
if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.<init>");
BufferedInputStream bis = new BufferedInputStream(in, STREAM_BUFFER_SIZE);
bis.mark(STREAM_BUFFER_SIZE);
boolean success = false;
try {
AudioInputStream as = AudioSystem.getAudioInputStream(bis);
// load the stream data into memory
success = loadAudioData(as);
if (success) {
success = false;
if (loadedAudioByteLength < CLIP_THRESHOLD) {
success = createClip();
}
if (!success) {
success = createSourceDataLine();
}
}
} catch (UnsupportedAudioFileException e) {
// not an audio file
try {
MidiFileFormat mff = MidiSystem.getMidiFileFormat(bis);
success = createSequencer(bis);
} catch (InvalidMidiDataException e1) {
success = false;
}
}
if (!success) {
throw new IOException("Unable to create AudioClip from input stream");
}
}
示例12: getFirstLine
import java.io.BufferedInputStream; //导入方法依赖的package包/类
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");
}
示例13: optimizeBitmap
import java.io.BufferedInputStream; //导入方法依赖的package包/类
/**
* optimize bitmap
* @param bitmap
* @return
*/
public static Bitmap optimizeBitmap(@NonNull Bitmap bitmap, int size) {
if (!bitmap.isRecycled()) {
// convert bitmap into inputStream
ByteArrayOutputStream stream = new ByteArrayOutputStream();
// bitmap compress
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
// bytes into array
InputStream is = new ByteArrayInputStream(stream.toByteArray());
BufferedInputStream imageFileStream = new BufferedInputStream(is);
try {
// Phase 1: Get a reduced size image. In this part we will do a rough scale down
int sampleSize = 1;
if (size > 0 && size > 0) {
final BitmapFactory.Options decodeBoundsOptions = new BitmapFactory.Options();
decodeBoundsOptions.inJustDecodeBounds = true;
imageFileStream.mark(64 * 1024);
BitmapFactory.decodeStream(imageFileStream, null, decodeBoundsOptions);
imageFileStream.reset();
final int originalWidth = decodeBoundsOptions.outWidth;
final int originalHeight = decodeBoundsOptions.outHeight;
// inSampleSize prefers multiples of 2, but we prefer to prioritize memory savings
sampleSize = Math.max(1, Math.max(originalWidth / size, originalHeight / size));
}
BitmapFactory.Options decodeBitmapOptions = new BitmapFactory.Options();
decodeBitmapOptions.inSampleSize = sampleSize;
//decodeBitmapOptions.inPreferredConfig = Bitmap.Config.ARGB_8888; // Uses 2-bytes instead of default 4 per pixel
// Get the roughly scaled-down image
Bitmap bmp = BitmapFactory.decodeStream(imageFileStream, null, decodeBitmapOptions);
// Phase 2: Get an exact-size image - no dimension will exceed the desired value
float ratio = Math.min((float) size / (float) bmp.getWidth(), (float) size / (float) bmp.getHeight());
int w = (int) ((float) bmp.getWidth() * ratio);
int h = (int) ((float) bmp.getHeight() * ratio);
// finally scaled bitmap
return Bitmap.createScaledBitmap(bmp, w, h, true);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
imageFileStream.close();
} catch (IOException ignored) {
}
}
return null;
} else {
return null;
}
}
示例14: getCharset
import java.io.BufferedInputStream; //导入方法依赖的package包/类
/**
* 判断文件编码
*
* @param file 文件
* @return 编码:GBK,UTF-8,UTF-16LE
*/
public String getCharset(File file) {
String charset = "GBK";
byte[] first3Bytes = new byte[3];
try {
boolean checked = false;
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)
break;
if (0xC0 <= read && read <= 0xDF) {
read = bis.read();
if (0x80 <= read && read <= 0xBF)
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;
}
示例15: InputStreamByteStream
import java.io.BufferedInputStream; //导入方法依赖的package包/类
/**
* Construct a new InputStreamByteStream wrapping an InputStream.
*
* @param input the input stream to wrap
*/
public InputStreamByteStream(final InputStream input) {
_inputStream = new BufferedInputStream(input, BUFFER_SIZE);
_inputStream.mark(Integer.MAX_VALUE);
}