📄 writerpoolmember.java
字号:
} else { WriterPoolMember.roundRobinIndex++; } if (d == null) { throw new IOException("Directories unusable."); } return d; } protected File checkWriteable(File d) { if (d == null) { return d; } try { IoUtils.ensureWriteableDirectory(d); } catch(IOException e) { logger.warning("Directory " + d.getPath() + " is not" + " writeable or cannot be created: " + e.getMessage()); d = null; } return d; } protected synchronized TimestampSerialno getTimestampSerialNo() { return getTimestampSerialNo(null); } /** * Do static synchronization around getting of counter and timestamp so * no chance of a thread getting in between the getting of timestamp and * allocation of serial number throwing the two out of alignment. * * @param timestamp If non-null, use passed timestamp (must be 14 digit * ARC format), else if null, timestamp with now. * @return Instance of data structure that has timestamp and serial no. */ protected synchronized TimestampSerialno getTimestampSerialNo(final String timestamp) { return new TimestampSerialno((timestamp != null)? timestamp: ArchiveUtils.get14DigitDate(), serialNo.getAndIncrement()); } /** * Return a unique basename. * * Name is timestamp + an every increasing sequence number. * * @param tsn Structure with timestamp and serial number. * * @return Unique basename. */ private String getUniqueBasename(TimestampSerialno tsn) { return tsn.getTimestamp() + "-" + WriterPoolMember.serialNoFormatter.format(tsn.getSerialNumber()); } /** * Get the file name * * @return the filename, as if uncompressed */ protected String getBaseFilename() { String name = this.f.getName(); if (this.compressed && name.endsWith(DOT_COMPRESSED_FILE_EXTENSION)) { return name.substring(0,name.length() - 3); } else if(this.compressed && name.endsWith(DOT_COMPRESSED_FILE_EXTENSION + OCCUPIED_SUFFIX)) { return name.substring(0, name.length() - (3 + OCCUPIED_SUFFIX.length())); } else { return name; } } /** * Get this file. * * Used by junit test to test for creation and when {@link WriterPool} wants * to invalidate a file. * * @return The current file. */ public File getFile() { return this.f; } /** * Post write tasks. * * Has side effects. Will open new file if we're at the upperbound. * If we're writing compressed files, it will wrap output stream with a * GZIP writer with side effect that GZIP header is written out on the * stream. * * @exception IOException */ protected void preWriteRecordTasks() throws IOException { checkSize(); if (this.compressed) { // Wrap stream in GZIP Writer. // The below construction immediately writes the GZIP 'default' // header out on the underlying stream. this.out = new CompressedStream(this.out); } } /** * Post file write tasks. * If compressed, finishes up compression and flushes stream so any * subsequent checks get good reading. * * @exception IOException */ protected void postWriteRecordTasks() throws IOException { if (this.compressed) { CompressedStream o = (CompressedStream)this.out; o.finish(); o.flush(); this.out = o.getWrappedStream(); } } /** * Postion in current physical file. * Used making accounting of bytes written. * @return Position in underlying file. Call before or after writing * records *only* to be safe. * @throws IOException */ public long getPosition() throws IOException { long position = 0; if (this.out != null) { this.out.flush(); } if (this.fos != null) { // Call flush on underlying file though probably not needed assuming // above this.out.flush called through to this.fos. this.fos.flush(); position = this.fos.getChannel().position(); } return position; } public boolean isCompressed() { return compressed; } protected void write(final byte [] b) throws IOException { this.out.write(b); } protected void flush() throws IOException { this.out.flush(); } protected void write(byte[] b, int off, int len) throws IOException { this.out.write(b, off, len); } protected void write(int b) throws IOException { this.out.write(b); } protected void readFullyFrom(final InputStream is, final long recordLength, final byte [] b) throws IOException { int read = b.length; int total = 0; while((read = is.read(b)) != -1 && total < recordLength) { total += read; write(b, 0, read); } if (total != recordLength) { throw new IOException("Read " + total + " but expected " + recordLength); } } protected void readToLimitFrom(final InputStream is, final long limit, final byte [] b) throws IOException { int read = b.length; long total = 0; while((read = is.read(b,0,(int)Math.min(b.length, (limit-total)))) != -1 && total < limit) { total += read; write(b, 0, read); } if (total != limit) { throw new IOException("Read " + total + " but expected " + limit); } } public void close() throws IOException { if (this.out == null) { return; } this.out.close(); this.out = null; this.fos = null; if (this.f != null && this.f.exists()) { String path = this.f.getAbsolutePath(); if (path.endsWith(OCCUPIED_SUFFIX)) { File f = new File(path.substring(0, path.length() - OCCUPIED_SUFFIX.length())); if (!this.f.renameTo(f)) { logger.warning("Failed rename of " + path); } this.f = f; } logger.info("Closed " + this.f.getAbsolutePath() + ", size " + this.f.length()); } } protected OutputStream getOutputStream() { return this.out; } protected String getCreateTimestamp() { return createTimestamp; } /** * An override so we get access to underlying output stream. * @author stack */ private class CompressedStream extends GZIPOutputStream { public CompressedStream(OutputStream out) throws IOException { super(out); } /** * @return Reference to stream being compressed. */ OutputStream getWrappedStream() { return this.out; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -