Java Code Examples for java.io.FilterOutputStream

The following are top voted examples for showing how to use java.io.FilterOutputStream. These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.
Example 1
Project: GitHub   File: ScalaZipCodeWriter.java   View source code 6 votes vote down vote up
@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"));
        }
    };
}
 
Example 2
Project: GitHub   File: ScalaSingleStreamCodeWriter.java   View source code 6 votes vote down vote up
@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"));
        }
    };
}
 
Example 3
Project: GitHub   File: ScalaFileCodeWriter.java   View source code 6 votes vote down vote up
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);
        }
    };
}
 
Example 4
Project: incubator-netbeans   File: PropertiesStorage.java   View source code 6 votes vote down vote up
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();
        }
    };
}
 
Example 5
Project: incubator-netbeans   File: LocalFileSystem.java   View source code 6 votes vote down vote up
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;
}
 
Example 6
Project: incubator-netbeans   File: FileObject75826Test.java   View source code 6 votes vote down vote up
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());
}
 
Example 7
Project: fluentxml4j   File: XmlResult.java   View source code 6 votes vote down vote up
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());
		}
	};
}
 
Example 8
Project: LoRaWAN-Smart-Parking   File: HttpResponseCache.java   View source code 6 votes vote down vote up
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);
    }
  };
}
 
Example 9
Project: LoRaWAN-Smart-Parking   File: HttpResponseCache.java   View source code 6 votes vote down vote up
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);
    }
  };
}
 
Example 10
Project: openjdk-jdk10   File: ToolBox.java   View source code 6 votes vote down vote up
@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);
                }

            });
        }
    };
}
 
Example 11
Project: openjdk-jdk10   File: SingleStreamCodeWriter.java   View source code 6 votes vote down vote up
@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
        }
    };
}
 
Example 12
Project: openjdk9   File: ToolBox.java   View source code 6 votes vote down vote up
@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);
                }

            });
        }
    };
}
 
Example 13
Project: SumZeroTrading   File: ApiConnection.java   View source code 6 votes vote down vote up
@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();
		}
	}
}
 
Example 14
Project: trading-indexFutureAndOptions   File: ApiConnection.java   View source code 6 votes vote down vote up
@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();
		}
	}
}
 
Example 15
Project: smart-mirror-app   File: HttpResponseCache.java   View source code 6 votes vote down vote up
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);
    }
  };
}
 
Example 16
Project: cordova-plugin-background-mode   File: HttpResponseCache.java   View source code 6 votes vote down vote up
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);
    }
  };
}
 
Example 17
Project: multirotorstuff-vtx-calc   File: HttpResponseCache.java   View source code 6 votes vote down vote up
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);
    }
  };
}
 
Example 18
Project: L.TileLayer.Cordova   File: HttpResponseCache.java   View source code 6 votes vote down vote up
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);
    }
  };
}
 
Example 19
Project: cordova-android-tv   File: HttpResponseCache.java   View source code 6 votes vote down vote up
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);
    }
  };
}
 
Example 20
Project: Cordova-Locale-Plugin   File: HttpResponseCache.java   View source code 6 votes vote down vote up
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);
    }
  };
}
 
Example 21
Project: pubnub-rpi-smart-parking   File: HttpResponseCache.java   View source code 6 votes vote down vote up
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);
    }
  };
}
 
Example 22
Project: IoTgo_Android_App   File: HttpResponseCache.java   View source code 6 votes vote down vote up
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);
    }
  };
}
 
Example 23
Project: j2objc   File: OldFilterOutputStreamTest.java   View source code 6 votes vote down vote up
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);
}
 
Example 24
Project: j2objc   File: OldFilterOutputStreamTest.java   View source code 6 votes vote down vote up
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.
    }
}
 
Example 25
Project: j2objc   File: OldFilterOutputStreamTest.java   View source code 6 votes vote down vote up
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.
    }
}
 
Example 26
Project: j2objc   File: OldFilterOutputStreamTest.java   View source code 6 votes vote down vote up
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.
    }
}
 
Example 27
Project: CanDoVS2015   File: HttpResponseCache.java   View source code 6 votes vote down vote up
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);
    }
  };
}
 
Example 28
Project: krakn   File: HttpResponseCache.java   View source code 6 votes vote down vote up
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);
    }
  };
}
 
Example 29
Project: krakn   File: HttpResponseCache.java   View source code 6 votes vote down vote up
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);
    }
  };
}
 
Example 30
Project: krakn   File: HttpResponseCache.java   View source code 6 votes vote down vote up
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);
    }
  };
}
 
Example 31
Project: krakn   File: HttpResponseCache.java   View source code 6 votes vote down vote up
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);
    }
  };
}
 
Example 32
Project: krakn   File: HttpResponseCache.java   View source code 6 votes vote down vote up
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);
    }
  };
}
 
Example 33
Project: posjs2   File: HttpResponseCache.java   View source code 6 votes vote down vote up
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);
    }
  };
}
 
Example 34
Project: knowledgestore   File: Dictionary.java   View source code 6 votes vote down vote up
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", "");
            }

        };
    }
 
Example 35
Project: hustElectricity   File: HttpResponseCache.java   View source code 6 votes vote down vote up
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);
    }
  };
}
 
Example 36
Project: Wetube   File: HttpResponseCache.java   View source code 6 votes vote down vote up
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);
    }
  };
}
 
Example 37
Project: jephyr   File: PGStream.java   View source code 6 votes vote down vote up
/**
 * 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);
}
 
Example 38
Project: search   File: SerializedDVStrategy.java   View source code 6 votes vote down vote up
@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)};
}
 
Example 39
Project: DemoArduinoAndroidCordovaBT   File: HttpResponseCache.java   View source code 6 votes vote down vote up
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);
    }
  };
}
 
Example 40
Project: trading-singleleg   File: ApiConnection.java   View source code 6 votes vote down vote up
@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();
		}
	}
}