@Override public OutputStream openBinary(final JPackage pkg, String fileName) throws IOException { final ByteArrayOutputStream javaSourceStream = new ByteArrayOutputStream(); final String scalaFileName = fileName.replaceAll("\\.java$", ".scala"); return new FilterOutputStream(javaSourceStream) { @Override public void close() throws IOException { super.close(); final String javaSource = new String(javaSourceStream.toByteArray(), "utf-8"); final String scalaSource = Converter.instance210().convert(javaSource, new ConversionSettings(false)); OutputStream parentStream = ScalaZipCodeWriter.super.openBinary(pkg, scalaFileName); parentStream.write(scalaSource.getBytes("utf-8")); } }; }
@Override public OutputStream openBinary(final JPackage pkg, String fileName) throws IOException { final ByteArrayOutputStream javaSourceStream = new ByteArrayOutputStream(); final String scalaFileName = fileName.replaceAll("\\.java$", ".scala"); return new FilterOutputStream(javaSourceStream) { @Override public void close() throws IOException { super.close(); final String javaSource = new String(javaSourceStream.toByteArray(), "utf-8"); final String scalaSource = Converter.instance210().convert(javaSource, new ConversionSettings(false)); OutputStream parentStream = ScalaSingleStreamCodeWriter.super.openBinary(pkg, scalaFileName); parentStream.write(scalaSource.getBytes("utf-8")); } }; }
public OutputStream openBinary(JPackage pkg, String fileName) throws IOException { final ByteArrayOutputStream javaSourceStream = new ByteArrayOutputStream(); final String javaFileName = getFile(pkg, fileName).getAbsolutePath(); final String scalaFileName = javaFileName.replaceAll("\\.java$", ".scala"); return new FilterOutputStream(javaSourceStream) { public void close() throws IOException { super.close(); final String javaSource = new String(javaSourceStream.toByteArray(), encoding); final String scalaSource = Converter.instance210().convert(javaSource, new ConversionSettings(false)); FileUtils.writeStringToFile(new File(scalaFileName), scalaSource, encoding); } }; }
private OutputStream outputStream() throws IOException { FileObject fo = toPropertiesFile(true); final FileLock lock = fo.lock(); OutputStream os = null; try { os = fo.getOutputStream(lock); } finally { if(os == null && lock != null) { // release lock if getOutputStream failed lock.releaseLock(); } } return new FilterOutputStream(os) { public @Override void close() throws IOException { super.close(); lock.releaseLock(); } }; }
private OutputStream getOutputStreamForMac42624(final OutputStream originalStream, final String name) { final File f = getFile(name); final long lModified = f.lastModified(); OutputStream retVal = new FilterOutputStream(originalStream) { @Override public void close() throws IOException { super.close(); if ((f.length() == 0) && (f.lastModified() == lModified)) { f.setLastModified(System.currentTimeMillis()); } } }; return retVal; }
TestFileSystem(LocalFileSystem lfs, String testName) throws Exception { super(); if ("testOutputStreamFiresIOException".equals(testName)) { this.info = new LocalFileSystem.Impl(this) { public OutputStream outputStream(String name) throws java.io.IOException { throw new IOException(); } }; } else if ("testCloseStreamFiresIOException".equals(testName)) { this.info = new LocalFileSystem.Impl(this) { public OutputStream outputStream(String name) throws java.io.IOException { return new FilterOutputStream(super.outputStream(name)) { public void close() throws IOException { throw new IOException(); } }; } }; } setRootDirectory(lfs.getRootDirectory()); }
public OutputStream getOutputStream() { return new FilterOutputStream(new ByteArrayOutputStream()) { @Override public void flush() throws IOException { super.flush(); publishData(); } @Override public void close() throws IOException { super.close(); publishData(); } private void publishData() { ByteArrayOutputStream bytesOut = (ByteArrayOutputStream) out; XmlResult.this.dataHolder = new BytesDataHolder(bytesOut.toByteArray()); } }; }
public CacheRequestImpl(final DiskLruCache.Editor editor) throws IOException { this.editor = editor; this.cacheOut = editor.newOutputStream(ENTRY_BODY); this.body = new FilterOutputStream(cacheOut) { @Override public void close() throws IOException { synchronized (HttpResponseCache.this) { if (done) { return; } done = true; writeSuccessCount++; } super.close(); editor.commit(); } @Override public void write(byte[] buffer, int offset, int length) throws IOException { // Since we don't override "write(int oneByte)", we can write directly to "out" // and avoid the inefficient implementation from the FilterOutputStream. out.write(buffer, offset, length); } }; }
@Override public OutputStream openOutputStream() { return new FilterOutputStream(new ByteArrayOutputStream()) { @Override public void close() throws IOException { out.close(); byte[] bytes = ((ByteArrayOutputStream) out).toByteArray(); save(location, name, new Content() { @Override public byte[] getBytes() { return bytes; } @Override public String getString() { return new String(bytes); } }); } }; }
@Override public OutputStream openBinary(JPackage pkg, String fileName) throws IOException { final String name = pkg != null && pkg.name().length() > 0 ? pkg.name() + '.' + fileName : fileName; out.println( "-----------------------------------" + name + "-----------------------------------"); return new FilterOutputStream(out) { @Override public void close() { // don't let this stream close } }; }
@Override public synchronized void eConnect(Socket socket, int clientId) throws IOException { super.eConnect(socket, clientId); // replace the output stream with one that logs all data to m_outLogger if (isConnected()) { try { Field realOsField = FilterOutputStream.class.getDeclaredField( "out"); realOsField.setAccessible( true); OutputStream realOs = (OutputStream)realOsField.get( m_dos); realOsField.set( m_dos, new MyOS( realOs) ); } catch( Exception e) { e.printStackTrace(); } } }
public void test_flush() throws IOException { Support_OutputStream sos = new Support_OutputStream(550); os = new FilterOutputStream(sos); os.write(fileString.getBytes(), 0, 500); os.flush(); assertEquals("Test 1: Bytes not written after flush;", 500, sos.size()); sos.setThrowsException(true); try { os.flush(); fail("Test 2: IOException expected."); } catch (IOException e) { // Expected. } sos.setThrowsException(false); }
public void test_write$B() throws IOException { Support_OutputStream sos = new Support_OutputStream(testLength); os = new FilterOutputStream(sos); os.write(fileString.getBytes()); bis = new ByteArrayInputStream(sos.toByteArray()); assertTrue("Test 1: Bytes have not been written.", bis.available() == testLength); byte[] wbytes = new byte[testLength]; bis.read(wbytes, 0, testLength); assertTrue("Test 2: Incorrect bytes written or read.", fileString.equals(new String(wbytes))); try { // Support_OutputStream throws an IOException if the internal // buffer is full, which it should be now. os.write(42); fail("Test 2: IOException expected."); } catch (IOException e) { // Expected. } }
public void test_write$BII() throws IOException { Support_OutputStream sos = new Support_OutputStream(testLength); os = new FilterOutputStream(sos); os.write(fileString.getBytes(), 10, testLength - 10); bis = new ByteArrayInputStream(sos.toByteArray()); assertTrue("Test 1: Bytes have not been written.", bis.available() == testLength - 10); byte[] wbytes = new byte[testLength - 10]; bis.read(wbytes); assertTrue("Test 2: Incorrect bytes written or read.", fileString.substring(10).equals(new String(wbytes))); try { // Support_OutputStream throws an IOException if the internal // buffer is full, which it should be eventually. os.write(fileString.getBytes()); fail("Test 2: IOException expected."); } catch (IOException e) { // Expected. } }
public void test_writeI() throws IOException { Support_OutputStream sos = new Support_OutputStream(1); os = new FilterOutputStream(sos); os.write(42); bis = new ByteArrayInputStream(sos.toByteArray()); assertTrue("Test 1: Byte has not been written.", bis.available() == 1); assertEquals("Test 2: Incorrect byte written or read;", 42, bis.read()); try { // Support_OutputStream throws an IOException if the internal // buffer is full, which it should be now. os.write(42); fail("Test 2: IOException expected."); } catch (IOException e) { // Expected. } }
private OutputStream writeWithBackup() throws IOException { // 1. delete filename.new if it exists delete(".new"); // 2. if filename exists, rename it to filename.backup (deleting old backup) if (lastModified("") != null) { delete(".backup"); rename("", ".backup"); } // 3. create filename.new, returning a stream for writing its content return new FilterOutputStream(write(".new")) { @Override public void close() throws IOException { super.close(); rename(".new", ""); } }; }
/** * Change the encoding used by this connection. * * @param encoding the new encoding to use * @throws IOException if something goes wrong */ public void setEncoding(Encoding encoding) throws IOException { // Close down any old writer. if (encodingWriter != null) encodingWriter.close(); this.encoding = encoding; // Intercept flush() downcalls from the writer; our caller // will call PGStream.flush() as needed. OutputStream interceptor = new FilterOutputStream(pg_output) { public void flush() throws IOException { } public void close() throws IOException { super.flush(); } }; encodingWriter = encoding.getEncodingWriter(interceptor); }
@Override public Field[] createIndexableFields(Shape shape) { int bufSize = Math.max(128, (int) (this.indexLastBufSize * 1.5));//50% headroom over last ByteArrayOutputStream byteStream = new ByteArrayOutputStream(bufSize); final BytesRef bytesRef = new BytesRef();//receiver of byteStream's bytes try { ctx.getBinaryCodec().writeShape(new DataOutputStream(byteStream), shape); //this is a hack to avoid redundant byte array copying by byteStream.toByteArray() byteStream.writeTo(new FilterOutputStream(null/*not used*/) { @Override public void write(byte[] b, int off, int len) throws IOException { bytesRef.bytes = b; bytesRef.offset = off; bytesRef.length = len; } }); } catch (IOException e) { throw new RuntimeException(e); } this.indexLastBufSize = bytesRef.length;//cache heuristic return new Field[]{new BinaryDocValuesField(getFieldName(), bytesRef)}; }