Code example for WritableByteChannel

Methods: write

0
 
  public void writeString(WritableByteChannel out, String name) throws IOException
  { 
    byte[] nameBytes = name.getBytes(UTF8);
    writeInt(out, nameBytes.length);
    out.write(ByteBuffer.wrap(nameBytes));
  } 
 
  public String readString(InputStream in) throws IOException
  { 
    final int length = readInt(in);
    byte[] stringBytes = new byte[length];
    ByteStreams.readFully(in, stringBytes);
    return new String(stringBytes, UTF8);
  } 
 
  public String readString(ByteBuffer in) throws IOException
  { 
    final int length = in.getInt();
    byte[] stringBytes = new byte[length];
    in.get(stringBytes);
    return new String(stringBytes, UTF8);
  } 
 
  public void writeStrings(OutputStream out, String[] names) throws IOException
  { 
    writeStrings(out, Arrays.asList(names));
  } 
 
  public void writeStrings(OutputStream out, List<String> names) throws IOException
  { 
    writeInt(out, names.size());
 
    for (String name : names) {
      writeString(out, name);
    } 
  } 
 
  public String[] readStrings(InputStream in) throws IOException
  { 
    int length = readInt(in);
 
    String[] retVal = new String[length];
 
    for (int i = 0; i < length; ++i) {
      retVal[i] = readString(in);
    } 
 
    return retVal;
  } 
 
  public String[] readStrings(ByteBuffer in) throws IOException
  { 
    int length = in.getInt();
 
    String[] retVal = new String[length];
 
    for (int i = 0; i < length; ++i) {
      retVal[i] = readString(in);
    } 
 
    return retVal;
  } 
 
  public void writeInt(OutputStream out, int intValue) throws IOException
  { 
    byte[] outBytes = new byte[4];
 
    ByteBuffer.wrap(outBytes).putInt(intValue);
 
    out.write(outBytes);
  } 
 
  public void writeInt(WritableByteChannel out, int intValue) throws IOException
  { 
    final ByteBuffer buffer = ByteBuffer.allocate(4);
    buffer.putInt(intValue);
    buffer.flip();
    out.write(buffer);
  } 
 
  public int readInt(InputStream in) throws IOException
  { 
    byte[] intBytes = new byte[4];
Experience pair programming with AI  Get Codota for Java