Refine search
/** * Tries once to read ahead in the stream to fill the context and * resets the stream to its position before the call. */ private byte[] tryReadAhead(BufferedInputStream stream, ByteBuffer haystack, int offset, int fileLength, int bytesRead) throws IOException { int numExpected = Math.min(fileLength - bytesRead, GREP_CONTEXT_SIZE); byte[] afterBytes = new byte[numExpected]; stream.mark(numExpected); // Only try reading once. stream.read(afterBytes, 0, numExpected); stream.reset(); return afterBytes; }
private static Reader getUTF8Reader(InputStream f) throws IOException { BufferedInputStream bis = new BufferedInputStream(f); assert bis.markSupported(); bis.mark(3); boolean reset = true; byte[] t = new byte[3]; bis.read(t); if (t[0] == ((byte) 0xef) && t[1] == ((byte) 0xbb) && t[2] == ((byte) 0xbf)) { reset = false; } if (reset) { bis.reset(); } return new InputStreamReader(bis, "UTF-8"); }
private byte[] readBuffer() throws IOException { stream = new BufferedInputStream(Files.newInputStream(filePath), BYTES_TO_DECODE * 2); stream.mark(BYTES_TO_DECODE); byte[] buf = new byte[BYTES_TO_DECODE]; int read = IOUtils.read(stream, buf, 0, BYTES_TO_DECODE); stream.reset(); stream.mark(-1); return Arrays.copyOf(buf, read); }
/** * Determines if the the input stream is xml if it is, use create properties loaded from xml * format, otherwise create properties from default format. * * @param in * @throws IOException */ public static Properties loadUniversal(InputStream in) throws IOException { final String xmlDeclarationStart = "<?xml"; BufferedInputStream bin = new BufferedInputStream(in); bin.mark(4096); BufferedReader reader = new BufferedReader(new InputStreamReader(bin)); String line = reader.readLine(); boolean isXML = line.startsWith(xmlDeclarationStart); bin.reset(); Properties props = new Properties(); if (isXML) props.loadFromXML(bin); else props.load(bin); return props; }
private static String getXMLGuessEncoding( BufferedInputStream is ) throws IOException { String encoding = null; int[] bytes = new int[4]; is.mark( 4 ); bytes[0] = is.read(); bytes[1] = is.read(); bytes[2] = is.read(); bytes[3] = is.read(); is.reset(); if ( bytes[0] == 0x00 && bytes[1] == 0x3C && bytes[2] == 0x00 && bytes[3] == 0x3F ) { encoding = UTF_16BE; } else if ( bytes[0] == 0x3C && bytes[1] == 0x00 && bytes[2] == 0x3F && bytes[3] == 0x00 ) { encoding = UTF_16LE; } else if ( bytes[0] == 0x3C && bytes[1] == 0x3F && bytes[2] == 0x78 && bytes[3] == 0x6D ) { encoding = UTF_8; } else if ( bytes[0] == 0x4C && bytes[1] == 0x6F && bytes[2] == 0xA7 && bytes[3] == 0x94 ) { encoding = EBCDIC; } return encoding; }
/** * Determine the content generator (i.e. Slic3r, Skeinforge) for the given BufferedReader. * * @param buffer * @return * @throws IOException */ public static final GCodeFlavor tasteFlavor(BufferedInputStream buffer) throws IOException { int rl = 200; final byte[] readAhead = new byte[rl]; buffer.mark(readAhead.length); int i = 0; while (buffer.available() > 0 && i < rl) { readAhead[i] = (byte) buffer.read(); i++; } buffer.reset(); final String readAheadString = new String(readAhead).toLowerCase(Locale.US); // check for slic3r header if (readAheadString.contains("generated by slic3r")) { return GCodeFlavor.SLIC3R; } else if (readAheadString.contains("skeinforge")) { return GCodeFlavor.SKEINFORGE; } // don't know if it's slic3r generated return GCodeFlavor.UNKNOWN; } }
private static String getXMLGuessEncoding(final BufferedInputStream is) throws IOException { String encoding = null; final int[] bytes = new int[4]; is.mark(4); bytes[0] = is.read(); bytes[1] = is.read(); bytes[2] = is.read(); bytes[3] = is.read(); is.reset(); if (bytes[0] == 0x00 && bytes[1] == 0x3C && bytes[2] == 0x00 && bytes[3] == 0x3F) { encoding = UTF_16BE; } else if (bytes[0] == 0x3C && bytes[1] == 0x00 && bytes[2] == 0x3F && bytes[3] == 0x00) { encoding = UTF_16LE; } else if (bytes[0] == 0x3C && bytes[1] == 0x3F && bytes[2] == 0x78 && bytes[3] == 0x6D) { encoding = UTF_8; } else if (bytes[0] == 0x4C && bytes[1] == 0x6F && bytes[2] == 0xA7 && bytes[3] == 0x94) { encoding = EBCDIC; } return encoding; }