@Override public FileChannel open(String mode) throws IOException { InputStream in = newInputStream(); FilePath copy = FilePath.get(getBase().toString() + ".copy"); OutputStream out = copy.newOutputStream(false); IOUtils.copy(in, out); FileChannel base = getBase().open(mode); FileChannel readBase = copy.open(mode); return new FileReorderWrites(this, base, readBase); }
// download arquivo @RequestMapping(value = "/{id}/download", method = RequestMethod.GET) public void getFile(@PathVariable("id") Long id, HttpServletResponse response) { try { Documento documento = serviceDocumento.find(Documento.class, id); if (documento != null) { InputStream is = new ByteArrayInputStream(documento.getArquivo()); response.setContentType(documento.getTipo()); response.setHeader( "Content-Disposition", "attachment; filename=" + documento.getNomeOriginal().replace(" ", "_")); IOUtils.copy(is, response.getOutputStream()); response.flushBuffer(); } } catch (IOException ex) { throw new RuntimeException("IOError writing file to output stream"); } }
private void uploadMultipart(InputStream in, int len) throws IOException { if (!new File(WebServer.TRANSFER).exists()) { return; } String fileName = "temp.bin"; headerBytes = 0; String boundary = readHeaderLine(); while (true) { String line = readHeaderLine(); if (line == null) { break; } int index = line.indexOf("filename=\""); if (index > 0) { fileName = line.substring(index + "filename=\"".length(), line.lastIndexOf('"')); } trace(" " + line); } if (!WebServer.isSimpleName(fileName)) { return; } len -= headerBytes; File file = new File(WebServer.TRANSFER, fileName); OutputStream out = new FileOutputStream(file); IOUtils.copy(in, out, len); out.close(); // remove the boundary RandomAccessFile f = new RandomAccessFile(file, "rw"); int testSize = (int) Math.min(f.length(), Constants.IO_BUFFER_SIZE); f.seek(f.length() - testSize); byte[] bytes = DataUtils.newBytes(Constants.IO_BUFFER_SIZE); f.readFully(bytes, 0, testSize); String s = new String(bytes, "ASCII"); int x = s.lastIndexOf(boundary); f.setLength(f.length() - testSize + x - 2); f.close(); }
/** * Restores database files. * * @param zipFileName the name of the backup file * @param directory the directory name * @param db the database name (null for all databases) * @throws DbException if there is an IOException */ public static void execute(String zipFileName, String directory, String db) { InputStream in = null; try { if (!FileUtils.exists(zipFileName)) { throw new IOException("File not found: " + zipFileName); } String originalDbName = null; int originalDbLen = 0; if (db != null) { originalDbName = getOriginalDbName(zipFileName, db); if (originalDbName == null) { throw new IOException("No database named " + db + " found"); } if (originalDbName.startsWith(SysProperties.FILE_SEPARATOR)) { originalDbName = originalDbName.substring(1); } originalDbLen = originalDbName.length(); } in = FileUtils.newInputStream(zipFileName); ZipInputStream zipIn = new ZipInputStream(in); while (true) { ZipEntry entry = zipIn.getNextEntry(); if (entry == null) { break; } String fileName = entry.getName(); // restoring windows backups on linux and vice versa fileName = fileName.replace('\\', SysProperties.FILE_SEPARATOR.charAt(0)); fileName = fileName.replace('/', SysProperties.FILE_SEPARATOR.charAt(0)); if (fileName.startsWith(SysProperties.FILE_SEPARATOR)) { fileName = fileName.substring(1); } boolean copy = false; if (db == null) { copy = true; } else if (fileName.startsWith(originalDbName + ".")) { fileName = db + fileName.substring(originalDbLen); copy = true; } if (copy) { OutputStream o = null; try { o = FileUtils.newOutputStream( directory + SysProperties.FILE_SEPARATOR + fileName, false); IOUtils.copy(zipIn, o); o.close(); } finally { IOUtils.closeSilently(o); } } zipIn.closeEntry(); } zipIn.closeEntry(); zipIn.close(); } catch (IOException e) { throw DbException.convertIOException(e, zipFileName); } finally { IOUtils.closeSilently(in); } }
private void test(StreamStore store, int minBlockSize, int maxBlockSize, int length) throws IOException { store.setMinBlockSize(minBlockSize); assertEquals(minBlockSize, store.getMinBlockSize()); store.setMaxBlockSize(maxBlockSize); assertEquals(maxBlockSize, store.getMaxBlockSize()); long next = store.getNextKey(); Random r = new Random(length); byte[] data = new byte[length]; r.nextBytes(data); byte[] id = store.put(new ByteArrayInputStream(data)); if (length > 0 && length >= minBlockSize) { assertFalse(store.isInPlace(id)); } else { assertTrue(store.isInPlace(id)); } long next2 = store.getNextKey(); if (length > 0 && length >= minBlockSize) { assertTrue(next2 > next); } else { assertEquals(next, next2); } if (length == 0) { assertEquals(0, id.length); } assertEquals(length, store.length(id)); InputStream in = store.get(id); ByteArrayOutputStream out = new ByteArrayOutputStream(); IOUtils.copy(in, out); assertTrue(Arrays.equals(data, out.toByteArray())); in = store.get(id); in.close(); assertEquals(-1, in.read()); in = store.get(id); assertEquals(0, in.skip(0)); if (length > 0) { assertEquals(1, in.skip(1)); if (length > 1) { assertEquals(data[1] & 255, in.read()); if (length > 2) { assertEquals(1, in.skip(1)); if (length > 3) { assertEquals(data[3] & 255, in.read()); } } else { assertEquals(0, in.skip(1)); } } else { assertEquals(-1, in.read()); } } else { assertEquals(0, in.skip(1)); } if (length > 12) { in = store.get(id); assertEquals(12, in.skip(12)); assertEquals(data[12] & 255, in.read()); long skipped = 0; while (true) { long s = in.skip(Integer.MAX_VALUE); if (s == 0) { break; } skipped += s; } assertEquals(length - 13, skipped); assertEquals(-1, in.read()); } store.remove(id); assertEquals(0, store.getMap().size()); }