Java Code Examples for java.nio.charset.Charset#equals()

The following examples show how to use java.nio.charset.Charset#equals() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
/**
 * Constructs a new {@code LineReader} with the specified capacity and charset.
 *
 * @param in the {@code InputStream} to read data from.
 * @param capacity the capacity of the buffer.
 * @param charset the charset used to decode data. Only US-ASCII, UTF-8 and ISO-8859-1 are
 *     supported.
 * @throws NullPointerException if {@code in} or {@code charset} is null.
 * @throws IllegalArgumentException if {@code capacity} is negative or zero
 *     or the specified charset is not supported.
 */
public StrictLineReader(InputStream in, int capacity, Charset charset) {
  if (in == null || charset == null) {
    throw new NullPointerException();
  }
  if (capacity < 0) {
    throw new IllegalArgumentException("capacity <= 0");
  }
  if (!(charset.equals(Util.US_ASCII))) {
    throw new IllegalArgumentException("Unsupported encoding");
  }

  this.in = in;
  this.charset = charset;
  buf = new byte[capacity];
}
 
Example 2
Source Project: candybar   File: StrictLineReader.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Constructs a new {@code LineReader} with the specified capacity and charset.
 *
 * @param in       the {@code InputStream} to read data from.
 * @param capacity the capacity of the buffer.
 * @param charset  the charset used to decode data. Only US-ASCII, UTF-8 and ISO-8859-1 are
 *                 supported.
 * @throws NullPointerException     if {@code in} or {@code charset} is null.
 * @throws IllegalArgumentException if {@code capacity} is negative or zero
 *                                  or the specified charset is not supported.
 */
public StrictLineReader(InputStream in, int capacity, Charset charset) {
    if (in == null || charset == null) {
        throw new NullPointerException();
    }
    if (capacity < 0) {
        throw new IllegalArgumentException("capacity <= 0");
    }
    if (!(charset.equals(Util.US_ASCII))) {
        throw new IllegalArgumentException("Unsupported encoding");
    }

    this.in = in;
    this.charset = charset;
    buf = new byte[capacity];
}
 
Example 3
Source Project: phonegapbootcampsite   File: StrictLineReader.java    License: MIT License 6 votes vote down vote up
/**
 * Constructs a new {@code LineReader} with the specified capacity and charset.
 *
 * @param in the {@code InputStream} to read data from.
 * @param capacity the capacity of the buffer.
 * @param charset the charset used to decode data. Only US-ASCII, UTF-8 and ISO-8859-1 are
 *     supported.
 * @throws NullPointerException if {@code in} or {@code charset} is null.
 * @throws IllegalArgumentException if {@code capacity} is negative or zero
 *     or the specified charset is not supported.
 */
public StrictLineReader(InputStream in, int capacity, Charset charset) {
  if (in == null || charset == null) {
    throw new NullPointerException();
  }
  if (capacity < 0) {
    throw new IllegalArgumentException("capacity <= 0");
  }
  if (!(charset.equals(Util.US_ASCII))) {
    throw new IllegalArgumentException("Unsupported encoding");
  }

  this.in = in;
  this.charset = charset;
  buf = new byte[capacity];
}
 
Example 4
/**
 * Constructs a new {@code LineReader} with the specified capacity and charset.
 *
 * @param in the {@code InputStream} to read data from.
 * @param capacity the capacity of the buffer.
 * @param charset the charset used to decode data. Only US-ASCII, UTF-8 and ISO-8859-1 are
 *     supported.
 * @throws NullPointerException if {@code in} or {@code charset} is null.
 * @throws IllegalArgumentException if {@code capacity} is negative or zero
 *     or the specified charset is not supported.
 */
