public static boolean isZipStream(InputStream in) throws IOException { in.mark(MAGIC_NUMBER.length); byte[] fileHeader = IOUtil.readBytes(in, MAGIC_NUMBER.length); in.reset(); return Arrays.equals(MAGIC_NUMBER, fileHeader); }
public static boolean isGZipStream(InputStream in) throws IOException { in.mark(MAGIC_NUMBER.length); byte[] fileHeader = IOUtil.readBytes(in, MAGIC_NUMBER.length); in.reset(); return Arrays.equals(MAGIC_NUMBER, fileHeader); } }
private void prepareForWire() throws Exception { if (file != null) { // set the data data = IOUtil.readBytes(file); } else if (stmts != null) { // set the data and content type (TRIG by default) format = RDFFormat.TRIG; data = serialize(stmts, format); } }
/** * Reads at most <tt>maxBytes</tt> bytes from the supplied input stream and * returns them as a byte array. * * @param in * The InputStream supplying the bytes. * @param maxBytes * The maximum number of bytes to read from the input stream. * @return A byte array of size <tt>maxBytes</tt> if the input stream can * produce that amount of bytes, or a smaller byte array containing * all available bytes from the stream otherwise. */ public static byte[] readBytes(InputStream in, int maxBytes) throws IOException { byte[] result = new byte[maxBytes]; int bytesRead = readBytes(in, result); if (bytesRead < maxBytes) { // Create smaller byte array byte[] tmp = new byte[bytesRead]; System.arraycopy(result, 0, tmp, 0, bytesRead); result = tmp; } return result; }
private void prepareForWire() throws Exception { if (file != null) { // set the data data = IOUtil.readBytes(file); } else if (is != null) { // set the data data = IOUtil.readBytes(is); } else if (reader != null) { // set the data data = IOUtil.readString(reader).getBytes(); } else if (stmts != null) { // set the data and content type (TRIG by default) format = RDFFormat.TRIG; data = serialize(stmts, format); } }
/** * Reads all bytes from the specified file and returns them as a byte array. * * @param file * The file to read. * @return A byte array containing all bytes from the specified file. * * @throws IOException * If an I/O error occurred while reading from the file. * @throws IllegalArgumentException * If the file size exceeds the maximum array length (larger than * {@link Integer#MAX_VALUE}. */ public static byte[] readBytes(File file) throws IOException { long fileSize = file.length(); if (fileSize > Integer.MAX_VALUE) { throw new IllegalArgumentException("File size exceeds maximum array length (" + fileSize + " > " + Integer.MAX_VALUE + ")"); } FileInputStream in = new FileInputStream(file); try { return readBytes(in, (int)fileSize); } finally { in.close(); } }
private void execute(final EmacsFunction f, final String text) throws IOException, InterruptedException, ExecutionException { Process p = functionExecutor.execute(f, text); if (null != p && 0 != p.exitValue()) { StringBuilder sb = new StringBuilder(); sb.append("failed to execute emacs function ") .append(f.getName()).append(f.requiresArgument ? " with argument " + text : "") .append(" (exit code = ").append(p.exitValue()).append(")."); byte[] in = IOUtil.readBytes(p.getInputStream()); if (in.length > 0) { sb.append("\nInput: "); sb.append(new String(in)); } byte[] out = IOUtil.readBytes(p.getErrorStream()); if (out.length > 0) { sb.append("\nOutput: "); sb.append(new String(out)); } throw new ExecutionException(sb.toString()); } }
private void execute(final EmacsFunction f, final String text, final boolean doHandle) throws IOException, InterruptedException, ExecutionException { Process p = functionExecutor.execute(f, text); if (null == p) { return; } if (0 != p.exitValue()) { StringBuilder sb = new StringBuilder(); sb.append("failed to execute emacs function ") .append(f.getName()).append(f.requiresArgument ? " with argument " + text : "") .append(" (exit code = ").append(p.exitValue()).append(")."); byte[] in = IOUtil.readBytes(p.getInputStream()); if (in.length > 0) { sb.append("\nInput: "); sb.append(new String(in)); } byte[] out = IOUtil.readBytes(p.getErrorStream()); if (out.length > 0) { sb.append("\nOutput: "); sb.append(new String(out)); } throw new ExecutionException(sb.toString()); } else if (doHandle) { try { resultHandler.handle(p.getInputStream()); } catch (Throwable t) { logger.log(Level.SEVERE, "failed to handle Brain-mode client result", t); } } }
final byte[] data = IOUtil.readBytes(file);
String s0; try { s0 = new String(IOUtil.readBytes(result)).trim(); } catch (IOException e) { logger.log(Level.SEVERE, "error reading Brain-mode response", e);
byte[] magicNumber = IOUtil.readBytes(in, MAGIC_NUMBER.length); if (!Arrays.equals(magicNumber, MAGIC_NUMBER)) { reportFatalError("File does not contain a binary RDF document");