⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 filestoreoutputstream.java

📁 非常棒的java数据库
💻 JAVA
字号:
/*
 * Copyright 2004-2008 H2 Group. Licensed under the H2 License, Version 1.0
 * (http://h2database.com/html/license.html).
 * Initial Developer: H2 Group
 */
package org.h2.store;

import java.io.IOException;
import java.io.OutputStream;
import java.sql.SQLException;

import org.h2.engine.Constants;
import org.h2.message.Message;
import org.h2.tools.CompressTool;

/**
 * An output stream that is backed by a file store.
 */
public class FileStoreOutputStream extends OutputStream {
    private FileStore store;
    private DataPage page;
    private String compressionAlgorithm;
    private CompressTool compress;
    
    public FileStoreOutputStream(FileStore store, DataHandler handler, String compressionAlgorithm) {
        this.store = store;
        if (compressionAlgorithm != null) {
            compress = CompressTool.getInstance();
            this.compressionAlgorithm = compressionAlgorithm;
        }
        page = DataPage.create(handler, Constants.FILE_BLOCK_SIZE);
    }

    public void write(byte[] buff) throws IOException {
        write(buff, 0, buff.length);
    }

    public void write(byte[] buff, int off, int len) throws IOException {
        if (len > 0) {
            try {
                page.reset();
                if (compress != null) {
                    if (off != 0 || len != buff.length) {
                        byte[] b2 = new byte[len];
                        System.arraycopy(buff, off, b2, 0, len);
                        buff = b2;
                        off = 0;
                    }
                    int uncompressed = len;
                    buff = compress.compress(buff, compressionAlgorithm);
                    len = buff.length;
                    page.writeInt(len);
                    page.writeInt(uncompressed);
                    page.write(buff, off, len);
                } else {
                    page.writeInt(len);
                    page.write(buff, off, len);
                }
                page.fillAligned();
                store.write(page.getBytes(), 0, page.length());
            } catch (SQLException e) {
                throw Message.convertToIOException(e);
            }
        }
    }

    public void close() throws IOException {
        if (store != null) {
            try {
                store.close();
            } finally {
                store = null;
            }
        }
    }

    public void write(int b) throws IOException {
        throw new IOException("this method is not implemented");
    }
    
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -