下面列出了java.io.PrintWriter#checkError ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
/**
* Returns the String representation of an AlloyNodeElement's settings.
*/
private static String writeNodeViz(VizState view, VizState defaultView, AlloyNodeElement x) throws IOException {
StringWriter sw = new StringWriter();
PrintWriter out = new PrintWriter(sw);
writeBool(out, "visible", view.nodeVisible.get(x), defaultView.nodeVisible.get(x));
writeBool(out, "hideunconnected", view.hideUnconnected.get(x), defaultView.hideUnconnected.get(x));
if (x == null || x instanceof AlloySet) {
AlloySet s = (AlloySet) x;
writeBool(out, "showlabel", view.showAsLabel.get(s), defaultView.showAsLabel.get(s));
writeBool(out, "showinattr", view.showAsAttr.get(s), defaultView.showAsAttr.get(s));
}
if (x == null || x instanceof AlloyType) {
AlloyType t = (AlloyType) x;
writeBool(out, "numberatoms", view.number.get(t), defaultView.number.get(t));
}
writeDotStyle(out, view.nodeStyle.get(x), defaultView.nodeStyle.get(x));
writeDotShape(out, view.shape.get(x), defaultView.shape.get(x));
writeDotColor(out, view.nodeColor.get(x), defaultView.nodeColor.get(x));
if (x != null && !view.label.get(x).equals(defaultView.label.get(x)))
Util.encodeXMLs(out, " label=\"", view.label.get(x), "\"");
if (out.checkError())
throw new IOException("PrintWriter IO Exception!");
return sw.toString();
}
/**
* Returns the String representation of an AlloyRelation's settings.
*/
private static String writeEdgeViz(VizState view, VizState defaultView, AlloyRelation x) throws IOException {
StringWriter sw = new StringWriter();
PrintWriter out = new PrintWriter(sw);
writeDotColor(out, view.edgeColor.get(x), defaultView.edgeColor.get(x));
writeDotStyle(out, view.edgeStyle.get(x), defaultView.edgeStyle.get(x));
writeBool(out, "visible", view.edgeVisible.get(x), defaultView.edgeVisible.get(x));
writeBool(out, "merge", view.mergeArrows.get(x), defaultView.mergeArrows.get(x));
writeBool(out, "layout", view.layoutBack.get(x), defaultView.layoutBack.get(x));
writeBool(out, "attribute", view.attribute.get(x), defaultView.attribute.get(x));
writeBool(out, "constraint", view.constraint.get(x), defaultView.constraint.get(x));
if (view.weight.get(x) != defaultView.weight.get(x))
out.write(" weight=\"" + view.weight.get(x) + "\"");
if (x != null && !view.label.get(x).equals(defaultView.label.get(x)))
Util.encodeXMLs(out, " label=\"", view.label.get(x), "\"");
if (out.checkError())
throw new IOException("PrintWriter IO Exception!");
return sw.toString();
}
/**
* If this solution is a satisfiable solution, this method will write it out in
* XML format.
*/
static void writeInstance(A4Reporter rep, A4Solution sol, PrintWriter out, Iterable<Func> extraSkolems, Map<String,String> sources) throws Err {
if (!sol.satisfiable())
throw new ErrorAPI("This solution is unsatisfiable.");
try {
Util.encodeXMLs(out, "<alloy builddate=\"", Version.buildDate(), "\">\n\n");
new A4SolutionWriter(rep, sol, sol.getAllReachableSigs(), sol.getBitwidth(), sol.getMaxSeq(), sol.getOriginalCommand(), sol.getOriginalFilename(), out, extraSkolems);
if (sources != null)
for (Map.Entry<String,String> e : sources.entrySet()) {
Util.encodeXMLs(out, "\n<source filename=\"", e.getKey(), "\" content=\"", e.getValue(), "\"/>\n");
}
out.print("\n</alloy>\n");
} catch (Throwable ex) {
if (ex instanceof Err)
throw (Err) ex;
else
throw new ErrorFatal("Error writing the solution XML file.", ex);
}
if (out.checkError())
throw new ErrorFatal("Error writing the solution XML file.");
}
private static void sendCommand(String json, ProcessHandler handler) {
final PrintWriter stdin = getStdin(handler);
if (stdin == null) {
FlutterUtils.warn(LOG, "can't write command to Flutter process: " + json);
return;
}
stdin.write('[');
stdin.write(json);
stdin.write("]\n");
if (FlutterSettings.getInstance().isVerboseLogging()) {
LOG.info("[--> " + json + "]");
}
if (stdin.checkError()) {
FlutterUtils.warn(LOG, "can't write command to Flutter process: " + json);
}
}
private static void sendCommand(String json, ProcessHandler handler) {
final PrintWriter stdin = getStdin(handler);
if (stdin == null) {
FlutterUtils.warn(LOG, "can't write command to Flutter process: " + json);
return;
}
stdin.write('[');
stdin.write(json);
stdin.write("]\n");
if (FlutterSettings.getInstance().isVerboseLogging()) {
LOG.info("[--> " + json + "]");
}
if (stdin.checkError()) {
FlutterUtils.warn(LOG, "can't write command to Flutter process: " + json);
}
}
/**
* Creates a temporary file containing the sentences to be processed by ASSERT.
*
* @param ss sentences to be parsed
* @return input file
*/
private static File createInputFile(String[] ss) throws Exception {
try {
File input = File.createTempFile("assert", ".input", new File(ASSERT_DIR + "/scripts"));
// input.deleteOnExit();
PrintWriter pw = new PrintWriter(new BufferedWriter(
new OutputStreamWriter(new FileOutputStream(input), "ISO-8859-1")));
for (String sentence : ss) {
pw.println(sentence);
if (pw.checkError()) throw new IOException();
}
pw.close();
if (pw.checkError()) throw new IOException();
return input;
} catch (IOException e) {
throw new IOException("Failed to create input file.");
}
}
/**
* Write the metamodel as <instance>..</instance> in XML format.
*/
public static void writeMetamodel(ConstList<Sig> sigs, String originalFilename, PrintWriter out) throws Err {
try {
new A4SolutionWriter(null, null, sigs, 4, 4, "show metamodel", originalFilename, out, null);
} catch (Throwable ex) {
if (ex instanceof Err)
throw (Err) ex;
else
throw new ErrorFatal("Error writing the solution XML file.", ex);
}
if (out.checkError())
throw new ErrorFatal("Error writing the solution XML file.");
}
public static void main(String[] args) throws Exception {
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Type something =>");
String line = reader.readLine();
// this can throw exceptions.
reader.close();
System.out.println("\nWhat you typed: ");
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(System.out));
// this can throw exceptions
writer.write(line);
writer.flush();
writer.close();
System.out.println("\nNow, I will print this again with a print writer:");
PrintWriter pw = new PrintWriter(new OutputStreamWriter(System.out));
pw.write(line);
pw.flush();
// with print writer you have to manually check
if(pw.checkError()) {
System.out.println("Wow some error occured!");
}
}
@Override
public void write(String metric, OutputStream out) {
PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(out, StandardCharsets.UTF_8), false);
printWriter.write(metric);
printWriter.flush();
if (printWriter.checkError()) {
throw new RuntimeException("Error writing to Timely");
}
}
public void format(List<MediaFile> files, PrintWriter writer) throws IOException {
writer.println("#EXTM3U");
for (MediaFile file : files) {
writer.println(file.getPath());
}
if (writer.checkError()) {
throw new IOException("Error when writing playlist");
}
}
public void format(List<MediaFile> files, PrintWriter writer) throws IOException {
writer.println("[playlist]");
int counter = 0;
for (MediaFile file : files) {
counter++;
writer.println("File" + counter + '=' + file.getPath());
}
writer.println("NumberOfEntries=" + counter);
writer.println("Version=2");
if (writer.checkError()) {
throw new IOException("Error when writing playlist.");
}
}
public void format(List<MediaFile> files, PrintWriter writer) throws IOException {
writer.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
writer.println("<playlist version=\"1\" xmlns=\"http://xspf.org/ns/0/\">");
writer.println(" <trackList>");
for (MediaFile file : files) {
writer.println(" <track><location>file://" + StringEscapeUtils.escapeXml(file.getPath()) + "</location></track>");
}
writer.println(" </trackList>");
writer.println("</playlist>");
if (writer.checkError()) {
throw new IOException("Error when writing playlist.");
}
}
/**
* sends a message to each of the clients in telnet-friendly output.
*/
public final void send(final String message) {
final Enumeration ce = connections.elements();
for (final Enumeration e = writers.elements(); e.hasMoreElements();) {
final Socket sock = (Socket) ce.nextElement();
final PrintWriter writer = (PrintWriter) e.nextElement();
writer.print(message);
if (writer.checkError()) {
// The client has closed the connection, remove it from our list:
connections.remove(sock);
writers.remove(writer);
}
}
}
/**
* Print a DOT representation of search graph.
*
* @param file file where to print the DOT representation to.
*
* @throws IllegalArgumentException if {@code file} is {@code null}.
* @throws FileNotFoundException if {@code file} cannot be created or written.
* @throws IllegalStateException if there was some trouble when writing the DOT representation.
*/
public void printDOT(final File file) throws FileNotFoundException {
Utils.nonNull(file, "the output file cannot be null");
final PrintWriter out = new PrintWriter(file);
printDOT(out);
if (out.checkError()) {
throw new IllegalStateException("error occurred while writing k-best haplotype search graph into file '"
+ file.getAbsolutePath() + '\'');
}
out.close();
}
/** Helper method to write out a full XML file. */
public void writeXML(PrintWriter writer, Iterable<Func> macros, Map<String,String> sourceFiles) throws Err {
A4SolutionWriter.writeInstance(null, this, writer, macros, sourceFiles);
if (writer.checkError())
throw new ErrorFatal("Error writing the solution XML file.");
}
/** Helper method to write out a full XML file. */
public void writeXML(A4Reporter rep, PrintWriter writer, Iterable<Func> macros, Map<String,String> sourceFiles) throws Err {
A4SolutionWriter.writeInstance(rep, this, writer, macros, sourceFiles);
if (writer.checkError())
throw new ErrorFatal("Error writing the solution XML file.");
}
/** Copy Reader to Writer for byteCount bytes or until EOF or exception.
*/
public static void copy(Reader in,
Writer out,
long byteCount)
throws IOException
{
char buffer[] = new char[bufferSize];
int len=bufferSize;
if (byteCount>=0)
{
while (byteCount>0)
{
if (byteCount<bufferSize)
len=in.read(buffer,0,(int)byteCount);
else
len=in.read(buffer,0,bufferSize);
if (len==-1)
break;
byteCount -= len;
out.write(buffer,0,len);
}
}
else if (out instanceof PrintWriter)
{
PrintWriter pout=(PrintWriter)out;
while (!pout.checkError())
{
len=in.read(buffer,0,bufferSize);
if (len==-1)
break;
out.write(buffer,0,len);
}
}
else
{
while (true)
{
len=in.read(buffer,0,bufferSize);
if (len==-1)
break;
out.write(buffer,0,len);
}
}
}
/** Copy Reader to Writer for byteCount bytes or until EOF or exception.
*/
public static void copy(Reader in,
Writer out,
long byteCount)
throws IOException
{
char buffer[] = new char[bufferSize];
int len=bufferSize;
if (byteCount>=0)
{
while (byteCount>0)
{
if (byteCount<bufferSize)
len=in.read(buffer,0,(int)byteCount);
else
len=in.read(buffer,0,bufferSize);
if (len==-1)
break;
byteCount -= len;
out.write(buffer,0,len);
}
}
else if (out instanceof PrintWriter)
{
PrintWriter pout=(PrintWriter)out;
while (!pout.checkError())
{
len=in.read(buffer,0,bufferSize);
if (len==-1)
break;
out.write(buffer,0,len);
}
}
else
{
while (true)
{
len=in.read(buffer,0,bufferSize);
if (len==-1)
break;
out.write(buffer,0,len);
}
}
}
/** Copy Reader to Writer for byteCount bytes or until EOF or exception.
*/
public static void copy(Reader in,
Writer out,
long byteCount)
throws IOException
{
char buffer[] = new char[bufferSize];
int len=bufferSize;
if (byteCount>=0)
{
while (byteCount>0)
{
if (byteCount<bufferSize)
len=in.read(buffer,0,(int)byteCount);
else
len=in.read(buffer,0,bufferSize);
if (len==-1)
break;
byteCount -= len;
out.write(buffer,0,len);
}
}
else if (out instanceof PrintWriter)
{
PrintWriter pout=(PrintWriter)out;
while (!pout.checkError())
{
len=in.read(buffer,0,bufferSize);
if (len==-1)
break;
out.write(buffer,0,len);
}
}
else
{
while (true)
{
len=in.read(buffer,0,bufferSize);
if (len==-1)
break;
out.write(buffer,0,len);
}
}
}
/**
* Given a Kodkod formula node, return a Java program that (when compiled and
* executed) would solve that formula.
* <p>
* Requirement: atoms must be String objects (since we cannot possibly output a
* Java source code that can re-generate arbitrary Java objects).
*
* @param formula - the formula to convert
* @param bitwidth - the integer bitwidth
* @param atoms - an iterator over the set of all atoms
* @param bounds - the Kodkod bounds object to use
* @param atomMap - if nonnull, it is used to map the atom name before printing
*/
public static String convert(Formula formula, int bitwidth, Iterable<String> atoms, Bounds bounds, Map<Object,String> atomMap) {
StringWriter string = new StringWriter();
PrintWriter file = new PrintWriter(string);
new TranslateKodkodToJava(file, formula, bitwidth, atoms, bounds, atomMap);
if (file.checkError()) {
return ""; // shouldn't happen
} else {
return string.toString();
}
}