/** * Constructor. * * @param enc encoding * @throws IOException I/O exception */ private Generic(final String enc) throws IOException { try { csd = Charset.forName(enc).newDecoder(); } catch (final Exception ex) { throw new EncodingException(ex); } }
/** * Constructor. * * @param os output stream * @param sopts serializer options * @throws QueryIOException query I/O exception */ protected OutputSerializer(final OutputStream os, final SerializerOptions sopts) throws QueryIOException { this.sopts = sopts; indent = sopts.yes(INDENT); // project-specific options indents = sopts.get(INDENTS); tab = sopts.yes(TABULATOR) ? '\t' : ' '; encoding = Strings.normEncoding(sopts.get(ENCODING), true); PrintOutput po; if (encoding == Strings.UTF8) { po = PrintOutput.get(os); } else { try { po = new EncoderOutput(os, Charset.forName(encoding)); } catch (final Exception ex) { throw SERENCODING_X.getIO(encoding); } } final int limit = sopts.get(LIMIT); if (limit != -1) po.setLimit(limit); final byte[] nl = token(sopts.get(NEWLINE).newline()); if (nl.length != 1 || nl[0] != '\n') po = new NewlineOutput(po, nl); out = po; }
/** * Tests response handling with specified charset in the header 'Content-Type'. * * @throws IOException I/O Exception * @throws QueryException query exception */ @Test public void responseWithCharset() throws IOException, QueryException { // Create fake HTTP connection final FakeHttpConnection conn = new FakeHttpConnection(new URL("http://www.test.com")); // Set content type conn.contentType = "text/plain; charset=CP1251"; // set content encoded in CP1251 final String test = "\u0442\u0435\u0441\u0442"; conn.content = Charset.forName("CP1251").encode(test).array(); final ItemList res = new HttpResponse(null, ctx.options).getResponse(conn, true, null); // compare results assertEquals(test, string(res.get(1).string(null))); }