public StrictLineReader(InputStream in, int capacity, Charset charset) {
  if (in == null || charset == null) {
    throw new NullPointerException();
  }
  if (capacity < 0) {
    throw new IllegalArgumentException("capacity <= 0");
  }
  if (!(charset.equals(Util.US_ASCII))) {
    throw new IllegalArgumentException("Unsupported encoding");
  }

  this.in = in;
  this.charset = charset;
  buf = new byte[capacity];
}
 
Example 5
/**
 * Constructs a new {@code LineReader} with the specified capacity and charset.
 *
 * @param in the {@code InputStream} to read data from.
 * @param capacity the capacity of the buffer.
 * @param charset the charset used to decode data. Only US-ASCII, UTF-8 and ISO-8859-1 are
 *     supported.
 * @throws NullPointerException if {@code in} or {@code charset} is null.
 * @throws IllegalArgumentException if {@code capacity} is negative or zero
 *     or the specified charset is not supported.
 */
public StrictLineReader(InputStream in, int capacity, Charset charset) {
  if (in == null || charset == null) {
    throw new NullPointerException();
  }
  if (capacity < 0) {
    throw new IllegalArgumentException("capacity <= 0");
  }
  if (!(charset.equals(Util.US_ASCII))) {
    throw new IllegalArgumentException("Unsupported encoding");
  }

  this.in = in;
  this.charset = charset;
  buf = new byte[capacity];
}
 
Example 6
Source Project: WliveTV   File: StrictLineReader.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * Constructs a new {@code LineReader} with the specified capacity and charset.
 *
 * @param in the {@code InputStream} to read data from.
 * @param capacity the capacity of the buffer.
 * @param charset the charset used to decode data. Only US-ASCII, UTF-8 and ISO-8859-1 are
 * supported.
 * @throws NullPointerException if {@code in} or {@code charset} is null.
 * @throws IllegalArgumentException if {@code capacity} is negative or zero
 * or the specified charset is not supported.
 */
public StrictLineReader(InputStream in, int capacity, Charset charset) {
	if (in == null || charset == null) {
		throw new NullPointerException();
	}
	if (capacity < 0) {
		throw new IllegalArgumentException("capacity <= 0");
	}
	if (!(charset.equals(Util.US_ASCII))) {
		throw new IllegalArgumentException("Unsupported encoding");
	}

	this.in = in;
	this.charset = charset;
	buf = new byte[capacity];
}
 
Example 7
/**
 * Constructs a new {@code LineReader} with the specified capacity and charset.
 *
 * @param in the {@code InputStream} to read data from.
 * @param capacity the capacity of the buffer.
 * @param charset the charset used to decode data. Only US-ASCII, UTF-8 and ISO-8859-1 are
 * supported.
 * @throws NullPointerException if {@code in} or {@code charset} is null.
 * @throws IllegalArgumentException if {@code capacity} is negative or zero
 * or the specified charset is not supported.
 */
public StrictLineReader(InputStream in, int capacity, Charset charset) {
	if (in == null || charset == null) {
		throw new NullPointerException();
	}
	if (capacity < 0) {
		throw new IllegalArgumentException("capacity <= 0");
	}
	if (!(charset.equals(Util.US_ASCII))) {
		throw new IllegalArgumentException("Unsupported encoding");
	}

	this.in = in;
	this.charset = charset;
	buf = new byte[capacity];
}
 
Example 8
/**
 * Constructs a new {@code LineReader} with the specified capacity and charset.
 *
 * @param in the {@code InputStream} to read data from.
 * @param capacity the capacity of the buffer.
 * @param charset the charset used to decode data. Only US-ASCII, UTF-8 and ISO-8859-1 are
 * supported.
 * @throws NullPointerException if {@code in} or {@code charset} is null.
 * @throws IllegalArgumentException if {@code capacity} is negative or zero
 * or the specified charset is not supported.
 */
