FileAccessPermissions implements Cloneable { public void chmod(String chmod) { CharSequenceScanner parse = new CharSequenceScanner(chmod); if (!parse.hasNext()) { throw new IllegalArgumentException(); } int mask = this.maskBits; try { int octal = parseOctalMode(parse); if (octal == -1) { while (parse.hasNext()) { mask = parseSymbolicMode(parse, mask); char c = parse.forceNext(); if ((c != ',') && (c != 0)) { throw new IllegalArgumentException("'" + c + "'"); } } } else { mask = octal; char c = parse.forceNext(); if (c != 0) { throw new IllegalArgumentException("'" + c + "'"); } } } catch (IllegalArgumentException e) { throw new IllegalArgumentException("Illegal chmod: " + chmod, e); } this.maskBits = mask; } FileAccessPermissions();  FileAccessPermissions(int mask); static FileAccessPermissions createByUmask(int umask, boolean isDirectory); int getMaskBits(); void setMaskBits(int mask); boolean isReadable(FileAccessClass fileModeClass); void setReadable(boolean readable); void setReadable(FileAccessClass fileModeClass, boolean readable); boolean isWritable(FileAccessClass fileModeClass); void setWritable(boolean writable); void setWritable(FileAccessClass fileModeClass, boolean writable); boolean isExecutable(FileAccessClass fileModeClass); void setExecutable(boolean executable); void setExecutable(FileAccessClass fileModeClass, boolean executable); boolean isSetuid(); void setSetuid(boolean setuid); boolean isSetgid(); void setSetgid(boolean setgid); boolean isSticky(); void setSticky(boolean sticky); void chmod(String chmod); @Override int hashCode(); @Override boolean equals(Object obj); @Override FileAccessPermissions clone(); @Override String toString(); static final int MASK_READABLE; static final int MASK_WRITABLE; static final int MASK_EXECUTABLE; static final int MASK_SETUID; static final int MASK_SETGID; static final int MASK_STICKY; }
@Test public void testChmod() { FileAccessPermissions mask = new FileAccessPermissions(); mask.chmod("a+x"); assertEquals(00111, mask.getMaskBits()); mask.chmod("g-x"); assertEquals(00101, mask.getMaskBits()); mask.chmod("u+w,g+r,o-x"); assertEquals(00340, mask.getMaskBits()); mask.chmod("o+rxw"); assertEquals(00347, mask.getMaskBits()); mask.chmod("o=rw"); assertEquals(00346, mask.getMaskBits()); mask.chmod("o=x"); assertEquals(00341, mask.getMaskBits()); mask.chmod("a="); assertEquals(00000, mask.getMaskBits()); mask.chmod("0124"); assertEquals(00124, mask.getMaskBits()); mask.chmod("=u"); assertEquals(00111, mask.getMaskBits()); mask.chmod("="); assertEquals(00000, mask.getMaskBits()); mask.chmod("g=rw,uo=x,+s"); assertEquals(04161, mask.getMaskBits()); mask.chmod("+g"); assertEquals(04767, mask.getMaskBits()); mask.chmod("+S,u-w"); assertEquals(06567, mask.getMaskBits()); mask.chmod("+t,-s,o-u,+o"); assertEquals(03762, mask.getMaskBits()); }