Corrections to JSON; Add tests for parsing and writing JSON; Implement some missing...
[SquirrelJME.git] / modules / cldc-compact / src / main / java / cc / squirreljme / runtime / cldc / io / PrintStreamWriter.java
blob2c771d0ac9ea0b1dc8b31a1e7ce9c2a5281535c5
1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the GNU General Public License v3+, or later.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc.squirreljme.runtime.cldc.io;
12 import java.io.Closeable;
13 import java.io.IOException;
14 import java.io.PrintStream;
15 import java.io.Writer;
17 /**
18 * This is a class which takes input characters and writes to the given
19 * print stream.
21 * This class is thread safe.
23 * @since 2016/08/12
25 public class PrintStreamWriter
26 extends Writer
27 implements Closeable
29 /** The stream to write to. */
30 protected final PrintStream output;
32 /**
33 * Initializes the wrapped print stream writer.
35 * @param __ps The stream to write to.
36 * @throws NullPointerException On null arguments.
37 * @since 2016/08/12
39 public PrintStreamWriter(PrintStream __ps)
40 throws NullPointerException
42 // Check
43 if (__ps == null)
44 throw new NullPointerException("NARG");
46 // Set
47 this.output = __ps;
50 /**
51 * Initializes the wrapped print stream writer and using the given lock.
53 * @param __lock The lock to use.
54 * @param __ps The stream to write to.
55 * @throws NullPointerException On null arguments.
56 * @since 2016/08/12
58 public PrintStreamWriter(Object __lock, PrintStream __ps)
59 throws NullPointerException
61 super(__lock);
63 // Check
64 if (__ps == null)
65 throw new NullPointerException("NARG");
67 // Set
68 this.output = __ps;
71 /**
72 * {@inheritDoc}
73 * @since 2016/08/12
75 @Override
76 public void close()
77 throws IOException
79 // Lock
80 PrintStream output = this.output;
81 synchronized (this.lock)
83 output.close();
84 this.__checkError();
88 /**
89 * {@inheritDoc}
90 * @since 2016/08/12
92 @Override
93 public void flush()
94 throws IOException
96 // Lock
97 PrintStream output = this.output;
98 synchronized (this.lock)
100 output.flush();
101 this.__checkError();
106 * {@inheritDoc}
107 * @since 2016/08/12
109 @Override
110 public void write(int __c)
111 throws IOException
113 // Lock
114 PrintStream output = this.output;
115 synchronized (this.lock)
117 output.print((char)__c);
118 this.__checkError();
123 * {@inheritDoc}
124 * @since 2016/08/12
125 * @param __c
126 * @param __o
127 * @param __l
129 @Override
130 public void write(char[] __c, int __o, int __l)
131 throws IndexOutOfBoundsException, IOException, NullPointerException
133 // Check
134 if (__c == null)
135 throw new NullPointerException("NARG");
136 int n = __c.length;
137 int end = __o + __l;
138 if (__o < 0 || __l < 0 || end > n)
139 throw new IndexOutOfBoundsException("IOOB");
141 // Lock
142 PrintStream output = this.output;
143 synchronized (this.lock)
145 // Print characters
146 for (int i = __o; i < end; i++)
147 output.print((char)__c[i]);
149 // Check
150 this.__checkError();
155 * Checks if the given stream has reported an error.
157 * @throws IOException If the stream has entered the error state.
158 * @since 2016/08/12
160 private void __checkError()
161 throws IOException
163 // {@squirreljme.error BD1n The underlying stream has entered the
164 // error state.}
165 if (this.output.checkError())
166 throw new IOException("BD1n");