List of usage examples for java.nio.channels FileChannel write
public final long write(ByteBuffer[] srcs) throws IOException
From source file:com.metamx.druid.index.v1.IndexMerger.java
public static void createIndexDrdFile(byte versionId, File inDir, GenericIndexed<String> availableDimensions, GenericIndexed<String> availableMetrics, Interval dataInterval) throws IOException { File indexFile = new File(inDir, "index.drd"); FileChannel channel = null; try {/*from w w w . j a v a 2 s .com*/ channel = new FileOutputStream(indexFile).getChannel(); channel.write(ByteBuffer.wrap(new byte[] { versionId })); availableDimensions.writeToChannel(channel); availableMetrics.writeToChannel(channel); serializerUtils.writeString(channel, String.format("%s/%s", dataInterval.getStart(), dataInterval.getEnd())); } finally { Closeables.closeQuietly(channel); channel = null; } IndexIO.checkFileSize(indexFile); }
From source file:org.solmix.commons.util.Files.java
/** * ?/*from w w w . j a v a 2s . c om*/ * * @param f1 * ? * @param f2 * * @throws Exception */ public static void copyFile(File f1, File f2) throws Exception { int length = 2097152; FileInputStream in = new FileInputStream(f1); FileOutputStream out = new FileOutputStream(f2); FileChannel inC = in.getChannel(); FileChannel outC = out.getChannel(); ByteBuffer b = null; while (true) { if (inC.position() == inC.size()) { inC.close(); outC.close(); } if ((inC.size() - inC.position()) < length) { length = (int) (inC.size() - inC.position()); } else length = 2097152; b = ByteBuffer.allocateDirect(length); inC.read(b); b.flip(); outC.write(b); outC.force(false); } }
From source file:org.lexgrid.loader.writer.NoClosingRootTagStaxEventItemWriter.java
@Override public void close() { super.close(); String rootTag = "</" + getRootTagName() + ">"; String xml = null;/*from www .j a va 2s .c o m*/ try { xml = FileUtils.readFileToString(xmlFile); } catch (IOException e) { throw new RuntimeException(e); } if (!xml.endsWith(rootTag)) { try { FileOutputStream os = new FileOutputStream(xmlFile, true); FileChannel channel = os.getChannel(); ByteBuffer byteBuffer = ByteBuffer.wrap(rootTag.getBytes()); channel.write(byteBuffer); channel.close(); xml = FileUtils.readFileToString(xmlFile); } catch (IOException ioe) { throw new RuntimeException(ioe); } } }
From source file:com.l2jfree.loginserver.L2LoginIdentifier.java
private synchronized void load() { if (isLoaded()) return;// w w w . ja v a 2 s. c o m File f = new File(System.getProperty("user.home", null), FILENAME); ByteBuffer bb = ByteBuffer.allocateDirect(8); if (!f.exists() || f.length() != 8) { _uid = getRandomUID(); _loaded = true; _log.info("A new UID has been generated for this login server."); FileOutputStream fos = null; try { f.createNewFile(); fos = new FileOutputStream(f); FileChannel fc = fos.getChannel(); bb.putLong(getUID()); bb.flip(); fc.write(bb); fos.flush(); } catch (IOException e) { _log.warn("Could not store login server's UID!", e); } finally { IOUtils.closeQuietly(fos); f.setReadOnly(); } } else { FileInputStream fis = null; try { fis = new FileInputStream(f); FileChannel fc = fis.getChannel(); fc.read(bb); } catch (IOException e) { _log.warn("Could not read stored login server's UID!", e); } finally { IOUtils.closeQuietly(fis); } if (bb.position() > 0) { bb.flip(); _uid = bb.getLong(); } else _uid = getRandomUID(); _loaded = true; } }
From source file:org.loadosophia.client.LoadosophiaAPIClient.java
protected String[] multipartPost(LinkedList<Part> parts, String URL, int expectedSC) throws IOException { log.debug("Request POST: " + URL); parts.add(new StringPart("token", token)); PostMethod postRequest = new PostMethod(URL); MultipartRequestEntity multipartRequest = new MultipartRequestEntity(parts.toArray(new Part[parts.size()]), postRequest.getParams());/*from www. ja v a 2 s. c o m*/ postRequest.setRequestEntity(multipartRequest); int result = httpClient.executeMethod(postRequest); if (result != expectedSC) { String fname = File.createTempFile("error_", ".html").getAbsolutePath(); notifier.notifyAbout("Saving server error response to: " + fname); FileOutputStream fos = new FileOutputStream(fname); FileChannel resultFile = fos.getChannel(); resultFile.write(ByteBuffer.wrap(postRequest.getResponseBody())); resultFile.close(); throw new HttpException("Request returned not " + expectedSC + " status code: " + result); } byte[] bytes = postRequest.getResponseBody(); if (bytes == null) { bytes = new byte[0]; } String response = new String(bytes); return response.trim().split(";"); }
From source file:org.mhisoft.common.util.FileUtils.java
/** * @param source/*from w w w .j av a2s .com*/ * @param target * @throws IOException */ public static void copyFile(final File source, final File target) throws IOException { FileChannel in = null; FileChannel out = null; // long totalFileSize = 0; try { in = new FileInputStream(source).getChannel(); out = new FileOutputStream(target).getChannel(); //totalFileSize = in.size(); ByteBuffer buffer = ByteBuffer.allocateDirect(BUFFER); int readSize = in.read(buffer); long totalRead = 0; //int progress = 0; //long startTime, endTime ; while (readSize != -1) { //startTime = System.currentTimeMillis(); totalRead = totalRead + readSize; //progress = (int) (totalRead * 100 / totalFileSize); buffer.flip(); while (buffer.hasRemaining()) { out.write(buffer); //System.out.printf("."); //showPercent(rdProUI, totalSize/size ); } buffer.clear(); readSize = in.read(buffer); //endTime = System.currentTimeMillis(); } } finally { close(in); close(out); } }
From source file:org.meerkat.util.FileUtil.java
/** * writeToFile//from w w w . j a va2 s. c o m * @param filename * @param contents */ public final void writeToFile(String filename, String contents) { RandomAccessFile destFile = null; File tmpFile; try { tmpFile = new File(filename); if (tmpFile.exists()) { tmpFile.delete(); } destFile = new RandomAccessFile(filename, "rw"); } catch (FileNotFoundException e) { log.error("Error accessing file: " + filename + " (" + e.getMessage() + ")"); } ByteBuffer buf = ByteBuffer.allocate(contents.length()); FileChannel outChannel = destFile.getChannel(); buf.put(contents.getBytes()); buf.flip(); //buffer set for read try { outChannel.write(buf); destFile.close(); } catch (IOException e) { log.error("Error writing to file " + filename + " (" + e.getMessage() + ")"); } }
From source file:com.sm.store.utils.FileStore.java
private boolean checkSignature(FileChannel channel) throws IOException { ByteBuffer intBytes = ByteBuffer.allocate(OFFSET); if (channel.size() == 0) { intBytes.putInt(MAGIC);/*from w w w .j a v a 2 s . co m*/ intBytes.flip(); channel.write(intBytes); return true; } else { channel.read(intBytes); intBytes.rewind(); if (intBytes.getInt() != MAGIC) throw new StoreException("Header mismatch expect " + MAGIC + " read " + intBytes.getInt()); } return true; }
From source file:cn.wanghaomiao.seimi.struct.Response.java
public void saveTo(File targetFile) { FileChannel fo = null; try {//from w ww . ja va2s.c om File pf = targetFile.getParentFile(); if (!pf.exists()) { pf.mkdirs(); } fo = new FileOutputStream(targetFile).getChannel(); if (BodyType.TEXT.equals(bodyType)) { fo.write(ByteBuffer.wrap(getContent().getBytes())); } else { fo.write(ByteBuffer.wrap(getData())); } } catch (Exception e) { throw new RuntimeException(e); } finally { if (fo != null) { try { fo.close(); } catch (IOException ignore) { logger.error(ignore.getMessage(), ignore); } } } }
From source file:org.forgerock.openidm.script.javascript.JavaScriptFactory.java
private Script initializeScript(String name, String source, boolean sharedScope) throws ScriptException { initDebugListener();/*from ww w .ja v a2 s. c o m*/ if (debugInitialised) { try { FileChannel outChannel = new FileOutputStream(getTargetFile(name)).getChannel(); FileLock outLock = outChannel.lock(); ByteBuffer buf = ByteBuffer.allocate(source.length()); buf.put(source.getBytes("UTF-8")); buf.flip(); outChannel.write(buf); outLock.release(); outChannel.close(); } catch (IOException e) { logger.warn("JavaScript source was not updated for {}", name, e); } } return new JavaScript(name, source, sharedScope); }