Java Code Examples for java.io.StringWriter.write()

The following are Jave code examples for showing how to use write() of the java.io.StringWriter class. You can vote up the examples you like. Your votes will be used in our system to get more good examples.
+ Save this method
Example 1
Project: pay   File: WebUtils.java   View Source Code Vote up 6 votes
private static String getStreamAsString(InputStream stream, String charset) throws IOException {
    try {
        BufferedReader reader = new BufferedReader(new InputStreamReader(stream, charset));
        StringWriter writer = new StringWriter();

        char[] chars = new char[256];
        int count = 0;
        while ((count = reader.read(chars)) > 0) {
            writer.write(chars, 0, count);
        }

        return writer.toString();
    } finally {
        if (stream != null) {
            stream.close();
        }
    }
}
 
Example 2
Project: smart-cache   File: Utils.java   View Source Code Vote up 6 votes
public static String read(Reader reader) {
    try {

        StringWriter writer = new StringWriter();

        char[] buffer = new char[DEFAULT_BUFFER_SIZE];
        int n = 0;
        while (-1 != (n = reader.read(buffer))) {
            writer.write(buffer, 0, n);
        }

        return writer.toString();
    } catch (IOException ex) {
        throw new IllegalStateException("read error", ex);
    }
}
 
Example 3
Project: jetfuel   File: StringAdapter.java   View Source Code Vote up 5 votes
/**
 * Converts an input stream to a string.
 * 
 * @param is
 * @return
 */
public String convertStream(InputStream is) {
	try {
		StringWriter writer = new StringWriter();
		for (int i = is.read(); i >= 0; i = is.read())
			writer.write(i);
		is.close();
		return writer.toString();
	} catch (Exception e) {
		throw new RuntimeException(e);
	}
}
 
Example 4
Project: openjdk-jdk10   File: ClassSelector.java   View Source Code Vote up 5 votes
/**
 * Copies a schema fragment into the javadoc of the generated class.
 */
private void addSchemaFragmentJavadoc( CClassInfo bean, XSComponent sc ) {

    // first, pick it up from <documentation> if any.
    String doc = builder.getBindInfo(sc).getDocumentation();
    if(doc!=null)
        append(bean, doc);

    // then the description of where this component came from
    Locator loc = sc.getLocator();
    String fileName = null;
    if(loc!=null) {
        fileName = loc.getPublicId();
        if(fileName==null)
            fileName = loc.getSystemId();
    }
    if(fileName==null)  fileName="";

    String lineNumber=Messages.format( Messages.JAVADOC_LINE_UNKNOWN);
    if(loc!=null && loc.getLineNumber()!=-1)
        lineNumber = String.valueOf(loc.getLineNumber());

    String componentName = sc.apply( new ComponentNameFunction() );
    String jdoc = Messages.format( Messages.JAVADOC_HEADING, componentName, fileName, lineNumber );
    append(bean,jdoc);

    // then schema fragment
    StringWriter out = new StringWriter();
    out.write("<pre>\n");
    SchemaWriter sw = new SchemaWriter(new JavadocEscapeWriter(out));
    sc.visit(sw);
    out.write("</pre>");
    append(bean,out.toString());
}
 
Example 5
Project: sds   File: ByteUtils.java   View Source Code Vote up 5 votes
static void addHexAscii(byte b, StringWriter sw) {
    short ub = toUnsigned(b);
    int h1 = ub / 16;
    int h2 = ub % 16;
    sw.write(toHexDigit(h1));
    sw.write(toHexDigit(h2));
}
 
Example 6
Project: FireFiles   File: Streams.java   View Source Code Vote up 5 votes
/**
 * Returns the remainder of 'reader' as a string, closing it when done.
 */
public static String readFully(Reader reader) throws IOException {
    try {
        StringWriter writer = new StringWriter();
        char[] buffer = new char[1024];
        int count;
        while ((count = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, count);
        }
        return writer.toString();
    } finally {
        reader.close();
    }
}
 
Example 7
Project: LoRaWAN-Smart-Parking   File: Util.java   View Source Code Vote up 5 votes
/** Returns the remainder of 'reader' as a string, closing it when done. */
public static String readFully(Reader reader) throws IOException {
  try {
    StringWriter writer = new StringWriter();
    char[] buffer = new char[1024];
    int count;
    while ((count = reader.read(buffer)) != -1) {
      writer.write(buffer, 0, count);
    }
    return writer.toString();
  } finally {
    reader.close();
  }
}
 
Example 8
Project: googles-monorepo-demo   File: CharSourceTester.java   View Source Code Vote up 5 votes
public void testOpenBufferedStream() throws IOException {
  BufferedReader reader = source.openBufferedStream();

  StringWriter writer = new StringWriter();
  char[] buf = new char[64];
  int read;
  while ((read = reader.read(buf)) != -1) {
    writer.write(buf, 0, read);
  }
  reader.close();
  writer.close();

  assertExpectedString(writer.toString());
}
 
Example 9
Project: ObjectCache   File: Util.java   View Source Code Vote up 5 votes
static String readFully(Reader reader) throws IOException {
  try {
    StringWriter writer = new StringWriter();
    char[] buffer = new char[1024];
    int count;
    while ((count = reader.read(buffer)) != -1) {
      writer.write(buffer, 0, count);
    }
    return writer.toString();
  } finally {
    reader.close();
  }
}
 
Example 10
Project: iosched-reader   File: DiskLruCacheTest.java   View Source Code Vote up 5 votes
private static String readFile(File file) throws Exception {
  Reader reader = new FileReader(file);
  StringWriter writer = new StringWriter();
  char[] buffer = new char[1024];
  int count;
  while ((count = reader.read(buffer)) != -1) {
    writer.write(buffer, 0, count);
  }
  reader.close();
  return writer.toString();
}
 
Example 11
Project: guava-mock   File: CharSourceTester.java   View Source Code Vote up 5 votes
public void testOpenStream() throws IOException {
  Reader reader = source.openStream();

  StringWriter writer = new StringWriter();
  char[] buf = new char[64];
  int read;
  while ((read = reader.read(buf)) != -1) {
    writer.write(buf, 0, read);
  }
  reader.close();
  writer.close();

  assertExpectedString(writer.toString());
}
 
Example 12
Project: Pluto-Android   File: DiskLruCache.java   View Source Code Vote up 5 votes
/**
 * Returns the remainder of 'reader' as a string, closing it when done.
 */
public static String readFully(Reader reader) throws IOException {
    try {
        StringWriter writer = new StringWriter();
        char[] buffer = new char[1024];
        int count;
        while ((count = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, count);
        }
        return writer.toString();
    } finally {
        reader.close();
    }
}
 
Example 13
Project: log4j-aws-appenders   File: LogMessage.java   View Source Code Vote up 5 votes
/**
 *  Constructs an instance from a Log4J event, using the specified layout.
 *
 *  @throws RuntimeException if any error occurred during formatting or conversion.
 *          Will include any root cause other than UnsupportedEncodingException.
 */
public LogMessage(LoggingEvent event, Layout layout)
{
    try
    {
        StringWriter out = new StringWriter(1024);
        out.write(layout.format(event));
        if ((event.getThrowableInformation() != null) && layout.ignoresThrowable())
        {
            for (String traceline : event.getThrowableStrRep())
            {
                out.write(traceline);
                out.write(Layout.LINE_SEP);
            }
        }
        out.close();

        this.timestamp = event.getTimeStamp();
        this.message = out.toString();
        this.messageBytes = this.message.getBytes("UTF-8");
    }
    catch (UnsupportedEncodingException e)
    {
        throw new RuntimeException("UnsupportedEncodingException when converting to UTF-8");
    }
    catch (Exception ex)
    {
        throw new RuntimeException("error creating LogMessage", ex);
    }
}
 
Example 14
Project: lib-commons-httpclient   File: FeedbackService.java   View Source Code Vote up 5 votes
public boolean process(final SimpleRequest request, final SimpleResponse response)
    throws IOException
{
    RequestLine requestline = request.getRequestLine();
    HttpVersion httpversion = requestline.getHttpVersion();

    StringWriter buffer = new StringWriter(100);
    buffer.write("Method type: ");
    buffer.write(requestline.getMethod());
    buffer.write("\r\n");
    buffer.write("Requested resource: ");
    buffer.write(requestline.getUri());
    buffer.write("\r\n");
    buffer.write("Protocol version: ");
    buffer.write(httpversion.toString());
    buffer.write("\r\n");
    
    String requestbody = request.getBodyString();
    if (requestbody != null && !requestbody.equals("")) {
        buffer.write("\r\n");
        buffer.write("Request body: ");
        buffer.write(requestbody);
        buffer.write("\r\n");
    }
    
    response.setStatusLine(httpversion, HttpStatus.SC_OK);
    response.setBodyString(buffer.toString());
    return true;
}
 
Example 15
Project: letv   File: bu.java   View Source Code Vote up 5 votes
public static String a(InputStream inputStream) throws IOException {
    InputStreamReader inputStreamReader = new InputStreamReader(inputStream);
    char[] cArr = new char[1024];
    StringWriter stringWriter = new StringWriter();
    while (true) {
        int read = inputStreamReader.read(cArr);
        if (-1 == read) {
            return stringWriter.toString();
        }
        stringWriter.write(cArr, 0, read);
    }
}
 
Example 16
Project: magic_imageloader_network   File: DiskLruCache.java   View Source Code Vote up 5 votes
/**
 * Returns the remainder of 'reader' as a string, closing it when done.
 */
