public class MetadataWriter extends PrintWriter
bfsPrintSection(String, boolean, boolean)
to start a new section, and bfsPrintValue(String, String...)
to print a section entry. It is not recommended that any of
the superclass methods (eg. PrintWriter
methods} are used
directly as it can create an invalid BFS metadata file.
This class can be subclassed to get specific functionality in specific use cases.
Modifier and Type | Field and Description |
---|---|
static String |
BOF_MARKER |
private String |
currentSection |
private EncoderDecoder |
encoder |
private String |
filename |
private int |
lineCount |
private int |
sectionCount |
private String |
subtype |
private boolean |
uniqueKeys |
private Set<String> |
usedKeys |
private Set<String> |
usedSections |
private int |
valueCount |
out
Constructor and Description |
---|
MetadataWriter(Writer out)
Create a new BFS metadata writer.
|
Modifier and Type | Method and Description |
---|---|
void |
bfsPrintComment(String comment)
Prints one or more comment lines to the metadata file.
|
void |
bfsPrintEmptyLine(int num)
Print one or more empty lines to the metadata file.
|
void |
bfsPrintSection(String name,
boolean unique,
boolean uniqueKeys)
Start a new section.
|
<T> void |
bfsPrintValue(String key,
Formatter<T> formatter,
T... values)
Prints a section entry.
|
void |
bfsPrintValue(String key,
String... values)
Prints a section entry.
|
<T> void |
bfsPrintValue(String key,
T... values)
Prints a section entry.
|
static MetadataWriter |
create(File file)
Utility method for creating a metadata writer to a file in the BASE
file system.
|
static MetadataWriter |
create(File file)
Utility method for creating a metadata writer to a file in the native
file system.
|
static MetadataWriter |
create(OutputStream out,
String filename)
Utility method for creating a metadata writer when you have an
output stream.
|
String |
encodeValue(String value)
Encode a value for use in a BFS file.
|
String |
getCurrentSection()
Get the name of the current section.
|
String |
getFilename()
Get the file name that this writer is printing to.
|
int |
getLineCount()
Get the number of lines that has been written to this
file so far, including all comment lines, empty lines,
sections headers, etc.
|
int |
getSectionCount()
Get the number of sections that has been written to this
file so far.
|
String |
getSubtype()
Get the BFS subtype.
|
boolean |
getUniqueKeys()
Checks if the current section requires entry names to be unique
or not.
|
int |
getValueCount()
Get the number of values that has been written to the current
section so far.
|
protected void |
internalPrintBofMarker()
Prints the beginning-of-file marker.
|
protected void |
internalPrintComment(String comment)
Print a single-line comment.
|
protected void |
internalPrintSection(String encodedName)
Prints a section header assuming that the name has already
been encoded.
|
protected void |
internalPrintValue(String encodedKey,
String encodedValue)
Prints a section entry assuming that the key and value has
already been encoded.
|
boolean |
isUsedKey(String key)
Checks if the current section already has an entry for the given
key or not.
|
void |
setFilename(String filename)
Set the file name that this writer is printing to.
|
void |
setSubtype(String subtype)
Set the BFS subtype for this writer.
|
append, append, append, checkError, clearError, close, flush, format, format, print, print, print, print, print, print, print, print, print, printf, printf, println, println, println, println, println, println, println, println, println, println, setError, write, write, write, write, write
public static final String BOF_MARKER
private final EncoderDecoder encoder
private String filename
private String subtype
private String currentSection
private boolean uniqueKeys
private int lineCount
private int sectionCount
private int valueCount
public MetadataWriter(Writer out)
out
- The parent writer which this writer will print topublic static MetadataWriter create(OutputStream out, String filename)
out
- The output stream the metadata writer should print tofilename
- Optional, the name of the file the output stream
is printing topublic static MetadataWriter create(File file)
file
- The file in the BASE file systempublic static MetadataWriter create(File file) throws IOException
file
- The file in the native file systemIOException
public String getFilename()
public void setFilename(String filename)
public String getSubtype()
public void setSubtype(String subtype)
subtype
- The subtypeIllegalStateException
- If data has already been writtenpublic String getCurrentSection()
public boolean getUniqueKeys()
public boolean isUsedKey(String key)
key
- The key to checkpublic String encodeValue(String value)
value
- The value to encode, may be nullpublic int getLineCount()
public int getSectionCount()
public int getValueCount()
public void bfsPrintEmptyLine(int num)
num
- The number of empty lines to print. If 0 or negative
nothing is writtenpublic void bfsPrintComment(String comment)
comment
- The comment stringpublic void bfsPrintSection(String name, boolean unique, boolean uniqueKeys)
name
- The name of the section, null is not allowedunique
- TRUE if this section must be unique within the metadata
fileuniqueKeys
- TRUE if all keys in this section must be uniqueNullPointerException
- If the section name is nullIllegalArgumentException
- If the name is not unique and
the writer requires unique sectionspublic void bfsPrintValue(String key, String... values)
bfsPrintSection(String, boolean, boolean)
before this
method is invoked.key
- The entry key; null is not allowed and it may not
start with '[' or '#'values
- An array of string values, each value will be encoded
independently and separated with a tabNullPointerException
- If the key is nullIllegalArgumentException
- If the key starts with '[' or
'#', or if the key is not unique within a section that requires
unique keysIllegalStateException
- If no section has been startedpublic <T> void bfsPrintValue(String key, T... values)
bfsPrintSection(String, boolean, boolean)
before this
method is invoked.key
- The entry key; null is not allowed and it may not
start with '[' or '#'values
- An array of objects, each value will be be converted to
a string by calling the toString() methodNullPointerException
- If the key is nullIllegalArgumentException
- If the key starts with '[' or
'#', or if the key is not unique within a section that requires
unique keysIllegalStateException
- If no section has been startedpublic <T> void bfsPrintValue(String key, Formatter<T> formatter, T... values)
bfsPrintSection(String, boolean, boolean)
before this
method is invoked.key
- The entry key; null is not allowed and it may not
start with '[' or '#'formatter
- A formatter that should be used to convert the
values to strings, or null to simply use the toString() methodvalues
- An array of objects, each value will be encoded
independently and separated with a tabNullPointerException
- If the key is nullIllegalArgumentException
- If the key starts with '[' or
'#', or if the key is not unique within a section that requires
unique keysIllegalStateException
- If no section has been startedprotected void internalPrintBofMarker()
BFSformat<tab>subtype
protected void internalPrintComment(String comment)
protected void internalPrintSection(String encodedName)