public StrictLineReader(InputStream in, int capacity, Charset charset) {
	if (in == null || charset == null) {
		throw new NullPointerException();
	}
	if (capacity < 0) {
		throw new IllegalArgumentException("capacity <= 0");
	}
	if (!(charset.equals(Util.US_ASCII))) {
		throw new IllegalArgumentException("Unsupported encoding");
	}

	this.in = in;
	this.charset = charset;
	buf = new byte[capacity];
}
 
Example 9
Source Project: reader   File: StrictLineReader.java    License: MIT License 6 votes vote down vote up
/**
 * Constructs a new {@code LineReader} with the specified capacity and charset.
 *
 * @param in the {@code InputStream} to read data from.
 * @param capacity the capacity of the buffer.
 * @param charset the charset used to decode data. Only US-ASCII, UTF-8 and ISO-8859-1 are
 *     supported.
 * @throws NullPointerException if {@code in} or {@code charset} is null.
 * @throws IllegalArgumentException if {@code capacity} is negative or zero
 *     or the specified charset is not supported.
 */
public StrictLineReader(InputStream in, int capacity, Charset charset) {
  if (in == null || charset == null) {
    throw new NullPointerException();
  }
  if (capacity < 0) {
    throw new IllegalArgumentException("capacity <= 0");
  }
  if (!(charset.equals(Util.US_ASCII))) {
    throw new IllegalArgumentException("Unsupported encoding");
  }

  this.in = in;
  this.charset = charset;
  buf = new byte[capacity];
}
 
Example 10
/**
 * Constructs a new {@code LineReader} with the specified capacity and charset.
 *
 * @param in the {@code InputStream} to read data from.
 * @param capacity the capacity of the buffer.
 * @param charset the charset used to decode data. Only US-ASCII, UTF-8 and ISO-8859-1 are
 * supported.
 * @throws NullPointerException if {@code in} or {@code charset} is null.
 * @throws IllegalArgumentException if {@code capacity} is negative or zero
 * or the specified charset is not supported.
 */
public StrictLineReader(InputStream in, int capacity, Charset charset) {
  if (in == null || charset == null) {
    throw new NullPointerException();
  }
  if (capacity < 0) {
    throw new IllegalArgumentException("capacity <= 0");
  }
  if (!(charset.equals(Util.US_ASCII))) {
    throw new IllegalArgumentException("Unsupported encoding");
  }

  this.in = in;
  this.charset = charset;
  buf = new byte[capacity];
}
 
