Writes a backup to the specified stream.

@param os the stream to write to
961333
public void backup(OutputStream os) throws IOException {
    int count = 0;
    GzipCompressorOutputStream zipStream = new GzipCompressorOutputStream(os);
    for (Master master : mRegistry.getServers()) {
      Iterator<JournalEntry> it = master.getJournalEntryIterator();
      while (it.hasNext()) {
        it.next().toBuilder().clearSequenceNumber().build().writeDelimitedTo(zipStream);
        count++;
      }
    }
    // finish() instead of close() since close would close os, which is owned by the caller.
    zipStream.finish();
    LOG.info("Created backup with {} entries", count);
  }
1356330
public static void save(Object o, OutputStream stream) {
        try {
            ObjectOutputStream outStream = 
                (stream instanceof ObjectOutputStream)
                    ? (ObjectOutputStream)stream
                    : new ObjectOutputStream(stream);
            outStream.writeObject(o);
        } catch (IOException ioe) {
            throw new IOError(ioe);
        }
    }
26372
public static void compress(InputStream is, OutputStream os) throws Exception {
		GZIPOutputStream gos = new GZIPOutputStream(os);
		int count;
		byte data[] = new byte[BUFFER];
		while ((count = is.read(data, 0, BUFFER)) != -1) {
			gos.write(data, 0, count);
		}
		gos.finish();
		gos.flush();
		gos.close();
	}
1117111
public void init(WriteStream os)
  {
    _os = os;
    _openCount.set(1);
    
    switch (_peer) {
    case CLIENT:
      _nextStreamId = 1;
      break;
      
    case SERVER:
      _nextStreamId = 2;
      break;
      
    default:
      throw new IllegalStateException(String.valueOf(_peer));
    }

    if (_conn.isHeaderHuffman()) {
      _outHeader = new OutHeaderHuffman(os);
    }
    else {
      _outHeader = new OutHeader(os);
    }
  }
1151751
public void serialize(List<T> list, OutputStream os) throws IOException {
        JsonGenerator jsonGenerator = LoganSquare.JSON_FACTORY.createGenerator(os);
        serialize(list, jsonGenerator);
        jsonGenerator.close();
    }
824710
public boolean writeToStream(SendfileOutputStream os, 
                               long offset, long length,
                               long []blockAddresses, long blockLength)
    throws IOException
  {
    throw new UnsupportedOperationException(getClass().getName());
  }
639739
public int write(OutputStreamWithBuffer os, char []cbuf, int off, int len)
    throws IOException
  {
    for (int i = 0; i < len; i++) {
      write(os, cbuf[off + i]);
    }
    
    return len;
  }
87265
@SuppressWarnings("unchecked")
    public static <E> void serialize(E object, OutputStream os) throws IOException {
        mapperFor((Class<E>)object.getClass()).serialize(object, os);
    }
225556
public static void setBytes(OutputStream os, byte[] bytes) throws IOException {
        try {
            os.write(bytes);
        } finally {
            closeWithWarning(os);
        }
    }
899117
protected Thread createSystemInPump(InputStream is, OutputStream os) {
    inputStreamPumper = new InputStreamPumper(is, os);
    return newThread(inputStreamPumper);
  }
848068
public static void copy(InputStream is, OutputStream os) throws IOException {

		Asserts.notNullParameter("is", is);
		Asserts.notNullParameter("os", os);
		
		try {
			byte[] buf = new byte[1024];
			int len = 0;
			
			while((len = is.read(buf)) > 0) {
				os.write(buf, 0, len);
			}
		
		} finally {
			if(os != null) {
				try {
					os.close();
				} catch (IOException e) {
					// ignore
				}
			}
			if(is != null) {
				try {
					is.close();
				} catch (IOException e) {
					// ignore
				}
			}
		}
	}
1029402
public void setOutput(OutputStream os, String encoding) throws IOException {
		if (os == null)
			throw new IllegalArgumentException("output stream can not be null");
		reset();
		if (encoding != null) {
			out = new OutputStreamWriter(os, encoding);
		} else {
			out = new OutputStreamWriter(os);
		}
	}
844862
public boolean readToOutputStream(OutputStream os)
        throws IOException
    {
        InputStream is = readInputStream();

        if (is == null)
            return false;

        if (_buffer == null)
            _buffer = new byte[256];

        try {
            int len;

            while ((len = is.read(_buffer, 0, _buffer.length)) > 0) {
                os.write(_buffer, 0, len);
            }

            return true;
        } finally {
            is.close();
        }
    }
961730
public void writeObject(Object o) throws IOException {
        ObjectOutputStream oos = AnonymousClassWarnings.checkingObjectOutputStream(out);
        oos.writeObject(o);
        // don't close oss, which will close the underlying stream
        // no need to flush either, given the way oos is implemented
    }
977792
public void writeHeader(OutputStreamCounter os) throws IOException {
		if (appendmode) {
			os.write(HEADER[0]);
		}
		else {
			os.write(HEADER[1]);
			os.write(getVersionAsByteArray(header_version));
			os.write(HEADER[2]);
			headerWasWritten = true;
		}
	}
1005769
void backupFile() {

        writeLock.lock();

        try {
            if (incBackup) {
                if (fa.isStreamElement(backupFileName)) {
                    fa.removeElement(backupFileName);
                }

                return;
            }

            if (fa.isStreamElement(fileName)) {
                FileArchiver.archive(fileName, backupFileName + ".new",
                                     database.getFileAccess(),
                                     FileArchiver.COMPRESSION_ZIP);
            }
        } catch (IOException e) {
            database.logger.appLog.logContext(e, null);

            throw new HsqlException(e.getMessage(), "", 0);
        } finally {
            writeLock.unlock();
        }
    }
1130844
private void writeObject(ObjectOutputStream oos) throws IOException
	{

		oos.defaultWriteObject();

		synchronized (bcsListeners)
		{
			serialize(oos, bcsListeners);
		}
	}