/** * Checks the start of the file for ASCII control characters */ public static boolean isBinary(final File file) throws IOException { byte[] bytes = readBytes(file, 128); for (byte b : bytes) { if (b < 32) { return true; } } return false; } }
public static byte[] readBytes(File file) throws IOException { return readBytes(file, -1); } public static byte[] readBytes(File file, int fixedLength) throws IOException {
/** * @see #readBytes(File, int) */ public static byte[] readBytes(final File file) throws IOException { return readBytes(file, NEGATIVE_ONE); }
/** * Returns files content from disk file. * If error occurs, it returns <code>null</code> */ @Override public byte[] getFileContent() throws IOException { return FileUtil.readBytes(file); }
/** * Returns the content of file upload item. */ @Override public byte[] getFileContent() throws IOException { if (data != null) { return data; } if (tempFile != null) { return FileUtil.readBytes(tempFile); } return null; }
/** * Checks the start of the file for ASCII control characters * * @param file {@link File} * @return true if the the start of the {@link File} is ASCII control characters. */ public static boolean isBinary(final File file) throws IOException { byte[] bytes = readBytes(file, 128); for (byte b : bytes) { if (b < 32 && b != 9 && b != 10 && b != 13) { return true; } } return false; }
public byte[] toBytes() { StreamUtil.close(zos); if (targetZipFile != null) { try { return FileUtil.readBytes(targetZipFile); } catch (IOException ignore) { return null; } } return targetBaos.toByteArray(); }
public static byte[] readBytes(String file) throws IOException { return readBytes(file(file)); }
@Override public byte[] getBytes() { try { return FileUtil.readBytes(file); } catch (IOException ioex) { throw new HttpException(ioex); } }
/** * @see #readBytes(File) */ public static byte[] readBytes(final String file) throws IOException { return readBytes(file(file)); }
public byte[] toBytes() { StreamUtil.close(zos); if (targetZipFile != null) { try { return FileUtil.readBytes(targetZipFile); } catch (IOException ignore) { return null; } } return targetBaos.toByteArray(); }
/** * Returns cached file bytes. */ public byte[] getFileBytes(File file) throws IOException { byte[] bytes = cache.get(file); if (bytes != null) { return bytes; } // add file bytes = FileUtil.readBytes(file); if ((maxFileSize != 0) && (file.length() > maxFileSize)) { // don't cache files that size exceed max allowed file size return bytes; } usedSize += bytes.length; // put file into cache // if used size > total, purge() will be invoked cache.put(file, bytes); return bytes; }
/** * Returns cached file bytes. If file is not cached it will be * read and put in the cache (if all the rules are satisfied). */ public byte[] getFileBytes(final File file) throws IOException { byte[] bytes = cache.get(file); if (bytes != null) { return bytes; } // add file bytes = FileUtil.readBytes(file); if ((maxFileSize != 0) && (file.length() > maxFileSize)) { // don't cache files that size exceed max allowed file size return bytes; } usedSize += bytes.length; // put file into cache // if used size > total, purge() will be invoked cache.put(file, bytes); return bytes; }
return FileUtil.readBytes((File) value); } catch (IOException ioex) { throw new TypeConversionException(value, ioex);
return FileUtil.readBytes((File) value); } catch (IOException ioex) { throw new TypeConversionException(value, ioex);
@Override public boolean serialize(final JsonContext jsonContext, final File file) { switch (serializationType) { case PATH: jsonContext.writeString(file.getAbsolutePath()); break; case NAME: jsonContext.writeString(file.getName()); break; case CONTENT: { byte[] bytes; try { bytes = FileUtil.readBytes(file); } catch (IOException e) { throw new JsonException("Unable to read files content", e); } String encoded = Base64.encodeToString(bytes); jsonContext.writeString(encoded); } break; default: throw new JsonException("Invalid type"); } return true; } }
@Test void testGzip() throws IOException { ZipUtil.gzip(new File(dataRoot, "sb.data")); File gzipFile = new File(dataRoot, "sb.data.gz"); assertTrue(gzipFile.exists()); FileUtil.move(gzipFile, new File(dataRoot, "sb2.data.gz")); ZipUtil.ungzip(new File(dataRoot, "sb2.data.gz")); File data = new File(dataRoot, "sb2.data"); assertTrue(data.exists()); byte[] data2Bytes = FileUtil.readBytes(data); byte[] data1Bytes = FileUtil.readBytes(new File(dataRoot, "sb.data")); assertTrue(Arrays.equals(data1Bytes, data2Bytes)); // cleanup FileUtil.delete(new File(dataRoot, "sb2.data")); FileUtil.delete(new File(dataRoot, "sb2.data.gz")); }
@Test void testNoContentLength() throws IOException { URL data = RawTest.class.getResource("3-response.txt"); byte[] fileContent = FileUtil.readBytes(data.getFile()); HttpResponse httpResponse = HttpResponse.readFrom(new ByteArrayInputStream(fileContent)); assertEquals("Body!", httpResponse.bodyText()); }
@Test void testBytes() { try { File file = new File(dataRoot + "/file/a.txt"); byte[] bytes = FileUtil.readBytes(dataRoot + "/file/a.txt"); assertEquals(file.length(), bytes.length); String content = new String(bytes); content = StringUtil.remove(content, '\r'); assertEquals("test file\n", content); } catch (IOException ioex) { fail(ioex.toString()); } }
@Test void testNoBody() throws IOException { /*HttpResponse httpResponse = HttpRequest.get("http://maps.googleapis.com/maps/api/geocode/json") .query("address", "14621") .query("sensor", "false") .send(); */ URL data = RawTest.class.getResource("2-response.txt"); byte[] fileContent = FileUtil.readBytes(data.getFile()); HttpResponse httpResponse = HttpResponse.readFrom(new ByteArrayInputStream(fileContent)); try { httpResponse.bodyText(); } catch (Exception ex) { fail(ex.toString()); } assertEquals("", httpResponse.bodyText()); }