Example 11
Source Project: consulo   File: LoadTextUtil.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Overwrites file with text and sets modification stamp and time stamp to the specified values.
 * <p/>
 * Normally you should not use this method.
 *
 * @param requestor            any object to control who called this method. Note that
 *                             it is considered to be an external change if {@code requestor} is {@code null}.
 *                             See {@link VirtualFileEvent#getRequestor}
 * @param newModificationStamp new modification stamp or -1 if no special value should be set @return {@code Writer}
 * @throws IOException if an I/O error occurs
 * @see VirtualFile#getModificationStamp()
 */
public static void write(@Nullable Project project, @Nonnull VirtualFile virtualFile, @Nonnull Object requestor, @Nonnull String text, long newModificationStamp) throws IOException {
  Charset existing = virtualFile.getCharset();
  Pair.NonNull<Charset, byte[]> chosen = charsetForWriting(project, virtualFile, text, existing);
  Charset charset = chosen.first;
  byte[] buffer = chosen.second;
  if (!charset.equals(existing)) {
    virtualFile.setCharset(charset);
  }
  setDetectedFromBytesFlagBack(virtualFile, buffer);

  virtualFile.setBinaryContent(buffer, newModificationStamp, -1, requestor);
}
 
Example 12
Source Project: ph-commons   File: CharsetHelper.java    License: Apache License 2.0 5 votes vote down vote up
@Nullable
public static String getAsStringInOtherCharset (@Nullable final String sText,
                                                @Nonnull final Charset aCurrentCharset,
                                                @Nonnull final Charset aNewCharset)
{
  ValueEnforcer.notNull (aCurrentCharset, "CurrentCharset");
  ValueEnforcer.notNull (aNewCharset, "NewCharset");

  if (sText == null || aCurrentCharset.equals (aNewCharset))
    return sText;

  return new String (sText.getBytes (aCurrentCharset), aNewCharset);
}
 
Example 13
@Override
public ByteSource asByteSource(Charset charset) {
  if (charset.equals(this.charset)) {
    return ByteSource.this;
  }
  return super.asByteSource(charset);
}
 
Example 14
Source Project: Quicksql   File: AbstractSqlTester.java    License: MIT License 5 votes vote down vote up
public void checkCharset(
    String expression,
    Charset expectedCharset) {
  for (String sql : buildQueries(expression)) {
    RelDataType actualType = getColumnType(sql);
    Charset actualCharset = actualType.getCharset();

    if (!expectedCharset.equals(actualCharset)) {
      fail("\n"
          + "Expected=" + expectedCharset.name() + "\n"
          + "  actual=" + actualCharset.name());
    }
  }
}
 
Example 15
@Override
public ByteSource asByteSource(Charset charset) {
  if (charset.equals(this.charset)) {
    return ByteSource.this;
  }
  return super.asByteSource(charset);
}
 
Example 16
private boolean isPermittedEncoding(String name) {
    try {
        Charset cs = Charset.forName(name);
        for (Charset encoding : permittedEncodings) {
            if (encoding.equals(cs))
                return true;
        }
        return false;
    } catch (UnsupportedCharsetException e) {
        return false;
    }
}
 
Example 17
Source Project: letv   File: StrictLineReader.java    License: Apache License 2.0 5 votes vote down vote up
public StrictLineReader(InputStream in, int capacity, Charset charset) {
    if (in == null || charset == null) {
        throw new NullPointerException();
    } else if (capacity < 0) {
        throw new IllegalArgumentException("capacity <= 0");
    } else if (charset.equals(Util.US_ASCII)) {
        this.in = in;
        this.charset = charset;
        this.buf = new byte[capacity];
    } else {
        throw new IllegalArgumentException("Unsupported encoding");
    }
}
 
Example 18
public static boolean equalCharsets(Charset one, String two) {
    return one.name().equals(two) || one.equals(Charset.forName(two));
}
 
Example 19
Source Project: ph-css   File: CSSDataURL.java    License: Apache License 2.0 4 votes vote down vote up
/**
 * Full constructor
 *
 * @param aMimeType
 *        The MIME type to be used. May not be <code>null</code>. If you don't
 *        know provide the default MIME type from
 *        {@link CSSDataURLHelper#DEFAULT_MIME_TYPE}.
 * @param bBase64Encoded
 *        <code>true</code> if the data URL String representation should be
 *        Base64 encoded, <code>false</code> if not. It is recommended to set
 *        this to <code>true</code> if you have binary data like images.
 * @param aContent
 *        The content of the data URL as a byte array. May not be
 *        <code>null</code> but may be empty. This content may not be Base64
 *        encoded!
 * @param aCharset
 *        The charset to be used to encode the String. May not be
 *        <code>null</code>. The default is
 *        {@link CSSDataURLHelper#DEFAULT_CHARSET}.
 * @param sContent
 *        The String representation of the content. It must match the byte
 *        array in the specified charset. If this parameter is
 *        <code>null</code> than the String content representation is lazily
 *        created in {@link #getContentAsString()}.
 */
public CSSDataURL (@Nonnull final IMimeType aMimeType,
                   final boolean bBase64Encoded,
                   @Nonnull final byte [] aContent,
                   @Nonnull final Charset aCharset,
                   @Nullable final String sContent)
{
  ValueEnforcer.notNull (aMimeType, "MimeType");
  ValueEnforcer.notNull (aContent, "Content");
  ValueEnforcer.notNull (aCharset, "Charset");

  // Check if a charset is contained in the MIME type and if it matches the
  // provided charset
  final Charset aMimeTypeCharset = MimeTypeHelper.getCharsetFromMimeType (aMimeType);
  if (aMimeTypeCharset == null)
  {
    // No charset found in MIME type
    if (!aCharset.equals (CSSDataURLHelper.DEFAULT_CHARSET))
    {
      // append charset only if it is not the default charset
      m_aMimeType = ((MimeType) aMimeType.getClone ()).addParameter (CMimeType.PARAMETER_NAME_CHARSET,
                                                                     aCharset.name ());
    }
    else
    {
      // Default charset provided
      m_aMimeType = aMimeType;
    }
  }
  else
  {
    // MIME type has a charset - check if it matches the passed one
    if (!aMimeTypeCharset.equals (aCharset))
      throw new IllegalArgumentException ("The provided charset '" +
                                          aCharset.name () +
                                          "' differs from the charset in the MIME type: '" +
                                          aMimeTypeCharset.name () +
                                          "'");
    m_aMimeType = aMimeType;
  }
  m_bBase64Encoded = bBase64Encoded;
  m_aContent = ArrayHelper.getCopy (aContent);
  m_aCharset = aCharset;
  m_sContent = sContent;
}
 
Example 20
Source Project: p4ic4idea   File: RpcOutputStream.java    License: Apache License 2.0 4 votes vote down vote up
public RpcOutputStream(RpcPerforceFile file, Charset charset, boolean isUnicodeServer,
                       boolean useLocalDigester, int filesys_utf8bom) throws IOException {
	super(file);
	if (file == null) {
		throw new NullPointerError(
				"Null RpcPerforceFile passed to RpcOutputStream constructor");
	}

	if (useLocalDigester) {
		this.localDigester = new MD5Digester();
	}
	this.fileType = file.getFileType();
	if (charset != null
			&& !(this.fileType.equals(RpcPerforceFileType.FST_XUTF8) || this.fileType.equals(RpcPerforceFileType.FST_UTF8))) {
		this.charset = isUnicodeServer && !charset.equals(CharsetDefs.UTF8) ? charset : null;
	}
	this.closed = false;
	this.file = file;
	this.lineEnding = file.getLineEnding();
	this.writeUtf8Bom = false;

	if (this.fileType != null) {
		switch (this.fileType) {
			case FST_UTF16:
			case FST_XUTF16:
				this.charset = CharsetDefs.UTF16;
			case FST_UTF8:
			case FST_XUTF8:
				// We only write a UTF8 BOM if no charset was set or it is set to a value other
				// than UTF8
				boolean addBOM = (filesys_utf8bom == 1 || (filesys_utf8bom == 2 && SystemInfo.isWindows()));
				this.writeUtf8Bom = (this.charset == null) ? addBOM : false;
			case FST_UNICODE:
			case FST_XUNICODE:
				if ((this.charset != null) &&
						(isUnicodeServer || (this.charset == CharsetDefs.UTF16))) {
					this.converter = new CharsetConverter(CharsetDefs.UTF8, this.charset);
				}
			case FST_TEXT:
			case FST_XTEXT:
				if (ClientLineEnding.needsLineEndFiltering(lineEnding)) {
					this.lineEndStream = new RpcLineEndFilterOutputStream(
							this, this.lineEnding);
				}

				break;

			case FST_GUNZIP:
			case FST_XGUNZIP:
				this.inflater = new Inflater(true);
				this.crc = new RpcCRC32Checksum();
				this.checkedOutStream = new CheckedOutputStream(new BufferedOutputStream(this), this.crc);
				this.outStream = new RpcInflaterOutputStream(this.checkedOutStream, this.inflater,
						this.localDigester);
				this.headerRead = false;
				this.footerBytes = new byte[TRAILER_SIZE];
				break;

			default:
				break;
		}
	} else {
		this.fileType = RpcPerforceFileType.FST_TEXT;
	}
}