public static String readFully(Reader reader) throws IOException {
    try {
        StringWriter writer = new StringWriter();
        char[] buffer = new char[1024];
        int count;
        while ((count = reader.read(buffer)) != -1) {
            writer.write(buffer, 0, count);
        }
        return writer.toString();
    } finally {
        reader.close();
    }
}
 
Example 17
Project: sstore-soft   File: SStoreTerminal.java   View Source Code Vote up 5 votes
private String formatResult(ClientResponse cr) {
    final VoltTable results[] = cr.getResults();
    final int num_results = results.length;
    StringBuilder sb = new StringBuilder();
    
    if (this.enable_debug) {
        sb.append(cr.toString());
    }
    else {
        // MAIN BODY
        if (this.enable_csv) {
            StringWriter out = new StringWriter();
            for (int i = 0; i < num_results; i++) {
                if (i > 0) out.write("\n\n");
                VoltTableUtil.csv(out, results[i], true);
            } // FOR
            sb.append(out.toString());
        } else {
            sb.append(VoltTableUtil.format(results));
        }
        
        // FOOTER
        String footer = "";
        if (this.enable_csv == false) {
            if (num_results == 1) {
                int row_count = results[0].getRowCount(); 
                footer = String.format("%d row%s in set", row_count, (row_count > 1 ? "s" : ""));
            }
            else if (num_results == 0) {
                footer = "No results returned";
            }
            else {
                footer = num_results + " tables returned";
            }
            sb.append(String.format("\n%s (%.2f sec)\n", footer, (cr.getClientRoundtrip() / 1000d)));
        }
    }
    return (sb.toString());
}
 
Example 18
Project: Logisim   File: PlaRom.java   View Source Code Vote up 5 votes
@Override
public String toStandardString(MemContents state) {
	int addr = state.getLogLength();
	int data = state.getWidth();
	StringWriter ret = new StringWriter();
	ret.write("addr/data: " + addr + " " + data + "\n");
	try {
		HexFile.save(ret, state);
	} catch (IOException e) {
	}
	return ret.toString();
}
 
Example 19
Project: s-store   File: HStoreTerminal.java   View Source Code Vote up 5 votes
private String formatResult(ClientResponse cr) {
    final VoltTable results[] = cr.getResults();
    final int num_results = results.length;
    StringBuilder sb = new StringBuilder();
    
    if (this.enable_debug) {
        sb.append(cr.toString());
    }
    else {
        // MAIN BODY
        if (this.enable_csv) {
            StringWriter out = new StringWriter();
            for (int i = 0; i < num_results; i++) {
                if (i > 0) out.write("\n\n");
                VoltTableUtil.csv(out, results[i], true);
            } // FOR
            sb.append(out.toString());
        } else {
            sb.append(VoltTableUtil.format(results));
        }
        
        // FOOTER
        String footer = "";
        if (this.enable_csv == false) {
            if (num_results == 1) {
                int row_count = results[0].getRowCount(); 
                footer = String.format("%d row%s in set", row_count, (row_count > 1 ? "s" : ""));
            }
            else if (num_results == 0) {
                footer = "No results returned";
            }
            else {
                footer = num_results + " tables returned";
            }
            sb.append(String.format("\n%s (%.2f sec)\n", footer, (cr.getClientRoundtrip() / 1000d)));
        }
    }
    return (sb.toString());
}
 
Example 20
Project: java-tools   File: WebServiceCall.java   View Source Code Vote up 4 votes
private String base64Encode(String srcString)
{
	String encodeArray = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

	byte[] src = srcString.getBytes();

	if (src.length == 0)
		return "";

	int tmpSize, i;
	int buff;

	tmpSize =src.length / 3 * 3;

	StringWriter dstWriter = new StringWriter((src.length * 4 / 3) + 8);

	for (i =0; i < tmpSize; i += 3)
	{
		buff = src[i] << 16 | src[i+1] << 8 | src[i+2];
		dstWriter.write(encodeArray.charAt((buff >> 18) & 0x3F));
		dstWriter.write(encodeArray.charAt((buff >> 12) & 0x3F));
		dstWriter.write(encodeArray.charAt((buff >> 6) & 0x3F));
		dstWriter.write(encodeArray.charAt((buff) & 0x3F));
	}

	int rest = src.length - tmpSize;
	if (rest == 2)
	{
		buff = src[i] << 8 | src[i+1];
		dstWriter.write(encodeArray.charAt((buff >> 10) & 0x3F));
		dstWriter.write(encodeArray.charAt((buff >> 4) & 0x3F));
		dstWriter.write(encodeArray.charAt((buff <<  2) & 0x3F));
		dstWriter.write("=");
	}
	else if (rest == 1)
	{
		buff = src[i];
		dstWriter.write(encodeArray.charAt((buff >> 2) & 0x3F));
		dstWriter.write(encodeArray.charAt((buff << 4) & 0x3F));
		dstWriter.write("==");
	}

	return dstWriter.toString();
}